Shell scripts may contain simple if-then-else structures like
Fortran or C:
if [ test ] then command else Else is optional. command fi if always finishes with fi.
The test may deal with file characteristics or numerical/string comparisons. Although the left bracket here appears to be part of the structure, it is actually another name for the Unix test command (located in /bin/[). Since [ is the name of a file, there must be spaces before and after it as well as before the closing bracket. To use the Korn shell's built-in test command, replace the single pair of square brackets with [[ ]] a pair of double brackets.
The next example is a script that takes one argument, a file name. If
the file exists, it is used as the input for the user's program. If
the number of arguments is incorrect or if the input file does not
exist, the script prints out a message and exits:
#!/bin/sh if [ $# -ne 1 ] then echo "This script needs one argument." exit -1 fi input="$1" if [ ! -f "$input" ] then echo "Input file does not exist." exit -1 else echo "Running program bigmat with input $input." bigmat < $input fi exit