Using Loops

One scripting tool is the loop—a way to execute the same code again and again.

There are two broad classes of loops in shell scripts. A for loop will execute code for a fixed number of times. This number may not be known when you write the script, but it can be determined when entering the loop. By contrast, while and until loops execute while or until some condition is met, respectively.

for Loops

The basic syntax of a for loop is shown here: for variable in list commands

The variable and commands are what you might expect. The list is a variable or expression that produces a list of elements—frequently, a command or set of commands enclosed in single back-quote marks Q, a symbol that appears to the left of the 1 key on most keyboards. The commands execute once for each element in the list, and the variable takes on the value of each element in the list for each run through the loop. For instance, consider Listing 4.6, which is a variant on Listing 4.4. Instead of identifying just one drive, though, this script identifies all of a computer's ATA devices.

Listing 4.6: Script Demonstrating a for Loop

echo "The model of drive $drive is:" cat $drive/model

In use, Listing 4.6 produces output that varies from one system to another. In general, it will resemble this:

The model of drive /proc/ide/hda is: SAMSUNG SV6003H The model of drive /proc/ide/hdc is: MATSHITA CR-173

while and until Loops

The while and until loops are very similar, but they differ in their exit conditions: A while loop executes for as long as the specified condition remains true. An until loop executes for as long as the specified condition remains false (that is, until it becomes true). Therefore, the two loop types are interchangeable, except that you must reverse the nature of the conditional expression. Their syntax is also quite similar. Here it is for the while loop:

while [ condition ] do commands

Replace while with until to implement an until loop. Consider Listing 4.7, which is a variant of Listing 4.5. When run, this script repeatedly asks for an input letter. When you type an acceptable response, it launches the program in question or exits. When you quit from the target program, you see another prompt for a program to run. This script could be written using while just as well as until; but the conditional expression would use != rather than = to determine when to exit.

Listing 4.7: Script Demonstrating Use of until

do echo "Type the first letter of a program name:" echo "emacs, lynx, or mutt" echo "(Type 'q' to exit)" read progletter case $progletter in e) emacs

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

As demonstrated by Listing 4.7, it's often necessary to set the variable being tested in a while or until loop before the loop begins. Listing 4.7 sets this variable to a dummy value (t). Any value (aside from q) will do in this case; the goal is simply to keep the looping logic from terminating early or printing an error message because of a variable that hasn't been initialized.

Team LIB

0 0

Post a comment