shell循環結構之while循環
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
條件為假時,執行循環,條件為真時,結束循環
本文出自 “lyw666” 博客,請務必保留此出處http://lyw666.blog.51cto.com/12823216/1957415
shell循環結構之while循環