bash之逻辑控制--while
while循环
示例:
❖ while设置条件
i=0; while ((i<3)) ;do echo $i; ((i=i+1));done
❖ ⼀⾏⾏的读取⽂件内容
while read line; do echo $line ; done < /tmp/tmp
退出控制语句:
return 函数返回
exit 脚本进程退出
break 退出当前循环
continue 跳过当前的循环,进⼊下⼀次循环。
一个简单的抽奖小程序:
lucky ()
{
seeds=$(while read line; do echo $line; done < 10.wx );
count=0;
index=0;
while ((count!=1)); do
((index+=1));
temp=$seeds;
seeds=$( for line in $seeds; do ((RANDOM%2==1)) && echo $line ; done);
echo;
echo 第 $index 轮:$seeds;
if [ -z "$seeds" ];then seeds=$temp;else count=$(echo "$seeds" | wc -l );fi
done
}