Tuesday, January 1, 2013

WHILE LOOP in Shell Script Example

Each while loop consists of a set of commands and a condition

general syntax as follows for bash while loop:

while [ condition ]do
 command1
 command2
 commandN
done
  1. The condition is evaluated, and if the condition is true, the command1,2…N is executed.
  2. This repeats until the condition becomes false.
  3. The condition can be integer ($i < 5), file test ( -e /tmp/lock ) or string ( $ans != "" )
Example :

x=1;
while [ $x -lt 10 ] ; do
(( x++ ))
echo "x = $x"
done


OUTPUT :

-bash-3.2$ sh while_sh
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
-bash-3.2$
The while loop should be executed upto the condition failed (1<10)

No comments:

Post a Comment

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...