Command1 ; Command2
executes the commands in sequential order. The exit code is not checked. The
following line displays the content of the le with cat and then prints its le
properties with ls regardless of their exit codes:
cat filelist.txt ; ls -l filelist.txt
Command1 && Command2
runs the right command, if the left command was successful (logical AND). The
following line displays the content of the le and prints its le properties only,
when the previous command was successful (compare it with the previous entry
in this list):
cat filelist.txt && ls -l filelist.txt
Command1 || Command2
runs the right command, when the left command has failed (logical OR). The fol-
lowing line creates only a directory in /home/wilber/bar when the creation of the
directory in /home/tux/foo has failed:
mkdir /home/tux/foo || mkdir /home/wilber/bar
funcname(){ ... }
creates a shell function. You can use the positional parameters to access its argu-
ments. The following line denes the function hello to print a short message:
hello() { echo "Hello $1"; }
You can call this function like this:
hello Tux
which prints:
Hello Tux
18.7 Working with Common Flow Constructs
To control the ow of your script, a shell has while, if, for and case constructs.
18.7.1 The if Control Command
The if command is used to check expressions. For example, the following code tests
whether the current user is Tux:
if test $USER = "tux"; then
echo "Hello Tux."
else
echo "You are not Tux."
fi
Bash and Bash Scripts 233