Previous | Table of Contents | Next |
The case statement is the other major form of flow control available in the shell. In this section I explain its usage.
The basic syntax is
case word in pattern1) list1 ;; pattern2) list2 ;; esac
Here the string word is compared against every pattern until a match is found. The list following the matching pattern executes. If no matches are found, the case statement exits without performing any action. There is no maximum number of patterns, but the minimum is one.
When a list executes, the command ;; indicates that program flow should jump to the end of the entire case statement. This is similar to break in the C programming language.
Some programmers prefer to use the form
case word in pattern1) list1 ;; pattern2) list2 ;; esac
This form should be used only if the list of commands to be executed is short.
Consider the following variable declaration and case statement:
FRUIT=kiwi case "$FRUIT" in apple) echo "Apple pie is quite tasty." ;; banana) echo "I like banana nut bread." ;; kiwi) echo "New Zealand is famous for kiwi." ;; esac
The execution of the case statement is as follows:
New Zealand is famous for kiwi.
In the previous example, you used fixed strings as the pattern. If used in this fashion the case statement degenerates into an if statement. For example, the if statement
if [ "$FRUIT" = apple ] ; then echo "Apple pie is quite tasty." elif [ "$FRUIT" = banana ] ; then echo "I like banana nut bread." elif [ "$FRUIT" = kiwi ] ; then echo "New Zealand is famous for kiwi." fi
is more verbose, but the real power of the case statement does not lie in simplifying if statements. The power lies in the fact that it uses patterns to perform matching.
A pattern is a string that consists of regular characters and special wildcard characters. The pattern determines whether a match is present.
The patterns can use the same special characters as patterns for pathname expansion covered in Chapter 8, Substitution, along with the or operator, |. Some default actions can be performed by giving the * pattern, which matches anything.
An example of a simple case statement that uses patterns is
case "$TERM" in *term) TERM=xterm ;; network|dialup|unknown|vt[0-9][0-9][0-9]) TERM=vt100 ;; esac
Here the string contained in $TERM is compared against two patterns. If this string ends with the string term, $TERM is assigned the value xterm. Otherwise ,$TERM is compared against the strings network, dialup, unknown, and vtXXX, where XXX is some three digit number, such as 102. If one of these strings matches, $TERM is set to vt100.
In this chapter, you covered the two main flow control mechanisms available in the shell. You looked at the following topics related to the if statement:
You also looked at the basic case statement and using pattern in conjunction with it.
Starting with the next chapter, you begin to see how you can use flow control while programming.
if [ -e /usr/local/bin/bash ] ; then /usr/local/bin/bash ; fi if [ -x /usr/local/bin/bash ] ; then /usr/local/bin/bash ; fi
HOME=/home/ranga BINDIR=/home/ranga/bin
if [ $HOME/bin = $BINDIR ] ; then echo "Your binaries are stored in your home directory." fi
if [ "$ANS" = "Yes" o "$ANS" = "yes" o "$ANS" = "y" o "$ANS" = "Y" ] ; then ANS="y" else ANS="n" fi
Previous | Table of Contents | Next |