Shell Script While Loop Examples
Can you provide me a while loop control flow statement shell script syntax and example that allows code to be executed repeatedly based on a given boolean condition?
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 <>
ksh while loop syntax:
while [[ condition ]] ; do
command1
command1
commandN
done
csh while loop syntax:
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/csh
c=1
while ( $c <= 5 )
echo "Welcome $c times"
@ c = $c + 1
end
Another example:
#!/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