Using Bash Control Structures

In Bash scripts, the control structures — such as if, case, for, and while — depend on the exit status of a command to decide what to do next. When any command executes, it returns an exit status: a numeric value that indicates whether or not the command has succeeded. By convention, an exit status of zero means the command has succeeded. (Yes, you read it right: zero indicates success.) A nonzero exit status indicates that something has gone wrong with the command.

As an example of using control structures, consider the following script, which makes a backup copy of a file before opening it with the vi text editor:

else echo "Failed to create backup copy"

This script illustrates the syntax of the if-then-else structure and shows how the exit status of the cp command is used by the if structure to determine the next action. If cp returns zero, the script invokes vi to edit the file; otherwise, the script displays a message and exits. By the way, the script names the backup file the same as that of the original, except for a number sign (#) added at the beginning of the filename.

insider Don't forget the final fi that terminates the if structure. Forgetting fi is a common nmglit source of errors in Bash scripts.

Bash includes the test command to enable you to evaluate any expression and to use the expression's value as the exit status of the command. Suppose that you want a script that enables you to edit a file only if it exists. Using test, you might write such a script as follows:

else echo "No such file"

A shorter form of the test command omits test and places the test command's options in square brackets ([...]). Using this notation, you can write the script that enables you to edit only existing files, as follows:

#!/bin/sh if [ -f "$1" ] then vi "$1"

else echo "No such file"

The left square bracket ([) is in fact a symbolic link to / usr/bin/test. You can confirm this fact by typing Is -l /usr/bin/[.

Another common control structure is the for loop. The following script adds the numbers 1 through 10:

for i in 1 2 3 4 5 6 7 8 9 10 do sum="expr $sum + $i'

This example also illustrates the use of the expr command to evaluate an expression.

The case statement is used to execute a group of commands based on the value of a variable. For example, consider the following script:

#!/bin/sh echo -n "What should I do -- (Y)es/(N)o/(C)ontinue? [Y] " read answer case $answer in y|Y|"") echo "YES"


echo "NO"

echo "UNKNOWN"


Save this in a file named confirm, and type chmod +x confirm to make it executable. Then, try it out like this:


What should I do -- (Y)es/(N)o/(C)ontinue? [Y] c CONTINUE

The script displays a prompt and reads the input you type. Your input is stored in a variable named answer. Then the case statement executes a block of code based on the value of the answer variable. For example, when I type c, the following block of commands is executed:

echo "CONTINUE" The echo command causes the script to display CONTINUE.

From this example, you can see that the general syntax of the case command is as follows:

case $variable in valuel | value2) commandl command2





Essentially, the case command begins with the word case and ends with esac. Separate blocks of code are enclosed between the values of the variable, followed by a right parenthesis and terminated by a pair of semicolons (;;).

insider Don't forget the final esac that terminates the case structure. Forgetting esac is a insight common source of errors in Bash scripts.

Was this article helpful?

0 0
Make Money Writing

Make Money Writing

This Report Will Show You How To Make Money By Providing Writing Services To Other Internet Marketers. Learn how to make money by writing the right way. Grab your copy of this report now and learn. Why writing is a great way to earn money. How to compete with cheap writers, even if you charge a lot more money.

Get My Free Ebook

Post a comment