Loop or iteration is repeats certain instructions again and again until the given condition is true. The block of code under the loop is executed repeatedly until the condition is true. There are three kinds of loop: while, until and for. If you are unfamiliar with the shell scripts, first look our previous tutorial on shell script and come here. The while and until loop is just opposite to each other. There are two types of for loop: for in loop and simple for loop. Let’s get started with the while loop.
While Loop:
The general syntax for while loop is:
1 2 3 4 | while [ condition ] OR while test condition do commands done |
The block of code between do and done is executed in While Loop until the condition is true. We are going to print the numbers from 0 to 10 using the while loop.
1 2 3 4 5 6 | i=o while [ $i -le 10 ] do echo "$i" i=`expr $i + 1` done |
The program starts from top. As you can see, first of all the number “0″ is assigned to variable “i” which satisfies the condition i.e 0<=10[Find more expressions here] so our program enters loop and block of code between do and done is executed. The i which is 0 now is printed and the value of i is incremented by 1. See, expr command handles the mathematical expressions in shell script. We will see more about expr command in coming tutorials. Now, the value of i is 1 which is also true thus 1 is printed and again the value of i is incremented by 1. This goes on until the value of i is 11. When the value of i is 11 the condition is not satisfied the program ends. Again, let me clear this new concept:
1 | i=`expr $i + 1` |
The expr commands tells the shell script that we are going to perform the mathematical expression and the backtick(`) tells the shell script to assign the result to the variable instead of printing it out.
The output is:
Until Loop:
The while loop and until loop is exactly opposite. While Loop is executed if the condition is true whereas until loop gets executed if the condition is false. Let’s see an example:
1 2 3 4 5 6 7 8 | x=angel name=dangel until [ "$name" = "$x" ] do echo "Your guess: c" read name done echo Right Guess!!! |
Let’s look from the top. The x variable and name variable holds the value “angel” and “dangel”. Definitely, those two are not the same i.e the condition is false and we enter into the loop. Then the program asks the user to guess the name and the name the user inputs is stored in the variable name. If the user inputs the angel “name” variable will be equal to the variable “x”, thus the program will say Right guess!!! and the program will exit. Otherwise, the program continues until the user enters the false name. See the output:
For Loop:
There are two kinds of for loop: for in and for loop. The syntax for “for in” loop is:
for loop-index in argument-list
do
commands
done
Let us see an example of for in loop:
1 2 3 4 5 | for actor in brad christain clint heath leonardo do echo $actor done echo DOne!!!! |
This is probably the most simplest shell script loop out there. We are going to print the name of my five super Hollywood actors using for in loop. There are 5 arguments i.e actor name in this loop. The loop will go on printing the actor until the argument in complete. When the argument is completed DOne!!!! is printed and the program exits. See the output:
In the for loop, instead of placing the argument in the shell script we type the argument while running the program. See the example:
1 2 3 4 5 | for actor do echo $actor done echo DOne!!!! |
As you can see, there is no argument in this for Loop. But, while running the program we will give the argument as:
As you can see, the three arguments: natalie, megan and kate is given while running the program and printed in the program.






Recent Comment