Linux Topic
   >  Shell Scripts
   >  Using Comments
   >  Passing Arguments to Scripts
   >  Using Conditional Statements
   >  Using Loop Statements
   >  Reading and Writing Files
   >  Script Permissions
   >  Cron and Scheduling
   >  A Scripting Example
   >  Other Scripting Languages

 

Looping Constructs in Shell Script

What is a Loop?

When coding, there are often times when you wish to execute the same code several times. Rather than copying the code multiple times, programming languages provide mechanisms to iterate around a block of code multiple times - normally until some condition is fulfilled. The BASH Shell Scripting language provides the following constructs for repeating a block of (-one or more) commands:

  • The for loop
  • The while loop
  • The until loop

Using the FOR Loop

The general syntax of the for loop is given below:

for <variable> in <list>
do
   <statement block>
done

The <variable> can be any shell variable and will be used by the loop to hold the current value of those given in the <list>: for each iteration around the loop, the value of the <list> will change to the next sequential value given in the <list> (-moving left to right) - and whatever commands are in the <statement block> will be executed - until all values given in the <list> have been exhausted - at which point the loop will exit.

That was a bit of a long sentence to comprehend, so let's look at a simple example:

for myVar in 1 2 3 5 9
do
   echo "Current value of myVar is $myVar"
done

If you run the above code, you'll get the following output:

$ ./myScript.sh
Current value of myVar is 1
Current value of myVar is 2
Current value of myVar is 3
Current value of myVar is 5
Current value of myVar is 9

Hopefully, you can now see how this works: the code block (-here it's a single echo statement) is actually executed five times (-once for each of the values 1,2,3,5,9) and you can also see that the myVar variable is assigned the current value in the list each time it is executed.

If you require more detail on the for loop, take a look at www.cyberciti.biz/faq/bash-for-loop/ (-or type "bash for loop" into your favourite search engine).


Using the WHILE Loop

The general syntax of the while loop is given below:

while <condition>
do
   <statement block>
done

The <condition> can be any shell condition (-i.e. anything that evaluates to a false (0) or true (non-zero) value). Whilst the <condition> remains true, the <statement block> will be executed - until the <condition> is false (zero).

Let's look at a simple example.

Note: that the read statement reads input from the keyboard into the myVar variable:

myVar=""

while [ "$myVar" != "quit" ]
do
   echo "Current value of myVar is $myVar"
   read myVar
done

If you run the above code, you'll get the following output:

$ ./myScript.sh
Current value of myVar is 
qww
Current value of myVar is qww
ffg
Current value of myVar is ffg
quit

Hopefully, you can now see how this works: the code block (-here it's two statements: one echo and one read statement) is executed until the string "quit" is input. Note that, if we change the initial value of myVar to "quit"..

myVar="quit"

while [ "$myVar" != "quit" ]
do
   echo "Current value of myVar is $myVar"
   read myVar
done

.. then the code block does not execute at all:

$ ./myScript.sh
$

If you require more detail on the while loop, take a look at www.cyberciti.biz/faq/bash-while-loop/ (-or type "bash while loop" into your favourite search engine).


Using the UNTIL Loop

The general syntax of the until loop in BASH scripting is very similar to that of the while loop, except that the test is reversed:

until <condition>
do
   <statement block>
done

The <condition> can be any shell condition (-i.e. anything that evaluates to a false (0) or true (non-zero) value). The <statement block> will be executed whilst the <condition> remains false; when the <condition> becomes true (non-zero), the loop exits.

Here's the example from the whileloop section, reworked to use the until loop:

myVar=""

until [ "$myVar" == "quit" ]
do
   echo "Current value of myVar is $myVar"
   read myVar
done

If you run the above code, you'll get the following output (-exactly the same as the until loop in the previous section:

$ ./myScript.sh
Current value of myVar is 
qww
Current value of myVar is qww
ffg
Current value of myVar is ffg
quit

If you require more detail on the until loop, take a look at tldp.org/LDP/abs/html/loops1.html/ (-or type "bash until loop" into your favourite search engine).


HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button