while循環格式
while expression
do
command
......
done
第一種:計數器控制的循環
counter=1
while expression
do
command
......
let command to operate counter
command
......
done
例:
#!/bin/bash
a=1
while (( "$a"<=4 ))
do
echo "$a"
let "a++"
done
第二種:結束標記控制的循環
read variable
while [[ "$variable != sentinel ]]
do
read variable
done
例:
#!/bin/bash
echo "input the num(1-10)"
read num
while [[ "$num !=4 ]]
do
if [ "$num" -lt 4 ]
then
echo "Too smanll.Try again"
read num
elif [ "$num" -gt 4 ]
then
echo "to hgh.Tyr again"
read num
else
exit 0
fi
done
echo "you are right"
第三種:標誌控制的循環
signal=0
while (( signal != 1 ))
do
...
if expression
then
signal=1
fi
...
done
例:
#!/bin/bash
echo "Please input the num"
read num
signal=0
while [[ "$signal" != 1 ]]
do
if [ "$num" -lt 4 ]
then
echo "Too small,Tye again"
read num
else [ "$num" -gt 4 ]
then
echo "To high.Try again"
read num
else
signal=1
echo "you are right"
fi
done
第四種:命令行控制的循環
while [[ "$*" != " " ]]
do
echo "$1"
shift
done
例:
#!/bin/bash
echo "number of arguments is $#"
echo "what you input is:"
while [[ "$*" != " " ]]
do
echo "$1"
shift
done