Each while loop consists of a set of commands and a condition. The 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 != "" )
while [[ condition ]] ; docsh while loop syntax:
command1
command1
commandN
done
while ( condition )
commands
end
BASH while Loop Example
#!/bin/bash
c=1
while [ $c -le 5 ]
do
echo "Welcone $c times"
(( c++ ))
done
KSH while loop Example
#!/bin/ksh
c=1
while [[ $c -le 5 ]]; do
echo "Welcome $c times"
(( c++ ))
done
CSH while loop Example
#!/bin/cshAnother example:
c=1
while ( $c <= 5 )
echo "Welcome $c times"
@ c = $c + 1
end
#!/bin/csh
set yname="foo"
while ( $yname != "" )
echo -n "Enter your name : "
set yname = $<
if ( $yname != "" ) then
echo "Hi, $yname"
endif
end
No comments:
Post a Comment