Loops allow us to take a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks.
There are 3 basic loop structures in Bash scripting which we'll look at below. There are also a few statements which we can use to control the loops operation.
while loop
One of the easiest loops to work with is while loops. They say, while an expression is true, keep executing these lines of code.
while [ test condition ]
do
#statements
done
Example: print all odd numbers less than 50
i=1
while [ $i -le 50 ]
do
echo $i
i=`expr $i + 2`
done
until loop
The until loop is fairly similar to the while loop. The difference is that it will execute the commands within it until the test becomes true.
Syntax
until [ test condition ]do
#statements
done
Example: print all odd numbers less than 50
i=1
until [ $i -ge 50 ]
do
echo $i
i=`expr $i + 2`
done
for loop
The for loop is a little bit different to the previous two loops. What it does is say for each of the items in a given list, perform the given set of commands. It has the following syntax.
do
#statements
done
The for loop will take each item in the list (in order, one after the other), assign that item as the value of the variable var, execute the commands between do and done then go back to the top, grab the next item in the list and repeat over.
Example: read a string and print the words
echo "enter a string"
read s
for w in $s
do
echo $w
done
We can also specify the range(bash). This will print 1 to 5.
for i in {1..5}
do
echo $i
done
It is also possible to specify a value to increase or decrease by each time. You do this by adding another two dots ( .. ) and the value to step by.This will print all odd numbers less than 10.
for i in {1..10..2} do
echo $i
done
The following for loop will print words from a file f.
for w in `cat f`
do
echo $w
done
C like syntax can also be used in bash. The following loop will print 1-10
for((i=1;i<=10;i++))
echo $i
done
Controlling loops break and continue
Most of the time your loops are going to through in a smooth and orderly manner. Sometimes however we may need to intervene and alter their running slightly. There are two statements we may issue to do this.
The break statement tells Bash to leave the loop straight away. It may be that there is a normal situation that should cause the loop to end but there are also exceptional situations in which it should end as well. For instance, maybe we are copying files but if the free disk space get's below a certain level we should stop copying.
The continue statement tells Bash to stop running through this iteration of the loop and begin the next iteration. Sometimes there are circumstances that stop us from going any further. For instance, maybe we are using the loop to process a series of files but if we happen upon a file which we don't have the read permission for we should not try to process it.
Example: The following program will check whether the given number is prime or not.
echo "Enter a number"
read n
prime=true
for((i=2;i<=n/2;i++))
do
r=`expr $n % $i`
if [ $r -eq 0 ]
then
prime=false
break
fi
done
if [ $prime == true ]
then
echo "Prime Number"
else
echo "Not a prime Number"
fi
Comments
Post a Comment