[关闭]
@yanglt7 2018-11-01T15:09:41.000000Z 字数 2618 阅读 765

《Shell 编程》11_循环控制及状态返回值

Shell


11.1 break、continue、exit、return 的区别和对比

cmd description
break n 如果省略 n,则表示跳出整个循环,n 表示跳出循环的层数
continue n 如果省略 n,则表示跳过本次循环,忽略本次循环的剩余代码,进入循环的下一次循环。n 表示退到第 n 层继续循环
exit n 退出当前 Shell 程序, n 为上一次程序执行的状态返回值。n 也可以省略,在下一个 Shell 里可通过 “$?” 接收 exit 的 n 值
return n 用于在函数里作为函数的返回值,以判断函数执行是否正确。在下一个 shell 里可通过 “$?” 接收 exit n 的 n值

11.2 break、continue、exit、return 的基础示例

例 11-1

  1. #!/bin/bash
  2. if [ $# -ne 1 ];then
  3. echo "usage:$0 {break|continue|exit|return}"
  4. exit 1
  5. fi
  6. test(){
  7. for ((i=0; i<=5; i++))
  8. do
  9. if [ $i -eq 3 ];then
  10. $*;
  11. fi
  12. echo $i
  13. done
  14. echo "I am func."
  15. }
  16. test $*
  17. func_ret=$?
  18. if [ `echo $*|grep return|wc -l` -eq 1 ];then
  19. echo "return's exit status:$func_ret"
  20. fi
  21. echo "ok"
  1. [root@web001 scripts]# sh 251.sh
  2. usage:251.sh {break|continue|exit|return}
  1. [root@web001 scripts]# sh 251.sh break
  2. 0
  3. 1
  4. 2
  5. I am func.
  6. ok
  1. [root@web001 scripts]# sh 251.sh continue
  2. 0
  3. 1
  4. 2
  5. 4
  6. 5
  7. I am func.
  8. ok
  1. [root@web001 scripts]# sh 251.sh return
  2. 0
  3. 1
  4. 2
  5. return's exit status:0
  6. ok
  1. [root@web001 scripts]# sh 251.sh exit
  2. 0
  3. 1
  4. 2

例 11-2 开发 Shell 脚本实现为服务器临时配置多个 IP,并且可以随时撤销配置的所有 IP。IP 的地址范围为 10.0.2.1~10.0.2.16,其中 10.0.2.10 不能配置。

给网卡配置额外的 IP。

方法 1:使用 ifconfig 配置别名 IP 的方法:

  1. ifconfig ens33:0 10.0.2.10/24 up #<== 添加 IP
  2. ifconfig ens3:0 10.0.2.10/24 down #<== 删除 IP

方法 2:使用 IP 配置辅助 IP 的方法:

  1. ip addr add 10.0.2.11/24 dev ens33 label ens33:0
  2. ip addr del 10.0.2.11/24 dev ens33 label ens33:0
  1. #!/bin/bash
  2. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  3. RETVAL=0
  4. op(){
  5. if [ "$1" == "del" ];then
  6. list=`echo {16..1}`
  7. else
  8. list=`echo {1..16}`
  9. fi
  10. for ip in list
  11. do
  12. if [ $ip -eq 10 ];then
  13. continue
  14. fi
  15. ip addr $1 10.0.2.$ip/24 dev ens33 label ens33:$ip &>/dev/null
  16. RETVAL=$?
  17. if [ $RETVAL -eq 0 ];then
  18. action "$1 $ip" /bin/true
  19. else
  20. action "$1 $ip" /bin/false
  21. fi
  22. done
  23. return $RETVAL
  24. }
  25. case "$1" in
  26. start)
  27. op add
  28. RETVAL=$?
  29. ;;
  30. stop)
  31. op del
  32. RETVAL=$?
  33. ;;
  34. restart)
  35. op del
  36. sleep2
  37. op add
  38. RETVAL=$?
  39. ;;
  40. *)
  41. printf "USAGE:$0 {start|stop|restart}\n"
  42. esac
  43. exit $RETVAL

例 11-3 分析 Apache 访问日志,把日志中每行的访问字节数所对应的字段数字相加,计算出总的访问量。

方法 1:while 循环(采用 bash exec 内置命令和 expr 判断整数)

  1. #!/bin/bash
  2. sum=0
  3. exec <$1
  4. while read line
  5. do
  6. size=`echo $line|awk '{print $10}'`
  7. expr $size + 1 &>/dev/null
  8. if [ $? -ne 0 ];then
  9. continue
  10. fi
  11. ((sum=sum+size))
  12. done
  13. echo "${1}:total:${sum}bytes =`echo $((${sum}/1024))`KB"

方法2:while 循环(采用 bash exec 内置命令 + 变量字串替换特殊方方来判断整数)

  1. exec <$1
  2. sum=0
  3. while read line
  4. do
  5. num=`echo $line|awk '{print $10}'`
  6. [ -n "$num" -a "$num" = "${num//[^0-9]/}" ] || continue
  7. ((sum=sum+num))
  8. done
  9. echo "${1}:${num} bytes =`echo $((${num}/1024))`KB"

方法 3:

  1. exec <access_www.log
  2. sum=0
  3. while read line
  4. do
  5. [ -z "`echo $line|awk '{print $10}'|sed 's#[0-9]##g'`" ] || continue
  6. ((sum=sum+`echo $line|awk '{print $10}'`))
  7. done
  8. echo $sum

例 11-4 已知下面的字符串是通过将 RANDOM 随机数采用 md5sum 加密后任意取出连续 10 位的结果,请破解这些字符串对应的 md5sum 前的数字?

  1. 4fe8bf20ed
  1. #!/bin/bash
  2. for n in {0..32767}
  3. do
  4. echo "`echo $n|md5sum` $n" >>/tmp/zhiwen.log
  5. done
  1. md5char="4fe8bf20ed"
  2. while read line
  3. do
  4. if [ `echo $line|grep "md5char"|wc -l` -eq 1 ];then
  5. echo $line
  6. break
  7. fi
  8. done </tmp/zhiwen.log
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注