while 循环语句的基本语法
while <条件表达式>
do
指令...
done
示例:显示每分钟负载情况
#!/bin/bash
while true
do
uptime >> uptime.txt
sleep 5
done
示例:打印54321
#!/bin/bash
i=5
while (( i > 0 ))
do
echo "$i"
(( i-- ))
done
#或者用以下
#while [ $i -gt 0 ]
#do
# echo "$i"
# (( i-- ))
#done
until 循环是不成立进行循环
#!/bin/bash
i=5
until (( i < 1 ))
do
echo "$i"
(( i-- ))
done
#until [ $i -lt 1 ]
#do
# echo "$i"
# (( i-- ))
#done
示例:计算100以内的和
#/bin/bash
i=1
sum=0
while (( i <=100 ))
do
(( sum+=i ))
(( i++ ))
done
[ "$sum" -ne 0 ] && printf "$sum\n"
#while [ $i -le 100 ]
#do
# (( sum+=i ))
# (( i++ ))
#done
#[ "$sum" -ne 0 ] && printf "$sum\n"
示例:检测网站状态
#!/bin/bash
read -p "请输入一个网址:" web #判断是否输入网址
[ -z "$web" ] && echo "输入错误" && exit
#if [ $# -ne 1 ]; then
# echo "请输入一个网址"
# exit 1
#fi
while true
do
if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}" $web|egrep -w "200|301|302"|wc -l` -ne 1 ] 对传入的url进行状态过滤 如果不等于1则表示状态不对
then
echo "$web is error"
else
echo "$web is ok"
fi
sleep 3
done
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有