The if conditional
An if conditional runs a sequence of
commands if a particular condition is met. It has the form:
if condition
then commands
fi
The end of the commands is indicated by fi (which
is "if" backward). For example, you could have: if test -d $1
then ls $1
fi
This tests to see if the string associated with the
first positional parameter, $1, is the name of a
directory. If so, it runs an ls command
to display the contents of the directory.Any number of commands may come between the then and
the fi that ends the control structure.
For example, you might have written:
if
test -d $1
then
echo "$1 is a directory"
ls $1
fi
This example also shows that the commands do not have
to begin on the same line as then, and the
condition being tested does not have to begin on the same line as if.
The condition and the commands are indented to make them stand out
more clearly. This is a good way to make your shell scripts easier
to read.Another form of the if conditional is:
if condition
then commands
else commands
fi
If the condition is true, the commands after the then are
run; otherwise, the commands after the else are
run. For example, suppose you know that the string associated with
the variable pathname is the name of either
a directory or a file. Then you could write: if
test -d $pathname
then
echo "$pathname is a directory"
ls $pathname
else
echo "$pathname is a file"
cat $pathname
fi
If the value of pathname is
the name of a file, this shell script uses echo to
display an appropriate message, and then uses cat to
display the contents of the file.The final form of the if control structure
is:
if condition1
then commands1
elif condition2
then commands2
elif condition3
then commands3
...
else commands
fi
elif is short for "else if"
In this example, if condition1 is true, commands1 are
run; otherwise, the shell goes on to check condition2.
If that is true, commands2 are run; otherwise,
the shell goes on to check condition3 and
so on. If none of the test conditions are true, the commands after
the else are run. Here is an example of
how this can be used: if test ! "$1"
then
echo "no positional parameters"
elif test -d $1
then
echo "$1 is a directory"
ls $1
elif test -f $1
then
echo "$1 is a file"
cat $1
else
echo "$1 is just a string"
fi
The test after the if determines
if the value of the first positional parameter, $1,
is an empty string. If so, there are no positional parameters, and
the shell script uses echo to display an
appropriate message; otherwise, the script checks to see if the parameter
is a directory name; if so, the contents of the directory are listed
with ls (after an appropriate message).
If that does not work, the script checks to see if the parameter is
a file name; if so, the contents of the file are listed with cat (after
an appropriate message). Finally, if none of the previous tests work,
the parameter is assumed to be an arbitrary string, and the script
displays a message to this effect.You could put that script into a file named listit and run
commands of the form:
listit name
to
list the contents of name in a useful form.