Using if and case

Scripts must frequently perform different actions depending upon certain conditions. For instance, you might want a script to abort if it can't find a file the user specified, but continue processing if the file exists; or you might want to display information that's customized for each of several possible inputs. The if and case statements handle these situations.

Making Binary Choices: if

The if keyword uses a conditional expression to determine whether or not to perform some action. Its basic syntax is given here:

actions other-actions

Note Indenting lines within if, case, and looping statements to help improve the readability of scripts is common. This indentation isn't required, though, and different people have different styles of indentation.

You can omit the else clause and the other-actions if you don't want the script to do anything should the condition not be met. As an example of if in action, consider Listing 4.4. This script checks for the presence of a /proc/ide/ideO/hdb directory, which should be present if and only if a slave hard disk is present on the primary ATA controller. If that directory is present, the script displays information on the attached hard disk. If not, the script tells you there's no drive.

Listing 4.4: Script Demonstrating Use of if

echo "The drive's model is:" cat /proc/ide/ideO/hdb/model echo "Sorry, there isn't a primary slave disk."

Although the identified drive model will vary from one system to another, Listing 4.4 produces output like this:

The drive's model is: Maxtor 91000D8

Making Many Choices: case

You can use if to make binary decisions—to take one action if a condition is true and another action if the condition isn't true. You can also nest if statements inside each other—in fact, the optional elif statement is built for this situation; use it after then but before else to perform a second test. Sometimes, though, a more flexible multiple-condition test is in order. That's where case comes in. You pass a variable to this statement and then specify a series of actions to be taken depending on the value of the variable. The case syntax looks like this:

case variable in valuel) commandsl i j value2) commands2

You can include an arbitrary number of values that might match the variable. The shell executes the commands associated with the first value that matches the variable. The values you specify may include wildcards, much like wildcards in matching filenames. One of the most useful of these is a value that consists of a single asterisk (*). This value matches any variable, so when it's the final option in a case statement, it works much like the else clause of an if statement.

Listing 4.5 shows case in action. This script launches one of three programs in response to the user's input. When a user runs this script, the text specified in the first two echo lines appears on the screen and the script accepts an input letter. If that input is anything but e, I, or m, the script displays the default error message and exits; otherwise, the script launches the specified user program.

Listing 4.5: Script Demonstrating Use of case #!/bin/bash echo "Type the first letter of a program name:" echo "emacs, lynx, or mutt" read progletter case $progletter in e) emacs j j

*) echo "You didn't type e, I, or m; exiting!"

0 0

Post a comment