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 ...
随机推荐
- Clion下同时编写多个main函数
在你的CMakeLists.txt文件下配置,使用add_executable(),前面的一定要不一样 红色部分是描述main的,配置后运行处可以选择:
- 移动端 - adb shell常见问题汇总
一.如何执行adb命令? 答:如果没有配置SDK的环境变量的话,那就先用cd命令进入adb所在文件目录(即F:\android-sdk-windows\platform-tools)后,再执行adb命 ...
- hdu5438 Ponds[DFS,STL vector二维数组]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...
- 【大数据】分布式文件系统HDFS 练习
作业要求来自于https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 利用Shell命令与HDFS进行交互 以”./bin/dfs d ...
- python简单的游戏场景代码
模拟英雄联盟游戏场景的简单场景 最后计算出英雄的战斗力 class Hero: def __init__(self, na, gen, age, fig): self.name = na self.g ...
- Nginx配置简单基于域名的虚拟主机
首先修改hosts文件,让浏览器在看到a.com或是www.a.com的网址时知道上哪里去找: # Copyright (c) 1993-2009 Microsoft Corp. # # This i ...
- 在evernote中如何使序号正常连续?
答: 在二级内容之前按Shift + Enter键,再次换行即可序号正常,示例如下: 1. 第一行(在此处按下Shift+Enter键) 第一行第一列(在此处按下Shift+Enter键) 第一行第二 ...
- typescript - 3.函数
(1)函数的定义 ## 函数声明法 // function run():string{ // return 'run'; // } //错误写法,返回类型错误 // function run():st ...
- ISO/IEC 9899:2011 摘要
本国际标准指定了C编程语言的形式并建立了对用它所表达的程序的解释.其目的在于促进在多种计算机系统上的C语言程序的可移植性.可靠性.可维护性以及高效的执行. 为了详细地说明C语言本身以及C语言执行库,包 ...
- 012-Shell 提示确认(Y / N,YES / NO)
例1:确认提示(一次) 这个示例代码将为确认提示一次,如果你给输入错误,程序会以状态1退出.这个例子将只接受Y或N或YES或NO(不区分大小写). #!/bin/bash read -r -p &qu ...