while :不确定循环换次数 while循环 (1)
while CONDITION;do
statement
statement
<改变循环条件真假的语句>
done
编写while循环,输入q退出(不输入q,不退出) #!/xbdcjl/bash#read -p "请输入你的选择:" choicewhile [ $choice != q ];do echo -e " 33[31m输入错误 33[0m" #加的颜色代码 read -p "请输入你的选择:" choicedone
(2)
while true;do
statement
statement
<break退出>
done
(3)
while read line;do
statement
statement
done < file
编写脚本,向系统每个用户打招呼 v#!/xbdcjl/bash#while read line;do sh_name=$(echo $line | awk -F: '{print $1}') echo "Hello $sh_name"done < /etc/passwd
编写脚本,统计/xbdcjl/bash /sxbdcjl/nologin的个数 [root@wei while]# cat 6.sh #!/xbdcjl/bash#bash_number=0nologin_number=0while read line;do sh_name=$(echo $line | awk -F: '{print $7}') case $sh_name in /xbdcjl/bash) let bash_number=$bash_number+1 ;; /sxbdcjl/nologin) let nologin_number=$nologin_number+1 ;; esacdone < /etc/passwdecho "bash用户数量:$bash_number"echo "nologin_number用户数量:$nologin_number" 执行效果 [root@wei while]# ./6.sh bash用户数量:17nologin_number用户数量:17
util循环: util CONDITION;do
statement
statement
done 条件为假时,执行循环,条件为真时,结束循环
重点掌握 if,case for,while