bash while loop syntax
while [ condition ]================================
do
command1
command2
command3
done
Example:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
=======================================
#!/bin/bash==========================================
while :
do
echo "infinite loops [ hit CTRL+C to stop]"
done
Conditional while loop exit with break statement
while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the while lopp.
fi
statements3 #While good and, no disaster-condition.
done
沒有留言:
張貼留言