Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Conditional Statements

Conditional statements are used in shell programs to decide which part of the program to execute depending on specified conditions.

The if Statement

The if statement evaluates a logical expression to make a decision. An if condition has the following format in pdksh and bash:

if [ expression ]; then
    Statements
elif [expression ]; then
    Statements
else
    Statements
fi

The if conditions can be nested. That is, an if condition can contain another if condition within it. It is not necessary for an if condition to have an elif or else part. The else part is executed if none of the expressions that are specified in the if statement and are optional in subsequent elif statements are true. The word fi is used to indicate the end of the if statements, which is very useful if you have nested if conditions. In such a case, you should be able to match fi to if to ensure that all if statements are properly coded.

In the following example, a variable var can have either of two values: Yes or No. Any other value is an invalid value. This can be coded as follows:

if [ $var = "Yes" ]; then
   echo "Value is Yes"
elif [ $var = "No" ]; then
   echo "Value is No"
else
   echo "Invalid value"
fi

In tcsh, the if statement has two forms. The first form, similar to the one for pdksh and bash, is as follows:

if (expression) then
    Statements
else if (expression) then
    Statements
else
    Statements
endif

The if conditions can be nested--that is, an if condition can contain another if condition within it. It is not necessary for an if condition to have an else part. The else part is executed if none of the expressions specified in any of the if statements are true. The optional if part of the statement (else if ( expression ) then) is executed if the condition following it is true and the previous if statement is not true. The word endif is used to indicate the end of the if statements, which is very useful if you have nested if conditions. In such a case, you should be able to match endif to if to ensure that all if statements are properly coded.

Remember the example of the variable var having only two values, Yes and No, for pdksh and bash? Here is how it would be coded with tcsh:

if ($var == "Yes") then
   echo "Value is Yes"
else if ($var == "No" ) then
   echo "Value is No"
else
   echo "Invalid value"
endif

The second form of the if condition for tcsh is as follows:

if (expression) command

In this format, only a single command can be executed if the expression evaluates to true.

The case Statement

The case statement is used to execute statements depending on a discrete value or a range of values matching the specified variable. In most cases, you can use a case statement instead of an if statement if you have a large number of conditions.

The format of a case statement for pdksh and bash is as follows:

case str in
   str1 | str2)
      Statements;;
   str3|str4)
      Statements;;
   *)
      Statements;;
esac

You can specify a number of discrete values—such as str1, str2, and so on—for each condition, or you can specify a value with a wildcard. The last condition should be * (asterisk) and is executed if none of the other conditions are met. For each of the specified conditions, all of the associated statements until the double semicolon (;;) are executed.

You can write a script that will echo the name of the month if you provide the month number as a parameter. If you provide a number that is not between 1 and 12, you will get an error message. The script is as follows:

#!/bin/sh

case $1 in
   01 | 1) echo "Month is January";;
   02 | 2) echo "Month is February";;
   03 | 3) echo "Month is March";;
   04 | 4) echo "Month is April";;
   05 | 5) echo "Month is May";;
   06 | 6) echo "Month is June";;
   07 | 7) echo "Month is July";;
   08 | 8) echo "Month is August";;
   09 | 9) echo "Month is September";;
   10) echo "Month is October";;
   11) echo "Month is November";;
   12) echo "Month is December";;
   *) echo "Invalid parameter";;
esac

You need to end the statements under each condition with a double semicolon(;;). If you do not, the statements under the next condition will also be executed.

The format for a case statement for tcsh is as follows:

switch (str)
   case str1|str2:
      Statements
      breaksw
   case str3|str4:
      Statements
      breaksw
   default:
      Statements
      breaksw
endsw

You can specify a number of discrete values—such as str1, str2, and so on—for each condition, or you can specify a value with a wildcard. The last condition should be default and is executed if none of the other conditions are met. For each of the specified conditions, all of the associated statements until breaksw are executed.

The example that echoes the month when a number is given, shown earlier for pdksh and bash, can be written in tcsh as follows:

#!/bin/tcsh

set month = 5
switch  ( $month )
   case 1:
      echo "Month is January"
      breaksw
   case 2:
      echo "Month is February"
      breaksw
   case 3:
      echo "Month is March"
      breaksw
   case 4:
      echo "Month is April"
      breaksw
   case 5:
      echo "Month is May"
      breaksw
   case 6:
      echo "Month is June"
      breaksw
   case 7:
      echo "Month is July";;
      breaksw
   case 8:
      echo "Month is August";;
      breaksw
   case 9:
      echo "Month is September"
      breaksw
   case 10:
      echo "Month is October"
      breaksw
   case 11:
      echo "Month is November"
      breaksw
   case 12:
      echo "Month is December"
      breaksw
   default:
      echo "Oops! Month is Octember!"
      breaksw
endsw

You need to end the statements under each condition with breaksw. If you do not, the statements under the next condition will also be executed.

Share ThisShare This

Informit Network