Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 10
Flow Control

The order in which commands execute in a shell script is called the flow of the script. In the scripts that you have looked at so far, the flow is always the same because the same set of commands executes every time.

In most scripts, you need to change the commands that execute depending on some condition provided by the user or detected by the script itself. When you change the commands that execute based on a condition, you change the flow of the script. For this reason, the commands discussed in this chapter are called flow control commands. You might also see them referred to as conditional flow control commands because they change the flow of a script based on some condition.

Two powerful flow control mechanics are available in the shell:

  The if statement
  The case statement

The if statement is normally used for the conditional execution of commands, whereas the case statement enables any number of command sequences to be executed depending on which one of several patterns matches a variable first.

In this chapter, I explain how to use flow control in your shell scripts.

The if Statement

The if statement performs actions depending on whether a given condition is true or false. Because the return code of a command indicates true (return code is zero) or false (return code is nonzero), one of the most common uses of the if statement is in error checking. An example of this is covered shortly.

The basic if statement syntax follows:

if list1
then
   list2
elif list3
then
   list4
else
   list5
fi

Both the elif and the else statements are optional. If you have an elif statement, you don’t need an else statement and vice versa. An if statement can be written with any number of elif statements.

The flow control for the general if statement follows:

1.  list1 is evaluated.
2.  If the exit code of list1 is 0, indicating a true condition, list2 is evaluated and the if statement exits.
3.  Otherwise, list3 is executed and its exit code is checked.
4.  If list3 returns 0, list4 executes and the if statement exits.
5.  If list3 does not return 0, list5 executes.

Because the shell considers an if statement to be a list, you can write it all in one line as follows:

if list1 ; then list2 ; elif list3 ; then list4 ; else list5 ; fi ;

Usually this form is used only for short if statements.

An if Statement Example

A simple use of the if statement is

if uuencode koala.gif koala.gif > koala.uu ; then
   echo "Encoded koala.gif to koala.uu"
else
   echo "Error encoding koala.gif"
fi

Look at the flow of control through this statement:

1.  First, the command
uuencode koala.gif koala.gif > koala.uu

executes. This command is list1 in the general statement.
2.  If this command is successful, the command
echo "Encoded koala.gif to koala.uu"

executes and the if statement exits. This command is list2 in the general statement.
3.  Otherwise the command
echo "Error encoding koala.gif"

executes, and the if statement exits. This command is list5 in the general statement.

You might have noticed in this example that both the if and then statements appear on the same line. Most shell programmers prefer to write if statements this way in order to make the if statement more concise. The majority of shell programmers claim that this form looks better.


Previous Table of Contents Next