shell 学习笔记9-while/until循环语句
一、while循环语句
1、循环语句
循环愈久就是重复执行一条指令或一组执行,知道条件不在满足时停止,shell循环语句包括,while、until、for、select语句
2、while循环
主要用来重复执行命令或语句,常用来守护进程或持续运行的程序,其实大多数循环都会用for循环语句
3、语法
while <条件表达式>
do
指令...
done
4、流程图

二、until循环语句
1、until循环语句语法
until <条件表达式>
do
指令...
done
until 循环语句用法与while类似,只是until会在表达式不成立时,进入循环执行命令,表达式成立,终止循环,until应用场景非常罕见
三、知识补充
1、当型和直型循环的范例
shell中有两个休息命令:sleep 1 表示休息1秒,usleep 1000000 也表示休息1秒
1)每隔2秒输出一次系统负载
[root@web1 scripts]# ./test
test10.sh test14.sh test19.sh test22.sh test27.sh test31.sh test36.sh test7.sh
test11.sh test15.sh test1.sh test23.sh test28.sh test32.sh test3.sh test8.sh
test12-.sh test16.sh test20.sh test24.sh test29.sh test33.sh test4.sh test9.sh
test12.sh test17.sh test21-.sh test25.sh test2.sh test34.sh test5.sh
test13.sh test18.sh test21.sh test26.sh test30.sh test35.sh test6.sh
[root@web1 scripts]# cat test36.sh
#!/bin/bash
while true #while true 表示条件永远为真,就像是死循环一样,我们称为守护进程
do
uptime
sleep 2 #休息2秒,在继续循环,while true循环中最好添加类似sleep的命令,可以控制脚本频率,省资源,不然真成死循环了
done
[root@web1 scripts]# ./test36.sh
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
2)每隔2秒在屏幕上输出一次负载并将负载值追加到log里,使用微秒单位
[root@web1 scripts]# cat test37.sh
#!/bin/bash
while [ ]
do
uptime >>/tmp/uptime.log
usleep
done
[root@web1 scripts]# ./test37.sh
^C
[root@web1 scripts]# cat /tmp/uptime.log
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
3)通过结尾使用&符合后台运行脚本
[root@web1 scripts]# ./test37.sh &
[]
[root@web1 scripts]# tail -f /tmp/uptime.log
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
:: up days, :, users, load average: 0.00, 0.01, 0.05
2、shell脚本后台运行的知识补充
1)用法及说明

[root@web1 scripts]# ./test37.sh & #后台运行脚本
[]
[] Terminated ./test37.sh
[root@web1 scripts]# fg #将脚本放到前台执行,如果多脚本任务,可以使用fg加jobs输出的任务编号调出相应的脚本到前台
./test37.sh
^Z #使用ctrl+z快捷键,暂停脚本
[]+ Stopped ./test37.sh
[root@web1 scripts]# bg #bg,将当前执行的脚本放到后台运行
[]+ ./test37.sh &
[root@web1 scripts]# jobs #查看任务
[]+ Running ./test37.sh & [root@web1 scripts]# fg 2 #fg加jobs输出的任务号调出脚本到前台来运行
./test37.sh
^C
[root@web1 scripts]# jobs
[root@web1 scripts]# ./test37.sh &
[]
[root@web1 scripts]# jobs
[]+ Running ./test37.sh &
[root@web1 scripts]# kill %1 #kill 命令关闭jobs任务脚本
[root@web1 scripts]# jobs
[]+ Terminated ./test37.sh
[root@web1 scripts]#
四、范例
1、使用while循环竖向打印54321
[root@web1 scripts]# cat test38.sh
#!/bin/bash
i=5 #因为是从大到小,所以初始化i的值为5
while ((i>)) #双小括号条件表达式,当i大于0不成立时就跳出循环
do
echo "$i" #打印变量i的值
((i--)) #i的值自减,每次减1
done [root@web1 scripts]# ./test38.sh [root@web1 scripts]#
2、使用双中括号条件表达式
[root@web1 while-until]# cat test39.sh
#!/bin/bash
i=
while [[ $i > ]]
do
echo $i
((i--))
done [root@web1 while-until]# ./test39.sh [root@web1 while-until]#
3、使用传参需要打印的数字
[root@web1 while-until]# cat test40.sh
#!/bin/bash i="$1"
while [[ $i -gt ]]
do
echo $i
((i--))
done [root@web1 while-until]# ./test40.sh [root@web1 while-until]#
4、使用until语句实现
[root@web1 while-until]# cat test41.sh
#!/bin/bash
i=
until [[ $i < ]] #当条件表达式不成立时,进入循环执行命令,因为0<1成立,因此退出循环
do
echo $i
((i--))
done
[root@web1 while-until]# ./test41.sh [root@web1 while-until]#
5、计算1加到100之和
[root@web1 while-until]# cat test42.sh
#!/bin/bash
i=1 #i为自增变量,从1到100,初始值1
sum=0 #总和变量初始值为0
while ((i<=)) #条件是i小于等于100,也就是从1加到100
do
((sum=sum+i)) #把i的值和当前sum总和的值做加法结果重新赋值给sum
((i++)) #i每次加1
done
[ "$sum" -ne ] && printf "totalsum is:$sum\n" [root@web1 while-until]# ./test42.sh
totalsum is:
[root@web1 while-until]#
6、通过数学公式实现
[root@web1 while-until]# cat -.sh
#!/bin/bash
i=
((sum=i*(i+)/)) #利用数公式求和公式计算,效率很高
echo $sum [root@web1 while-until]# ./-.sh
7、猜数字游戏
#!/bin/bash
total=
export LANG="zh_CN.UTF-8"
NUM=$((RANDOM%))
echo "当前苹果价格是每斤$NUM元"
echo "========================"
usleep
clear
echo '这苹果多少钱一斤啊?
请猜0-60的数字'
apple(){
read -p "请输入你的价格:" PRICE
expr $PRICE + &>/dev/null
if [ $? -ne ]
then
echo "别逗我了,快猜数字"
apple
fi
} guess(){
((total++))
if [ $PRICE -eq $NUM ]
then
echo "猜对了,就是$NUM元"
if [ $total -le ];then
echo "一共猜了$total次,太牛了。"
elif [ $total -gt -a $total -le ];then
echo "一共猜了$total次,次数有点多,加油啊。"
elif [ $total -gt ];then
echo "一共猜了$total次,行不行,猜了这多次"
fi
exit
elif [ $PRICE -gt $NUM ]
then
echo "嘿嘿,要不你用这个价买?"
echo "再给你一次机会,请继续猜:"
apple
elif [ $PRICE -lt $NUM ]
then
echo "太低太低"
echo "再给你一次机会,请继续猜:"
apple
fi
}
main(){
apple
while true
do
guess
done
}
main
8、手机充值
手机充值10元,每发一次短信(输出当前余额)花费1角5分钱,当余额地域1角5分钱时就不能再发短信了,提示余额不足,请重置,充值后可以继续发短信
[root@oldboy C11]# cat 10_5_1.sh
#!/bin/sh
export LANG="zh_CN.UTF-8"
sum=
msg_fee=
msg_count=
menu(){
cat <<END
当前余额为${sum}分,每条短信需要${msg_fee}分
==========================
.充值
.发消息
.退出
==========================
END
}
recharge(){
read -p "请输入金额充值:" money
expr $money + &>/dev/null
if [ $? -ne ]
then
echo "then money your input is error,must be int."
else
sum=$(($sum+$money))
echo "当前余额为:$sum"
fi
}
sendInfo(){
if [ ${sum} -lt $msg_fee ]
then
printf "余额不足:$sum ,请充值。\n"
else
while true
do
read -p "请输入短信内容(不能有空格):" msg
sum=$(($sum-$msg_fee))
printf "Send "$msg" successfully!\n"
printf "当前余额为: $sum\n"
if [ $sum -lt ]
then
printf "余额不足,剩余$sum分\n"
return
fi
done
fi
}
main(){
while true
do
menu
read -p "请输入数字选择:" num
case "$num" in
)
recharge
;;
)
sendInfo
;;
)
exit
;;
*)
printf "选择错误,必须是{1|2|3}\n"
esac
done
}
main
9、使用while守护进程的方式监控网站,每隔10s确定一次网站是否正常
#!/bin/sh
if [ $# -ne ];then
echo $"usage $0 url"
exit
fi
while true
do
if [ `curl -o /dev/null --connect-timeout -s -w "%{http_code}" $|egrep -w "200|301|302"|wc -l` -ne ]
then
echo "$1 is error."
#echo "$1 is error."|mail -s "$1 is error." --@qq.com
else
echo "$1 is ok"
fi
sleep
done
[root@web1 while-until]# ./-.sh
usage ./-.sh url
[root@web1 while-until]# ./-.sh www.baidu.com
www.baidu.com is ok
^C
[root@web1 while-until]# ./-.sh www.baidu1111.com
www.baidu1111.com is ok
^C
[root@web1 while-until]# ./-.sh www.111baidu1111.com
www.111baidu1111.com is error.
^C
[root@web1 while-until]#
参考学习引用:https://blog.51cto.com/oldboy/1855442
参考学习图书:<跟老男孩学linux运维:shell编程实战 >
转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11412371.html
shell 学习笔记9-while/until循环语句的更多相关文章
- python学习笔记(7)--循环语句
循环语句如下: for i in range(start, end): //注意 前闭后开 coding for i in range(m,n,k): coding for c in s: codin ...
- Objective-C学习笔记(十)——循环语句for和do-while的使用
在OC中.除了while这样的循环方式外,还有另外for循环和do-while循环.它们在不同的业务逻辑下会有不同的作用.能够和C语言和Java对照着学习. (一)代码一: int main(int ...
- shell学习笔记
shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...
- [转帖][Bash Shell] Shell学习笔记
[Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html 阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...
- shell学习笔记汇总
1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...
- SHELL学习笔记三
SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...
- SHELL学习笔记----IF条件判断,判断条件
SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外. if list then do something here ...
- shell 学习笔记2-shell-test
一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...
- Shell脚本的条件控制和循环语句
条件判断:if语句 语法格式: if [ expression ] then Statement(s) to be executed if expression is true fi 注意:expre ...
随机推荐
- Dense Semantic Labeling with Atrous Spatial Pyramid Pooling and Decoder for High-Resolution Remote Sensing Imagery(高分辨率语义分割)
对 Potsdam and Vaihingen 公开数据集进行处理,得到了SOTA的结果,超越DeepLab_v3+,提出的网络结构如下:结合了ASPP和FCN,UNet
- Java操作Excel中HSSFCell.CELL_TYPE_STRING、BOOLEAN、NUMERIC无定义解决方法
错误原因:jar包版本更新,官方改动: 解决方法: 导入CellType包import org.apache.poi.ss.usermodel.CellType使用CellType.STRING代替H ...
- 再见,Eclipse。
阅读本文大概需要 5 分钟. 来源:cnblogs.com/ouyida3/p/9901312.html 最近,改用了 IDEA,同事都说我投敌了.当然,这些同事都是和我一样的“老”程序员.不说毕业生 ...
- .sql文件l通过PLSQL导入到Oracle数据库
最近从第三方共享到一个数据,对方提供的是.sql文件.如何导入Oracle数据库? 开始想通过navicat for mysql工具--运行SQL文件来导入表---总是出现错误,失败. 后来还是用回P ...
- linux学习(1):linux命令大全
Linux命令 目录 1 文件管理... 5 1.1 basename. 5 1.2 cat 5 1.3 cd. 5 1.4 ...
- iptables实现端口映射(本地和远程端口映射)
说明:需要将外网访问本地IP(192.168.75.5)的80端口转换为访问192.168.75.3的8000端口,这就需要用到iptables的端口映射 实现:1. 需要先开启linux的数据转发功 ...
- [转]npm安装教程
原文地址:https://www.cnblogs.com/lgx5/p/10732016.html 一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpa ...
- tomcat请求响应代码分享
NioEndpoint的Poller轮询器持续进行扫描是否有新的event()方法调用后产生新的event配置. 发现后执行AbstractProtocol.class中的process()方法进行处 ...
- Shell中的通配符
shell常见的通配符,注意与正则稍有不同: 字符 含义 实例 * 匹配0个或多个任意字符 a*b,a与b之间可以有任意长度的字符,也可以没有. 例如:aabcb,ab,azxcb... ? 匹配一个 ...
- Java高级面试题整理(附答案)
这是我收集的10道高级Java面试问题列表.这些问题主要来自 Java 核心部分 ,不涉及 Java EE 相关问题.你可能知道这些棘手的 Java 问题的答案,或者觉得这些不足以挑战你的 Java ...