A loop is a powerful programming tool that enables you to execute a set of commands repeatedly. In this chapter, we will examine the following types of loops available to shell programmers −
- The while loop
The while loop enables you to execute a set of commands repeatedly until some condition occurs. It is usually used when you need to manipulate the value of a variable repeatedly.
Syntax
while command do Statement(s) to be executed if command is true done
Here the Shell command is evaluated. If the resulting value is true, given statement(s) are executed. If command is false then no statement will be executed and the program will jump to the next line after the done statement.
Example
Here is a simple example that uses the while loop to display the numbers zero to nine −
#!/bin/sh a=0 while [ $a -lt 10 ] do echo $a a=`expr $a + 1` done
Upon execution, you will receive the following result −
0 1 2 3 4 5 6 7 8 9
Each time this loop executes, the variable a is checked to see whether it has a value that is less than 10. If the value of a is less than 10, this test condition has an exit status of 0. In this case, the current value of a is displayed and later a is incremented by 1.
- The for loop
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax
for var in word1 word2 ... wordN do Statement(s) to be executed for every word. done
Here var 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 var is set to the next word in the list of words, word1 to wordN.
Example
Here is a simple example that uses the for loop to span through the given list of numbers −
#!/bin/sh for var in 1 2 3 4 5 6 7 8 9 do echo $var done
Upon execution, you will receive the following result −
0 1 2 3 4 5 6 7 8 9
Following is the example to display all the files starting with .bash and available in your home. We will execute this script from my root −
#!/bin/sh for FILE in $HOME/.bash* do echo $FILE done
The above script will produce the following result −
/root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc
- The until loop
The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.
Syntax
until command do Statement(s) to be executed until command is true done
Here the Shell command is evaluated. If the resulting value is false, given statement(s) are executed. If the command is true then no statement will be executed and the program jumps to the next line after the done statement.
Example
Here is a simple example that uses the until loop to display the numbers zero to nine −
#!/bin/sh a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done
Upon execution, you will receive the following result −
0 1 2 3 4 5 6 7 8 9
- 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.
Syntax
select var in word1 word2 ... wordN do Statement(s) to be executed for every word. done
Here var 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 var is set to the next word in the list of words, word1 to wordN.
For every selection, a set of commands will be executed within the loop. This loop was introduced in ksh and has been adapted into bash. It is not available in sh.
Example
Here is a simple example to let the user select a drink of choice −
#!/bin/ksh select DRINK in tea cofee water juice appe all none do case $DRINK in tea|cofee|water|all) echo "Go to canteen" ;; juice|appe) echo "Available at home" ;; none) break ;; *) echo "ERROR: Invalid selection" ;; esac done
The menu presented by the select loop looks like the following −
$./test.sh 1) tea 2) cofee 3) water 4) juice 5) appe 6) all 7) none #? juice Available at home #? none $
You can change the prompt displayed by the select loop by altering the variable PS3 as follows −
$PS3 = "Please make a selection => " ; export PS3 $./test.sh 1) tea 2) cofee 3) water 4) juice 5) appe 6) all 7) none Please make a selection => juice Available at home Please make a selection => none $
You will use different loops based on the situation. For example, the while loop executes the given commands until the given condition remains true; the until loop executes until a given condition becomes true.
Once you have good programming practice you will gain the expertise and thereby, start using appropriate loop based on the situation. Here, while and for loops are available in most of the other programming languages like C, C++ and PERL, etc.
Nesting Loops
All the loops support nesting concept which means you can put one loop inside another similar one or different loops. This nesting can go up to unlimited number of times based on your requirement.
Here is an example of nesting while loop. The other loops can be nested based on the programming requirement in a similar way −
Nesting while Loops
It is possible to use a while loop as part of the body of another while loop.
Syntax
while command1 ; # this is loop1, the outer loop do Statement(s) to be executed if command1 is true
while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done
Statement(s) to be executed if command1 is true
done
Example
Here is a simple example of loop nesting. Let’s add another countdown loop inside the loop that you used to count to nine −
#!/bin/sh a= while [ "$a" -lt 10 ] # this is loop1 do b="$a" while [ "$b" -ge ] # this is loop2 do echo -n "$b " b=`expr $b - 1` done echo a=`expr $a + 1` done
This will produce the following result. It is important to note how echo -n works here. Here -n option lets echo avoid printing a new line character.
0 1 0 2 1 0 3 2 1 0 4 3 2 1 0 5 4 3 2 1 0 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0