[关闭]
@yanglt7 2018-10-30T12:33:09.000000Z 字数 2886 阅读 782

《Shell 编程》09_while 循环和 until 循环

Shell


9.1 当型和直到型循环语法

9.1.1 while 循环语句

while 循环语句的基本语法为:

  1. while <条件表达式>
  2. do
  3. cmd...
  4. done

9.1.2 until 循环语句

  1. until <条件表达式>
  2. do
  3. cmd...
  4. done

9.1.3 范例

例 9-1 每隔 2 秒在屏幕上输出一次负载值。

  1. #!/bin/bash
  2. while true #<== 表达条件永远为真,会一直运行,称之为守护进程。
  3. do
  4. uptime
  5. sleep 2
  6. done

将负载值追加到 log 里,使用微秒单位

  1. #!bin/bash
  2. while [ 1 ]
  3. do
  4. uptime >>/tmp/uptime.log
  5. usleep 2000000
  6. done

通过在脚本的结尾使用 & 符号来在后台运行脚本

  1. [root@web001 scripts]# sh 9_1.sh &
  2. [2] 15344
  3. [root@web001 scripts]# tail -f /tmp/uptime.log
  4. 09:49:34 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
  5. 09:49:36 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
  6. 09:49:38 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
  7. 09:49:40 up 1 day, 7:25, 2 users, load average: 0.00, 0.01, 0.05
  8. ...

9.2 让 Shell 脚本在后台运行的知识

usage description
sh while1.sh & 把脚本 while1.sh 放到后台执行
ctrl+c 停止执行当前脚本或任务
ctrl+z 暂停执行当前脚本或任务
bg 把当前脚本或任务放到后台执行,background
fg 把当前脚本或任务放到前台执行,如果有多个任务,可以使用 fg 加任务编号调出对应的脚本任务,frontground
jobs 查看当前执行的脚本任务
kill 关闭执行的脚本任务,即以 “ kill %任务编号 ” 的形式关闭脚本
  1. [root@web001 scripts]# sh 194load.sh &
  2. [1] 16043
  3. [root@web001 scripts]# fg
  4. sh 194load.sh
  5. ^Z
  6. [1]+ Stopped sh 194load.sh
  7. [root@web001 scripts]# bg
  8. [1]+ sh 194load.sh &
  9. [root@web001 scripts]# jobs
  10. [1]+ Running sh 194load.sh &
  11. [root@web001 scripts]# fg 1
  12. sh 194load.sh
  13. ^C

使用 kill 命令关闭 jobs 任务脚本

  1. [root@web001 scripts]# jobs
  2. [1]- Running sh 194load.sh &
  3. [2]+ Running sh 194load.sh &
  4. [root@web001 scripts]# kill %2
  5. [root@web001 scripts]# jobs
  6. [1]- Running sh 194load.sh &
  7. [2]+ Terminated sh 194load.sh

进程管理的Linux 相关命令:

例 9-2 使用 while 循环或 until 循环竖向打印 54321。

  1. #!/bin/bash
  2. i=5
  3. #while ((i>0))
  4. #while [[ $i > 0 ]]
  5. #while [ $i -gt 0 ]
  6. until [[ i < 1 ]]
  7. do
  8. echo "$i"
  9. ((i--))
  10. done

例 9-3 猜数字游戏。首先让系统随机生成一个数字,范围为(1-60),让用户输入所猜的数字。如果不符合要求,则给予或高或低的猜对后则给出猜对所用的次数。

  1. #!/bin/bash
  2. cnt=0
  3. NUM=$((RANDOM%61))
  4. input(){
  5. read -p "pls input a num:" num
  6. expr $num + 1 &>/dev/null
  7. [ $? -ne 0 ] && echo "pls input a 'num'."
  8. input
  9. }
  10. guess(){
  11. ((cnt++))
  12. if [ $num -eq $NUM ]; then
  13. echo "You are right."
  14. if [ $cnt - le 3 ]; then
  15. echo "You have try $cnt times, excellent!"
  16. elif [ $cnt -gt 3 -a $cnt le 6 ]; then
  17. echo "You have try $cnt times, good."
  18. elif [ $cnt -gt 6 ]; then
  19. echo "You have try $cnt times."
  20. fi
  21. exit 0
  22. elif [ $num -gt $NUM ]; then
  23. echo "It is too big. Try again."
  24. input
  25. elif [ $num -lt $NUM ]; then
  26. echo "It is too small. Try again."
  27. input
  28. fi
  29. }
  30. main(){
  31. input
  32. while true
  33. do
  34. guess
  35. done
  36. }

9.3 while 循环按行读文件的方式总结

方式 1: 采用 exec 读取文件,然后进入 while 循环处理

  1. exec <FILE
  2. sum=0
  3. while read line
  4. do
  5. cmd
  6. done

方式 2:使用 cat 读取文件内容,然后通过管道进入 while 循环处理

  1. cat FILE_PATH|while read line
  2. do
  3. cmd
  4. done

方式 3:在 while 循环结尾 done 处输入重定向指定读取的文件。

  1. while read line
  2. do
  3. cmd
  4. done<FILE

小结

1)while 循环结构及相关语句综合实践小结

2)Shell 脚本中各个语句的使用场景

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注