10.7 Branching

   

Two types of branching are used in shell programming. One of these is represented by the if structure and the other one by the case structure. In this section, you will learn how to use both of these branches. You will also find some useful examples of shell programs.

The if-then-fi Structure

The if-then-fi structure is used to check a condition with the help of a test command. If the test returns a true value, then an action is performed. If the test returns a false value (not true), the action part of the program is not executed. The general syntax of this structure is as follows .

 if  expr  then  action  fi 

where expr is a test performed on some variables or constants. The action is a set of one or more commands that are executed if the test returned a true value. This is shown in Figure 10-1 using a flow diagram.

Figure 10-1. The if-then-fi structure.

graphics/10fig01.gif

Previously you have seen how to check the number of arguments on the command line. You can write a shell program to check the number of arguments and print an error message if there is no argument on the command line. The program script-08 does the same thing and is listed here using the cat command.

 $  cat script-08  #!/usr/bin/sh if [ "$#" -lt 1 ] then    echo "Error: No arguments provided on command line"    exit 3 fi echo "Program terminated normally" $ 

When you execute this program without any command line argument, you will see the following message on your screen.

 Error: No arguments provided on command line 

If you check the return code using the echo $? command, a value of 3 will appear. On the other hand, if you provide at least one argument on the command line, the return code will be zero and the message on your screen will be:

 Program terminated normally 

This is a useful way of checking the number of command line arguments if your program expects a fixed number of arguments. For example, the cp command requires two command line arguments showing the source and destination paths of the file being copied . If you don't provide two arguments, the command prints an error message.

The if-then-else-fi Structure

This structure is used when you want to perform one of two actions, depending on the result of a test. The general syntax of the structure is:

 if  expr  then    action1 else  action2  fi 

If the result of expr is true, action1 is performed. In the other case, action2 is performed. Each of action1 and action2 may be a set of one or more commands. The flow chart of this structure is shown in Figure 10-2.

Figure 10-2. The if-then-else-fi structure.

graphics/10fig02.gif

You can modify script-08 so that it accepts two file names as argument and copies the first file to the second file. If there are not exactly two arguments, it tells you that you did not provide two arguments. It then displays all of the arguments it received from the command line. The modified form is script-09 .

 #!/usr/bin/sh if [ "$#" -eq 2 ] then    cp   else    echo "You did not provide two arguments."    echo "The arguments provided are $*" fi echo "Program terminated" 

This program works fine, but it does not incorporate any facility for dealing with a problem. The next program, script-10 , is a more sophisticated shell program that performs a number of checks before copying one file to another. It first checks the number of arguments, and if you have not provided two arguments, it terminates at that point. Then it checks if the source and destination files are the same. If they are, it terminates at this point. The next check is to verify that the source file exists and that it is not a special file. After that, it checks if the source file exists and is readable. If both of the conditions are true, it checks the destination file. If the destination file already exists, it asks if you want to overwrite it. If you say "yes," it copies the source file to the destination; otherwise it terminates. If the destination file does not exist, it copies the source to the destination without interacting with you.

 #!/usr/bin/sh if [ "$#" -ne 2 ] then    echo "You have not provided exactly two arguments."    exit 1 fi if [ "" = "" ] then    echo "Source and destination names are the same."    exit 1 fi if [ ! -f "" ] then                  echo "File  does not exist or not a regular file."    exit 2 fi if [ ! -r "" ] then    echo "File  is not readable."    exit 3 fi if [ -f  ] then    echo "File  already exists. Do you want"    echo "to  overwrite (yes/no): \c"    read ANSWER    if [ "$ANSWER" = "yes" ]    then       cp         echo "File copied"       exit 0    else       echo "File not copied"       exit 4    fi fi cp   echo "File copied" 

The exit status is different at every point so that you may come to know at what stage the program terminated by checking the exit code. You can use this program in place of the standard cp command as it is safer and does not overwrite existing files without notification.

The case Structure

The case structure is used where you want to branch to multiple program segments depending on the value of a variable. The general syntax of the case structure is:

 case  var  in    pattern1)       commands       ;;    pattern2)       commands       ;;        patternn)       commands       ;;    *)       commands       ;; esac 

The value of var is checked. If this is equal to pattern1 , the commands in the first block are executed. The first block ends at the ;; pattern. If the value of var matches pattern2 , the commands in the second block are executed. If var matches none of the pattern values, the commands in the last block after " *) " are executed. This last part is optional and can be omitted. The case statement is shown in the flow diagram of Figure 10-3.

Figure 10-3. The case structure.

graphics/10fig03.gif

Program script-11 is used as an example here. It provides you with a few choices. If you select one of these, appropriate action is taken.

 #!/usr/bin/sh echo "Press w to list users" echo "Press d to see date and time" echo "Press p to see current directory" echo echo "Enter your choice: \c" read VAR case $VAR in    wW) who         ;;    dD) date         ;;    pP) pwd         ;;    *)   echo "You have not pressed a valid key" esac echo "The case structure finished" 

For example, if you press " w " or " W ", the list of users currently logged into the system is displayed. Note that the " " character is used for the logical OR operation in the case structure. If you make a choice that is not valid, the last part of the case structure is executed, which shows that you need to make a valid choice. Wildcard and range characters ( * , ? , [ ] ) can also be used in patterns to be matched.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net