-->
Previous Table of Contents Next


Using case

The case structure is a decision structure that lets you select one of several courses of action, based on the value of a variable. Listing 18.2 shows a short menu program.

Listing 18.2 Implementing a Menu Shell Script with case


# Name:     ShrtMenu

# Purpose:   Allow user to print a file, delete a file,

#            or quit the program

# Display menu

       echo “Please choose either P, D, or Q to ”

        echo “ [P]rint a file”

        echo “ [D]elete a file”

       echo “ [Q]uit”

# Get response from user

     read response

# Use case to match response to action

     case $response in

         P|p) echo “Name of file to print?”

              read filename

               lp $filename;;

         D|d) echo “Name of file to delete?”

             read filename

             rm $filename;;

           *) echo “leaving now”;;

     esac

The syntax of the case statement is this:


case word in

        pattern) statement(s);;

        pattern) statement(s);;

     …

 esac

The word parameter is matched against each pattern parameter, starting with the pattern at the top of the list. The statements that execute if word matches a pattern are terminated by two semicolons (;;). The end of the case statement is marked by the word esac (that’s “case” spelled backward).

In Listing 18.2, the pipe character was used to give a choice for a match. For example, P|p means that either an uppercase or lowercase letter P is considered a match.

The pattern * is used to represent all other patterns not explicitly stated. If users press any key besides <P>, <p>, <D>, or <d>, they exit from the menu.

Listing 18.3 uses a case statement that makes a selection based on the number of parameters the shell represents as $#.

Listing 18.3 Command-Line Parsing with case


# Name:     recent

# Purpose:  list the most recent files in a directory

# If user types recent <Return> then the names of

#      the 10 most recently modified files are displayed

# If the user types recent n <Return> then the names of

#      the n most recently modified files are displayed

# Otherwise, user is notified of incorrect usage

#

# Case based on number of parameters

      case $# in

             0) ls -lt | head ;;

                  # ls -lt lists names of file in order of

                # most recently modified

                  # head displays the first 10 lines of a file

             1) case $1 in

                  [0-9]*) ls -lt | head -$1 ;;

                   *)echo “Usage: recent number-of-files”;;

                esac;;

               *) echo “Usage: recent number-of-files”;;

      esac

Finding the Exit Status

When a shell command executes, it’s either successful or not. If you use the command grep “American Terms” customers to see whether the string American Terms is in the file customers, and the file exits, you have read permission to the file, and American Terms is in the file, the shell command has executed successfully. If any of those conditions isn’t true, the shell command executes unsuccessfully.

The shell always reports back about the status of the termination of a command, program, or shell script. The value reported back is called the exit status of a command and is represented by the variable #?. If you enter the following commands, you see the value of $?.


grep “American Terms” customers

echo $?


NOTE:  If $? has a value of 0, this command was successful; otherwise, the command was unsuccessful.

The following is an example in which the exit status of the command who|grep $1 is used in the case statement:


# Name:       just.checking

# Purpose:   Determine if person is logged in

# Usage:      just.checking login_name

#

     case ‘who | grep $1 > /dev/null` in

            0) echo “$1 is logged in.”;;

             *) echo “$1 is not here. Try again later.”;;

     esac

     echo “Have a great day!”

If you enter just.checking rflame and rflame is logged in, you see the following:


rflame is logged in.

Have a great day!

If rflame isn’t logged in, you see this instead:


rflame is not here. Try again later.

Have a great day!

Using if Structures

The if…then…else…fi structure is a decision structure that allows you to select one of two courses of action based on the result of a command. The else portion of the structure is optional. One or more commands go in place of the ellipsis (…). Provided that the exit status of the last command following the if is zero (that is, the command executed successfully), the commands following the then and preceding the else (if there is one) are executed. Otherwise, the commands following the else are executed.

In other words, one or more commands are executed. If the last command was successful, the commands in the then portion of the statement are performed and then the commands following the fi (the end of the structure) are executed. If the last commands aren’t successful, the commands after the else are performed.

Here’s a familiar example that behaves exactly the same as when it was written using the case statement:


# Name:      just.checking

# Purpose:  Determine if person is logged in

# Usage:     just.checking login_name

#

if

    who | grep $1 > /dev/null

then

     echo “$1 is logged in.”

else

     echo “$1 is not here. Try again later.”

fi

echo “ Have a great day!”


Previous Table of Contents Next