方式一:计数器控制while的循环
#!/bin/bash
sum=0
i=0
while (( i <= 100 ))
do
let "sum=sum+$i"
let "i=i+2"
done
echo $sum
方式二:结束标记控制循环
read -p "please input a num[1-10] " num2
while [[ "$num" != 5 ]]
do
if [ "$num" -lt 5 ];then
echo "too small,try again"
read num
elif [ "$num" -gt 5 ];then
echo "too big ,try again"
read num
else
exit 0
fi
done
echo "you are right"
方式三:标志控制的while循环
#!/bin/bash
read -p "please input a num [1-10]" num
signal=0
while [[ "$signal" != 1 ]]
do
if [ "$num" -lt 4 ];then
echo "too small ,try again"
read num
elif [ "$num" -gt 4 ];then
echo "too big,try again"
read num
else
signal=1
fi
done
echo "you are right"
方式四:命令行控制的while循环
#!/bin/bash
##第四种while使用方式
echo "number of arguement: $#"
echo "what you input is:"
while [[ "$*" != "" ]]
do
echo "$1"
shift
done