Loops in shell scripts
|
While and Until Loops
A while loop runs as long as it meets the specified condition in the loop header. Of course, you could control the process independently of the header using continue , break or exit . The syntax is as follows:
while [CONDITION]
This type of loop, unlike a for loop, doesn't do any processing by itself.
Listing 4 shows a minimal example where the loop executes for as long as the $k variable is less than 5. It also shows you that you have to increment k separately in between do and done .
Listing 4
while1.sh
01 #! /bin/sh 02 k=0 03 while [ $k -lt 5 ]; 04 do 05 k=$(echo $k + 1 | bc) 06 echo $k 07 done
The until loop runs until the condition in the header becomes true (i.e., the condition has to be false for the loop to run), otherwise the rules are the same as for the while loop. Listing 5 shows a sample script.
Listing 5
until1.sh
01 #! /bin/sh 02 k=0 03 until [ $k -eq 5 ]; 04 do 05 k=$(echo $k + 1 | bc) 06 echo $k 07 done
The while and until loops are followed by a condition in the loop header. If you want an endless loop controlled from within the loop body, set the while loop to true or the until loop to false . The two statements set the conditions and return true as 0 or false as 1.
Endless loops are used, for example, in menu control or monitoring tasks. Listing 6 shows an application of both types of loops. Note the use of break and exit to quit the loops.
Listing 6
infinite.sh
01 #! /bin/sh 02 k=0 03 echo "while-loop" 04 while true; 05 do 06 k=$(echo $k + 1 | bc) 07 echo $k 08 09 # End loop with a >> break << 10 11 if [ $k -eq 5 ]; 12 then 13 break 14 fi 15 done 16 17 k=0 18 echo "until-loop" 19 until false 20 do 21 k=$(echo $k + 1 | bc) 22 echo $k 23 24 # End loop and program with >> exit << 25 26 if [ $k -eq 5 ]; 27 then 28 exit 29 fi 30 done
Listing 7
Exiting with Code 3
01 #! /bin/sh 02 echo "Applying an exit code value of 3" 03 exit 3
With continue , the loop is interrupted without executing any remaining commands and then restarted in the next cycle. Used inside nested loops, continue can even switch through several nested levels if you indicate the number of levels. The level number of the current loop begins with 1 and extends to the outside loops, the outer loop being the highest number.
A continue without an argument refers to the currently executing loop only. (An example of multiple nested loop levels, with the continue near the bottom of the script, is shown later in Listing 8).
Listing 8
exiting.sh
01 #! /bin/sh 02 a=0 03 b=0 04 c=0 05 while true; 06 do 07 08 if [ $a -eq 8 ]; 09 then 10 echo "That's it!" 11 exit 12 fi 13 14 a=$(echo $a + 1 | bc) 15 echo "A: $a" 16 if [ $a -eq 5 ]; 17 then 18 while true; 19 do 20 b=$(echo $b + 1 | bc) 21 echo "B: $b" 22 if [ $b -eq 5 ]; 23 then 24 while true; 25 do 26 c=$(echo $c + 1 | bc) 27 echo "C: $c" 28 if [ $c -eq 5 ]; 29 then 30 continue 3 31 fi 32 done 33 fi 34 done 35 fi 36 done
You break off a loop using break . Here, too, you can jump over nested loop levels by indicating the level number. Listing 6 shows an example of using a break .
If the script is to exit after a condition is met, use exit and the loop will end and no further commands executed. Listings 6 and 7 show some applications of a script abruptly ending.
You can give the command its own exit code in the script to be used at another location. The short example in Listing 7 shows how this works. You can ask for the exit code immediately after the command by using echo $? .
Sample Loop Body
The sample script in Listing 8 will count three times within nested loops. The end of the script is triggered by a condition in the "main" loop (exit ). In the first level, the loop is interrupted and redirected to the main loop with a continue . The main loop then takes over again and continues counting until it meets an exit condition. Figure 3 shows the result of the script.
« Previous 1 2 3 4 Next »
Buy this article as PDF
Pages: 5
(incl. VAT)