<tt>if</tt> statement



next up previous contents
Next: case: Selections Up: Control Structures Previous: until loop

if statement

What would happen if no parameters had been passed to the script Here is how one can verify that there is at least one parameter in the list:

    #!/bin/sh
    if test $# -eq 0
    then
        echo there must be at least one parameter on the command line
    else
        until test $# -eq 0
        do
            echo parameter $1
            shift
        done
    fi
    exit 0
Another way to write this same program is
    #!/bin/sh
    if test $# -eq 0
    then
        echo there must be at least one parameter on the command line
        exit 1
    fi
    until test $# -eq 0
    do
        echo parameter $1
        shift
    done
    exit 0
Note that the format of the if statement is
if comparison
then
. . .
else
. . .
fi
The else is optional, or if followed by another if, could be replaced by elif. The statement is ended by fi (``if" spelled backwards).



Super-User
Fri Feb 17 15:55:40 EST 1995