Looping Statement in Linux Programming

loop is a bash programming language statement which allows code to be repeatedly executed. A loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.

Until Loop

The until loop is useful when you need to execute a set of commands until a condition is true.

Syntax


until [ condition ]
do
   statement 1
   statement 2
done
							

Example



i=1
while [ !$i -lt 10 ]
do
    echo $i
	i=`expr $i + 1`
done
							
							

While Loop

The while loop enables you to execute set of commands repeatedly until some condition occurs.it usually used when you need to manipulate the value of a variable repeatedly.

Syntax


while [ condition ]
do
   statement 1
   statement 2
done
							

Example



i = 1
while [ $i -le 10 ]
do
    echo $i
	i = `expr $i + 1`
done
	
							
							

For Loop

a for loop is a bash programming language statement which allows code to be repeatedly executed

Syntax


for [ variable_name ] in ...
do
    statement 1
	statement 2
	statement n
done
							

Example



for no in {1..10}
do
    echo $no
done
							
Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit