-->
Previous Table of Contents Next


The if Statement

All three shells support nested if-then-else statements. These statements provide you with a way of performing complicated conditional tests in your shell programs. The syntax of the if statement is the same for bash and pdksh and is shown here:


if [ expression ]

then

      commands

elif [ expression2 ]

then

      commands

else

      commands

fi


Note:  
The elif and else clauses are both optional parts of the if statement. Also note that bash and pdksh use the reverse of the statement name in most of their complex statements to signal the end of the statement. In this statement, the fi keyword is used to signal the end of the if statement.

The elif statement is an abbreviation of else if. This statement is executed only if none of the expressions associated with the if statement or any elif statements before it were true. The commands associated with the else statement are executed only if none of the expressions associated with the if statement or any of the elif statements were true.

In tcsh, the if statement has two different forms. The first form provides the same function as the bash and pdksh if statement. This form of if statement has the following syntax:


if (expression1) then

      commands

else if (expression2) then

      commands

else

      commands

endif

The second form of if statement provided by tcsh is a simple version of the first if statement. This form of if statement evaluates only a single expression. If the expression is true, it executes a single command; if the expression is false, nothing happens. The syntax for this form of if statement is the following:


if (expression) command

This statement can be written using the first form of if statement by writing the if without any else or else if clauses. This form just saves a little typing.

The following is an example of a bash or pdksh if statement. This statement checks to see whether there is a .profile file in the current directory:


if [ -f .profile ]

then

      echo “There is a .profile file in the current directory.”

else

      echo “Could not find the .profile file.”

fi

The same statement written using the tcsh syntax is shown here:


#

if ( { -f .profile } ) then

     echo “There is a .profile file in the current directory.”

else

     echo “Could not find the .profile file.”

endif


Note:  
Notice that in the tcsh example the first line starts with a #. This is required for tcsh to recognize the file containing the commands as a tcsh script file.

The case Statement

The case statement enables you to compare a pattern with several other patterns and execute a block of code if a match is found. The shell case statement is quite a bit more powerful than the case statement in Pascal or the switch statement in C. This is because in the shell case statement you can compare strings with wildcard characters in them, whereas with the Pascal and C equivalents, you can only compare enumerated types or integer values.

Once again, the syntax for the case statement is identical for bash and pdksh and different for tcsh. The syntax for bash and pdksh is the following:


case string1 in

      str1)

            commands;;

      str2)

            commands;;

      *)

            commands;;

esac

string1 is compared to str1 and str2. If one of these strings matches string1, all commands up to the double semicolon (;;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings.

The tcsh equivalent of the bash and pdksh case statement is called the switch statement. This statement’s syntax closely follows the C switch statement syntax. Here it is:


switch (string1)

      case str1:

           statements

      breaksw

      case str2:

           statements

      breaksw

      default:

           statements

      breaksw

endsw

This behaves in the same manner as the bash and pdksh case statement. Each string following the keyword case is compared with string1. If any of these strings matches string1, the code follows it until the breaksw keyword is executed. If none of the strings match, the code follows the default keyword until the breaksw keyword is executed.

The following code is an example of a bash or pdksh case statement. This code checks to see if the first command-line option is -i or -e. If it is -i, the program counts the number of lines in the file specified by the second command-line option that begins with the letter i. If the first option is -e, the program counts the number of lines in the file specified by the second command-line option that begins with the letter e. If the first command-line option is not -i or -e, the program prints a brief error message to the screen.


case $1 in

   -i)

      count=‘grep ^i $2 | wc -l‘

      echo “The number of lines in $2 that start with an i is $count”

      ;;

   -e)

      count=‘grep ^e $2 | wc -l‘

      echo “The number of lines in $2 that start with an e is $count”

      ;;

   * )

      echo “That option is not recognized”

      ;;

esac

The same example written in tcsh syntax is shown here:


# remember that the first line must start with a # when using tcsh

switch ( $1 )

   case -i | i:

      set count = ‘grep ^i $2 | wc -l‘

      echo “The number of lines in $2 that begin with i is $count”

   breaksw

   case -e | e:

      set count = ‘grep ^e $2 | wc -l‘

      echo “The number of lines in $2 that begin with e is $count”

   breaksw

   default:

      echo “That option is not recognized”

   breaksw

endsw


Previous Table of Contents Next