Previous Table of Contents Next


if-else-else if-endif

The C shell has built-in constructs that you can use to test conditions. Refer to the section "Test and C Shell Built-in Test" in Chapter 17 for C shell test command options.

The C shell fragment shown next tests whether a user has entered one or more command-line arguments following the name of the script:

#!/bin/csh -f

if ($#argv == Ø) then
    echo Usage: $Ø name requires one command-line argument
    exit 1
endif


NOTE:  The then statement for the C shell, if present, must be positioned at the end of the if line.

If you want the script to perform additional actions if the first conditional test fails, use the else clause, as shown in the following C shell fragment:

#!/bin/csh -f

if ($#argv == Ø) then
    echo Usage: $Ø requires command-line arguments
    exit 1
else
    echo "Thank you for entering arguments"
    echo "You entered $# arguments"
endif

You can use the else if conditional to test for additional conditions within the if statement. The else if clause is performed only if the previous if or else if fails (is false). Each else if statement lists another command to be tested. The following C shell example has one else if and one else statement:

#!/bin/csh -f
#
# Time of day greetings
#
set d=`date +%H`
set hour = $d[4]

if ($hour < 12) then
    echo "Good Morning\!"
else if ($hour < 17) then
    echo "Good Afternoon\!"
else
    echo "Good Night\!"
endif

Nested if Constructs

If statements can contain additional if statements. When you write a script that contains nested if statements, be sure to indent the statements to make it easier to follow the logic of the statements and to check that you have included all the required elements.

Here is the syntax for nesting if statements in the Bourne and Korn shells:

if command
then
    if command
    then
        command
        ...
    else
        command
        ...
    fi
elif command
then
    command
    ...
else
    command
    ...
fi

The syntax for nesting if statements in the C shell follows:

if (expression) then
    if (expression) then
        if (expression) then
            command
            ...
        else
            command
            ...
        endif
    else if (expression) then
        command
        ...
    else
        command
        ...
    endif
endif

Multi-Branching

You may want to take several different types of action depending on the value of a variable or a parameter. Although you can use conditional testing to test each value and take action, you can more easily perform such tests using the case statement or the C shell switch statement, as shown in Table 16-13.

Table 16-13 Switch and Case Syntax

Bourne and Korn Shells C Shell
case value in switch (value)
pattern1) case pattern:
command commands
command ;; breaksw
pattern2) default:
command ;; commands
*) breaksw
default action ;; endsw
esac

The value of the variable is successively compared against patterns until a match is found. The commands immediately following the matching pattern are executed until finding ;; (for the Bourne and Korn shells) or breaksw (for the C shell). The last test *) (Bourne or Korn shell) or default: (C shell) is a default action. If no other values match, the shell executes the default action. In many scripts, the default action displays an error message and exits from the shell.

Case statements can be especially helpful for processing parameters to a function.

The following Bourne script example sets the terminal type:

#!/bin/sh
#
# Set the terminal type
#
case $TERM in
    tvi???)
        echo $TERM
        echo Probably the system console ;;
    vt[12][02]0)
        echo A VT terminal ;;
    Wyse40 | Wyse75)
        echo A Wyse terminal ;;
    sun)
        echo Aha!! a workstation ;;
    *)
        echo surprise! it's a $TERM
esac

The following example shows an interactive C shell script to set the terminal type:

#!/bin/csh -f
#
# Set the terminal type
#
echo "Do you want to see the present setting? \c"
set input = $<
switch ("$input")
    case [Yy]*:
        echo "term =" $TERM
        breaksw
endsw
echo "Terminal type? \c"
set term = $<
switch ($term)
    case tvi95Ø:
    case vt1ØØ:
    case sun:
        setenv TERM $term
        breaksw
    default:
        echo "I don't know that one."
        breaksw
endsw


Previous Table of Contents Next