pollard
pollard

Reputation: 37

Using case statement within select loop

I am currently doing bash programming wherein I am using case statement within select loop.However,whenever I enter any input, the control goes to default case and not the case which it should actually go to.For eg. when I enter date,instead of going to date case, it goes to default case. Can anybody of you notice some error?

 #!/bin/bash
    select command in date pwd ls
    do
    case $command in
         date)date;;
         pwd)pwd;;
         ls)ls;;
         *)echo"wrong";;

    esac
    done

Upvotes: 0

Views: 230

Answers (1)

janos
janos

Reputation: 124646

[...] when I enter date,instead of going to date case, it goes to default case.

The select builtin works with numbered options. You need to enter 1 for the first option "date", 2 for the second option "pwd", and so on.

The relevant parts from help select:

select: select NAME [in WORDS ... ;] do COMMANDS; done
    [...]  If the line consists of the number
    corresponding to one of the displayed words, then NAME is set
    to that word.  If the line is empty, WORDS and the prompt are
    redisplayed.  If EOF is read, the command completes.  Any other
    value read causes NAME to be set to null.  [...]

Upvotes: 1

Related Questions