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


The for and select Loops

Unlike the while loop, which exits when a certain condition is false, both the for and select loops operate on lists of items. The for loop repeats a set of commands for every item in a list, whereas the select loop enables the user to select an item from a list.

The for Loop

The basic syntax is

for name in word1 word2 ... wordN
do
    list
done

Here name is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable name is set to the next word in the list of words, word1 to wordN. The first time, name is set to word1; the second time, it’s set to word2; and so on.

This means that the number of times a for loop executes depends on the number of words that are specified. For example, if the following words were specified to a for loop

there comes a time

the loop would execute four times.

In each iteration of the for loop, the commands specified in list are executed.

You can also write the entire loop on a single line as follows:

for name in word1 word2 ... wordN ; do list ; done

If list and the number of words are short, the single line form is often chosen; otherwise, the multiple-line form is preferred.

A simple for loop example is

for i in 0 1 2 3 4 5 6 7 8 9
do
    echo $i
done

This loop counts to nine as follows:

0
1
2
3
4
5
6
7
8
9

Note that although the output is identical to the while loop, the for loop does something altogether different. In each iteration, $i is set to the next item in the list. When the list is finished, the loop exits.

In this example I chose the list to be the numbers from 0 to 9. In the while loop, the next number to display was being computed, and it was not part of a predetermined list.

If you change the list slightly, notice how the output changes:

for i in 0 1 2 4 3 5 8 7 9
do
    echo $i
done
0
1
2
4
3
5
8
7
9

Manipulating a Set of Files

Say that you need to copy a bunch of files from one directory to another and change the permissions on the copy. You could do this by copying each file and changing the permissions manually.

A better solution would be to determine the commands you need to execute in order to copy a file and change its permissions and then have the computer do this for every file you were interested in. In fact this is one of the most common uses of the for loop—iterating over a set of file names and performing some operations on those files.

The procedure to do this follows:

1.  Create a for loop with a variable named file or FILE. Other favored names include i, j, and k. Usually the name of the variable is singular.
2.  Create a list of files to manipulate. This is frequently accomplished using the filename substitution technique I discussed in Chapter 8, “Substitution.”
3.  Manipulate the files in the body of the loop.

An example of this is the following for loop:

for FILE in $HOME/.bash*
do
    cp $FILE ${HOME}/public_html
    chmod a+r ${HOME}/public_html/${FILE}
done

In this loop I use filename substitution to obtain a list of files in my home directory that start with .bash*. In the body of the loop, I copy each of these files to my public_html directory, and then I make them readable by everyone. This way people stopping by my Web page can see the scripts.

Notice that you are using the name FILE for the variable. This is because each time you are dealing with a single file from a list of files. The rationale behind making the for loop’s variable singular, such as FILE instead of FILES, is that you are dealing with only one item from a set of items each time the loop executes.

The select Loop

The select loop provides an easy way to create a numbered menu from which users can select options. It is useful when you need to ask the user to choose one or more items from a list of choices.

This loop was introduced in ksh and has been adapted into bash. It is not available in sh.

The basic syntax of the select loop is

select name in word1 word2 ... wordN
do
    list
done

Here name is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). The set of commands to execute after the user has made a selection is specified by list.

The execution process for a select loop is as follows:

1.  Each item in list1 is displayed along with a number.
2.  A prompt, usually #?, is displayed.
3.  When the user enters a value, $REPLY is set to that value.
4.  If $REPLY contains a number of a displayed item, the variable specified by name is set to the item in list1 that was selected. Otherwise, the items in list1 are displayed again.
5.  When a valid selection is made, list2 executes.
6.  If list2 does not exit from the select loop using one of the loop control mechanisms such as break, the process starts over at step 1.

If the user enters more than one valid value, $REPLY contains all the user’s choices. In this case, the variable specified by name is not set.


Previous Table of Contents Next