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
条件为假时,执行循环,条件为真时,结束循环