Unix / Linux - Shell Loop Types
1. The while loop
Syntaxwhile 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.
ExampleHere 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
2. The for loop
Syntaxfor 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.
ExampleHere is a simple example that uses the for loop to span through the given list of numbers −
#!/bin/sh for var in 0 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
3. The until loop
Syntaxuntil 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.
ExampleHere 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
4. The select loop
Syntaxselect 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.
ExampleHere 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 LoopsAll 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 LoopsIt is possible to use a while loop as part of the body of another while loop.
Syntaxwhile 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
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=0 while [ "$a" -lt 10 ] # this is loop1 do b="$a" while [ "$b" -ge 0 ] # 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
Comments
Post a Comment