Each while loop consists of a set of commands and a condition
general syntax as follows for bash while loop:
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)
general syntax as follows for bash while loop:
while [ condition ]do command1 command2 commandN done
- The condition is evaluated, and if the condition is true, the command1,2…N is executed.
- This repeats until the condition becomes false.
- The condition can be integer ($i < 5), file test ( -e /tmp/lock ) or string ( $ans != "" )
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