#!/bin/bash
int=1 #注意这里定义变量时,等号两边不能有空格
while(( "$int" <= 5))
do
echo "$int"
let "int++"
done
结果:
~/Note/test # ./11_while1.sh
1
2
3
4
5
例子2:
#!/bin/bash
echo "Please input the num(1-10):"
read num
while [[ "$num" != 4 ]]
do
if [ "$num" -lt 4 ];then
echo "Too small,Try again"
echo "input again"
read num
elif [ "$num" -gt 4 ];then
echo "To high,Try again"
echo "input again"
read num
else
exit 0
fi
done
echo "Congratulation,you are right."
结果:
~/Note/test # ./12while.sh
Please input the num(1-10):
5
To high,Try again
input again
3
Too small,Try again
input again
4
Congratulation,you are right.
例子3:打印参数while
#!/bin/bash
echo "number of arguments is $#"
echo "what you input is:"
while [[ "$*" != "" ]]
do
echo "$1"
shift
done
结果
~/Note/test # ./14while.sh
number of arguments is 0
what you input is:
~/Note/test # ./14while.sh 1 2 3
number of arguments is 3
what you input is:
1
2
3