Selecting From a Menu of Items

If you want your script to give someone a set menu of options and then want to act on his responses, the case statement is a convenient way to do it. It takes the following form:

case value in
   pattern1 )    command
        ...
        ;;
   pattern2 )     command
        ...
        ;;
        ...
   patternx )     command
        ...
        ;;
esac

Here, value is any text string that will be compared with the patterns pattern1, pattern2,and so on to patternx. The command list following the first pattern that matches value will be executed. Usually, a catch-all pattern (such as *) will be used as the last pattern to catch anything that you weren’t expecting or that the user mistyped. Note the required double semicolons (;;) after the last command in each pattern section. Don’t forget ...

Get Practical UNIX now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.