您的位置:首页 > 运维架构 > Shell

shell循环结构之while循环

2017-08-18 17:23 429 查看
while循环

1)

while CONDITION; do
statement
statement
<改变循环条件真假的语句>
done

编写脚本,计算1---100的和

#!/bin/bash
#

sum=0
i=1

while [ $i -le 100 ]; do
let sum=$sum+$i
let i=$i+1
done

echo $sum

2)

while true; do
statement
statement
done

#!/bin/bash
#

while true; do
read -p "请输入你的选择:" choice
if [ $choice == "q" ]; then
break
fi
done

#!/bin/bash
#

while true; do
uptime
sleep 3
done

3)

while read line; do
statement
statement
done < file

#!/bin/bash
#

bash_num=0
nologin_num=0

while read line; do
sh_name=$(echo $line | awk -F: '{print $7}')
case $sh_name in
/bin/bash)
let bash_num=$bash_num+1
;;
/sbin/nologin)
let nologin_num=$nologin_num+1
;;
esac
done < /etc/passwd

echo $bash_num
echo $nologin_num

util循环:

util CONDITION; do
statement
statement
done

条件为假时,执行循环,条件为真时,结束循环
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell之while