Previous | Table of Contents | Next |
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 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, its 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 loopiterating over a set of file names and performing some operations on those files.
The procedure to do this follows:
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 loops 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 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:
If the user enters more than one valid value, $REPLY contains all the users choices. In this case, the variable specified by name is not set.
Previous | Table of Contents | Next |