版权声明: https://blog.csdn.net/weixin_42061048/article/details/83548375
《Shell 编程》09_while 循环和 until 循环
标签(空格分隔): Shell
文章目录
9.1 当型和直到型循环语法
9.1.1 while 循环语句
while 循环语句的基本语法为:
while <条件表达式>
do
cmd...
done
9.1.2 until 循环语句
until <条件表达式>
do
cmd...
done
9.1.3 范例
例 9-1
每隔 2 秒在屏幕上输出一次负载值。
#!/bin/bash
while true #<== 表达条件永远为真,会一直运行,称之为守护进程。
do
uptime
sleep 2
done
将负载值追加到 log 里,使用微秒单位
#!bin/bash
while [ 1 ]
do
uptime >>/tmp/uptime.log
usleep 2000000
done
通过在脚本的结尾使用 & 符号来在后台运行脚本
[root@web001 scripts]# sh 9_1.sh &
[2] 15344
[root@web001 scripts]# tail -f /tmp/uptime.log
09:49:34 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
09:49:36 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
09:49:38 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
09:49:40 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
...
9.2 让 Shell 脚本在后台运行的知识
usage | description |
---|---|
sh while1.sh & | 把脚本 while1.sh 放到后台执行 |
ctrl+c | 停止执行当前脚本或任务 |
ctrl+z | 暂停执行当前脚本或任务 |
bg | 把当前脚本或任务放到后台执行,background |
fg | 把当前脚本或任务放到前台执行,如果有多个任务,可以使用 fg 加任务编号调出对应的脚本任务,frontground |
jobs | 查看当前执行的脚本任务 |
kill | 关闭执行的脚本任务,即以 “ kill %任务编号 ” 的形式关闭脚本 |
[root@web001 scripts]# sh 194load.sh &
[1] 16043
[root@web001 scripts]# fg
sh 194load.sh
^Z
[1]+ Stopped sh 194load.sh
[root@web001 scripts]# bg
[1]+ sh 194load.sh &
[root@web001 scripts]# jobs
[1]+ Running sh 194load.sh &
[root@web001 scripts]# fg 1
sh 194load.sh
^C
使用 kill 命令关闭 jobs 任务脚本
[root@web001 scripts]# jobs
[1]- Running sh 194load.sh &
[2]+ Running sh 194load.sh &
[root@web001 scripts]# kill %2
[root@web001 scripts]# jobs
[1]- Running sh 194load.sh &
[2]+ Terminated sh 194load.sh
进程管理的Linux 相关命令:
- kill、killall、pkill:杀掉进程。
- ps:查看进程。
- pstree:显示进程状态树。
- top:显示进程。
- renice:改变优先权。
- nohup:用户退出系统之后继续工作。
- pgrep:查找匹配条件的进程。
- strace:跟踪一个进程的系统调用清理。
- ltrace:跟踪进程调用库函数的情况。
例 9-2
使用 while 循环或 until 循环竖向打印 54321。
#!/bin/bash
i=5
#while ((i>0))
#while [[ $i > 0 ]]
#while [ $i -gt 0 ]
until [[ i < 1 ]]
do
echo "$i"
((i--))
done
例 9-3
猜数字游戏。首先让系统随机生成一个数字,范围为(1-60),让用户输入所猜的数字。如果不符合要求,则给予或高或低的猜对后则给出猜对所用的次数。
#!/bin/bash
cnt=0
NUM=$((RANDOM%61))
input(){
read -p "pls input a num:" num
expr $num + 1 &>/dev/null
[ $? -ne 0 ] && echo "pls input a 'num'."
input
}
guess(){
((cnt++))
if [ $num -eq $NUM ]; then
echo "You are right."
if [ $cnt - le 3 ]; then
echo "You have try $cnt times, excellent!"
elif [ $cnt -gt 3 -a $cnt le 6 ]; then
echo "You have try $cnt times, good."
elif [ $cnt -gt 6 ]; then
echo "You have try $cnt times."
fi
exit 0
elif [ $num -gt $NUM ]; then
echo "It is too big. Try again."
input
elif [ $num -lt $NUM ]; then
echo "It is too small. Try again."
input
fi
}
main(){
input
while true
do
guess
done
}
9.3 while 循环按行读文件的方式总结
方式 1: 采用 exec 读取文件,然后进入 while 循环处理
exec <FILE
sum=0
while read line
do
cmd
done
方式 2:使用 cat 读取文件内容,然后通过管道进入 while 循环处理
cat FILE_PATH|while read line
do
cmd
done
方式 3:在 while 循环结尾 done 处输入重定向指定读取的文件。
while read line
do
cmd
done<FILE
小结
1)while 循环结构及相关语句综合实践小结
- while 循环的特长是执行守护进程,以及实现我们希望循环持续执行不退出的应用,适用于频率小于 1 分钟的循环处理,其他的 while 循环几乎都可以被 for 循环及定时任务 crond 功能所替代。
- case 语句可以用 if 语句来替换,而在系统启动脚本时传入少量固定规则字符串的情况下,多用 case 语句,其他普通判断多用 if 语句。
- 一句话场景下,if 语句、for 语句最常用,其次是 while(守护进程)、case(服务启动脚本)。
2)Shell 脚本中各个语句的使用场景
- 条件表达式,用于简短的条件判断及输出(文件是否存在,字符串是否为空等)。
- if 取值判断,多用于不同值数量较少的情况。
- for 最常用于正常的循环处理中。
- while 多用于守护进程、无限循环(要加 sleep 和 usleep 控制频率)场景。
- case 多用于服务启动脚本中,打印菜单可用 select 语句,不过很少见,一般用 cat 的 here 文档方法来替代。
- 函数的作用主要是使编码逻辑清晰,减少重复语句开发。