-->

Previous | Table of Contents | Next

Page 431


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 (else if (expression) then) part of the statement 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. This 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 the 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 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 will be executed if none of the other conditions are met. For each of the specified conditions, all the associated statements until the double semicolon (;;) are executed.

Page 432

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 other than one between 1 and 12, then you will get an error message. The script is as follows:


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

It is important that you end the statements under each condition with a double semicolon (;;). If you do not do that, then 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 will be executed if none of the other conditions are met. For each of the specified conditions, all 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:


switch  ( $1 )

   case 01 | 1:

      echo "Month is January"

      breaksw

   case 02 | 2:

      echo "Month is February"

      breaksw

   case 03 | 3:

      echo "Month is March"

      breaksw



Page 433


   case 04 | 4:

      echo "Month is April"

      breaksw

   case 05 | 5:

      echo "Month is May"

      breaksw

   case 06 | 6:

      echo "Month is June"

      breaksw

   case 07 | 7:

      echo "Month is July"

      breaksw

   case 08 | 8:

      echo "Month is August"

      breaksw

   case 09 | 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 "Invalid parameter"

      breaksw

endsw

It is important that you end the statements under each condition with breaksw. If you do not, the statements under the next condition will also be executed.

Miscellaneous Statements

There are two other statements that you should be aware of. These are the break statement and the exit statement.

The break Statement

The break statements can be used to terminate an iteration loop. The loop can be a for, until, or repeat command, for example.

The exit Statement

exit statements can be used to exit a shell program. You can optionally use a number after exit. If the current shell program has been called by another shell program, then the calling program can check for the code and make a decision accordingly.

Page 434

Functions

As do other programming languages, shell programs also support functions. A function is a piece of shell program that does a particular process that can be used more than once in the shell program. Writing a function will help you write shell programs without duplication of code.

Following is the format of a function definition in pdksh and bash:


func(){

   Statements

}

You can call a function as follows:


func param1 param2 param3

The parameters, param1, param2, and so on, are optional. You can also pass the parameters as a single string, for example, $@. A function can parse the parameters as if they were positional parameters passed to a shell program.

An example is a function that displays the name of the month or an error message if you pass a month number. Here is the example, in pdksh and bash:


Displaymonth() {

   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

}



displaymonth 8

The preceding program will display the following:

Month is August

Summary

In this chapter, you have learned how to write a shell program. Shell programs can be used to write programs that can be used to do simple things such as setting a number of aliases when you log on as well as complicated things such as customizing your shell environment.

Previous | Table of Contents | Next