Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


The case Statement

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.

A case Statement Example

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:

1.  The string contained in the variable FRUIT is expanded to kiwi.
2.  The string kiwi is compared against the first pattern, apple. Because they don’t match, you go to the next pattern.
3.  The string kiwi is compared against the next pattern, banana. Because they don’t match, you go to the next pattern.
4.  The string kiwi is compared against the final pattern, kiwi. Because they match, the following message is produced:
New Zealand is famous for kiwi.

Using Patterns

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.

Summary

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:

  Performing file tests
  Performing string comparisons
  Performing numerical comparisons
  Using compound expressions

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.

Questions

1.  What is the difference between the following commands?
if [ -e /usr/local/bin/bash ] ; then /usr/local/bin/bash ; fi
if [ -x /usr/local/bin/bash ] ; then /usr/local/bin/bash ; fi
2.  Given the following variable declarations,
HOME=/home/ranga
BINDIR=/home/ranga/bin

what is the output of the following if statement?
if [ $HOME/bin = $BINDIR ] ; then
    echo "Your binaries are stored in your home directory."
fi
3.  What test command should be used in order to test whether /usr/bin is a directory or a symbolic link?
4.  Given the following if statement, write an equivalent case statement:
if [ "$ANS" = "Yes" –o "$ANS" = "yes" –o "$ANS" = "y" –o "$ANS" = "Y"
] ; then
    ANS="y"
else
    ANS="n"
fi


Previous Table of Contents Next