Bourne/bash shell scripts: case statement

Contributor Icon Contributed by Rex  
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

 

8 Comments -


  1. Anonymous said on December 31, 2008

    case “$1″ in
    stdin|
    -)
    commands
    ;;

    What does stdin|
    -) mean here?
    Can somebody explain it?

  2. newbie said on April 21, 2009

    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)

  3. S.Raghu said on June 13, 2009

    How to branch if the value of the variable is ‘*’ ?
    For eg parameters passed to a program are 2 numbers and an arithmetical operand.

  4. Patrick said on March 5, 2010

    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

  5. Mark said on March 28, 2010

    Thanks!

  6. Garry Hurley Jr said on October 11, 2010

    Simple. You need to use the escape character on the switch, and you escape it as a parameter. Here is a basic example of that line

    #!/bin/bash
    case $1 in
    %)
    echo “%”
    ;;
    *)
    echo “*”
    ;;
    *)
    echo “none of the above”
    ;;
    esac

    Sample output:

    snoopy:~>./testBash *
    *

    My suggestion is to use the ‘x’ for multiplication instead if you don’t want your users to have to escape the ‘*’ every time they use the program.

  7. Sony Sebastian said on January 19, 2011

    #!/bin/sh

    echo “Enter your choice:”
    read username

    case “$username” in
    ’start’)
    echo ’starting…………’;;
    ’stop’)
    echo ’stopping………..’;;
    ‘restart’)
    echo ‘restarting………’;;
    esac

    ##################
    Output:
    ./test.sh

    Enter your choice:
    start
    starting…………

  8. Ocho said on January 27, 2012

    read input
    case “${input}” in
    y|Y|yes|YES|Yes)
    echo “Do some yes things…”
    ;;
    n|N|no|NO|No)
    echo “Do some no things”
    ;;
    *)
    echo “${input} is not a valid input. ”
    # loop back in via a function or return 1 or quit etc.
    ;;
    esac

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -