Bourne/bash shell scripts: case statement

Contributor Icon Contributed by Rex Date Icon October 22, 2003  
Tag Icon Tagged: Bourne shell scripting

The case statement is an elegant replacement for if/then/else if/else statements when making numerous comparisons. This recipe describes the case statement syntax for the Bourne shells (sh, ksh, bash, zsh, etc.).


case "$var" in
value1)
commands;
;;
value2)
commands;
;;
*)
commands;
;;
esac

The case statement compares the value of the variable ($var in this case) to one or more values (value1, value2, …). Once a match is found, the associated commands are executed and the case statement is terminated. The optional last comparison *) is a default case and will match anything.

For example, branching on a command line parameter to the script, such as ’start’ or ’stop’ with a runtime control script. The following example uses the first command line parameter ($1):

case "$1" in
'start')
/usr/app/startup-script
;;
'stop')
/usr/app/shutdown-script
;;
'restart')
echo "Usage: $0 [start|stop]"
;;
esac

Previous recipe | Next recipe |
 
  • S.Raghu
    How to branch if the value of the variable is '*' ?
    For eg parameters passed to a program are 2 numbers and an arithmetical operand.
  • Ravi_Kiran
    case "$1" in
    stdin|\
    -)
    commands
    ;;

    What does stdin|\
    -) mean here?
    Can somebody explain it?
  • newbie
    stdin means "standard input" from the keyboard (file descriptor=0)
    stdout means "standard output" to the computer screen (file descriptor=1)
    stderr means "standard error"; output for error messages (file descriptor=2)
  • Anonymous
    It's possible to use even more complicated syntax with regular expressions:

    case "$1" in
    +(start|run) ) /usr/app/startup-script ;;
    @([Ss])top ) /usr/app/stop-script ;;
    esac

    ?(pattern1 | pattern2 | ... | patternn)
    zero or one occurrence of any pattern

    *( pattern1 | pattern2 | ... | patternn)
    zero or more occurrences of any pattern

    @( pattern1 | pattern2 | ... | patternn)
    exactly one occurrence of any pattern

    +( pattern1 | pattern2 | ... | patternn)
    one or more occurrence of any pattern

    !( pattern1 | pattern2 | ... | patternn)
    all strings except those that match any pattern
  • Patrick
    Nice this one was really usefull, but the last one didn't seem to work for me.
    It would whine about a syntax error, so I removes the ! in front and added a *) to catch all the rest, in the case I just did a echo "parameter is valid"

    But thanx a lot
  • Matir
    Actually, that should be '*)'. If you look, it's trying to give syntax for errors. restart would read like: 'restart')
    $0 stop
    $1 start
    ;;
  • for great restart
    there is an error in the second example


    case "$1" in
    'start')
    /usr/app/startup-script
    ;;
    'stop')
    /usr/app/shutdown-script
    ;;
    --> 'start') <-- here error, should be restart
    echo "Usage: $0 [start|stop]"
    ;;
    esac
  • hpx
    :lol:
    good job!
blog comments powered by Disqus