1.break、continue、exit、return的对比

break、continue在条件语句和循环语句中用于控制程序走向;

exit用于终止所有语句并退出当前脚本,还可以返回上一次程序或命令的执行状态值给当前shell;

return用于在函数内部返回函数执行的状态值。





2.基础示例

(1)break

[root@codis-178 ~]# cat 12_1.sh
#!/bin/bash
if [ $# -ne 1 ];then
echo $"usage:$0 {break|continue|exit|return}"
exit 1
fi
test(){
for((i=0;i<=5;i++))
do
if [ $i -eq 3 ];then
$*;
fi
echo $i
done
echo "I am in func."
}
test $*
func_ret=$?
if [ `echo $*|grep return|wc -l` -eq 1 ]
then
echo "return's exit status:$func_ret"
fi
echo "ok"
[root@codis-178 ~]# sh 12_1.sh
usage:12_1.sh {break|continue|exit|return}
[root@codis-178 ~]# sh 12_1.sh break
0
1
2
I am in func.
ok
[root@codis-178 ~]# sh 12_1.sh continue
0
1
2
4
5
I am in func.
ok

3.企业案例

(1)实现服务器临时配置多个IP,并且可以随时撤销配置的所有IP,IP范围:10.0.3.1~10.0.3.16,其中10不能配置。

[root@codis-178 ~]# cat 12_2.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
RETVAL=0
op(){
if [ "$1" == "del" ];then
list=`echo {16..1}`
else
list=`echo {1..16}`
fi
for ip in $list
do
if [ $ip -eq 10 ];then
continue
fi
ip addr $1 10.0.2.$ip/24 dev eth1 label eth1:$ip &>/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "$1 $ip" /bin/true
else
action "$1 $ip" /bin/false
fi
done
return $RETVAL
}
case "$1" in
start)
op add
RETVAL=$?
;;
stop)
op del
RETVAL=$?
;;
restart)
op del
sleep 2
op add
RETVAL=$?
;;
*)
printf "Usage:$0 {start|stop|restart}\n"
esac
exit $RETVAL

(2)分析Apache访问日志,把日志中每行的访问字节数所对应的字段数字相加,计算总和

[root@codis-178 ~]# cat 12_3.sh
#!/bin/bash
exec <$1
sum=0
while read line
do
num=`echo $line|awk '{print $10}'`
[ -n "$num" -a "$num" = "${num//[^0-9]/}"] || continue
((sum=sum+num))
done
echo "${1}:${sum} byte = `echo $((${sum}/1024))`KB"

(3)已知下面的字符串是通过RANDOM随机数采用md5sum加密后任意取出的连续10为数字,请破解这些字符串对应的md5sum数字

4fe8bf20ed

思路:
1.RANDOM取值0~32767,通过md5sum加密后,把加密后的字符串和加密前的数字存入文本中
[root@codis-178 ~]# cat 12_4.sh
#!/bin/bash
for n in {0..32767}
do
echo "`echo $n|md5sum` $n" >>zhiwen.log
done
[root@codis-178 ~]# sh 12_4.sh [root@codis-178 ~]# cat zhiwen.log |head -5
897316929176464ebc9ad085f31e7284 - 0
b026324c6904b2a9cb4b88d6d61c81d1 - 1
26ab0db90d72e28ad0ba1e22ee510510 - 2
6d7fce9fee471194aa8b5b6e47267f03 - 3
48a24b70a0b376535542b996af517398 - 4 2.将字符串与文本进行对比
[root@codis-178 ~]# cat 12_4_1.sh
#!/bin/bash
md5char="4fe8bf20ed"
while read line
do
if [ `echo $line|grep "$md5char"|wc -l` -eq 1 ];then
echo $line
break
fi
done </root/zhiwen.log
[root@codis-178 ~]# sh 12_4_1.sh
1dcca23355272056f04fe8bf20edfce0 - 5

Shell编程之循环控制及状态返回值的更多相关文章

  1. 【Linux】循环控制及状态返回值break、continue、exit、return

    一.break.continue.exit.return的区别和对比 break n     :如果省略n,则表示跳出整个循环,n表示跳出循环的层数 continue n:如果省略n,则表示跳过本次循 ...

  2. Bash脚本编程学习笔记04:测试命令test、状态返回值、位置参数和特殊变量

    我自己接触Linux主要是大学学习的Turbolinux --> 根据<鸟哥的Linux私房菜:基础篇>(第三版) --> 马哥的就业班课程.给我的感觉是这些课程对于bash的 ...

  3. linux编程中接收主函数返回值以及错误码提示

    程序A创建子进程,并调用进程B,根据不调用的不同情况,最后显示结果不同. #include <stdio.h> #include <unistd.h> #include < ...

  4. 关于用python作为第三方程序,来调用shell命令的问题,以及返回值格式解析

    1.用python语言作为第三方,调用shell 在python2.x中,可以通过包commands来进行调用shell命令.如下: cmd就是你要调用的shell命令,把环境配置好,输入正确的命令格 ...

  5. [shell]管道连接的命令判断返回值

    场景: 在bash执行管道连接的命令,需要获取到各个命令的返回值用于判断 在脚本中我们可能需要将执行结果打印到屏幕,同时保存在文件中供后面分析用,写出如下的命令 command 2>&1 ...

  6. HTTP各个状态返回值

    转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomcat没有启动起来 504 Gateway Time-out: nginx ...

  7. http 各个状态返回值

    code 定义在 org.apache.http.HttpStatus 转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomc ...

  8. Shell脚本获取C语言可执行程序返回值

    #!/bin/sh #./test是c程序,该程序 返回0 ./test OP_MODE=$? echo $OP_MODE # $? 显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误.

  9. 在shell脚本中使用函数的返回值

    #!/bin/bash - function mytest() { echo "arg1 = $1" if [ $1 = "1" ] ;then return ...

随机推荐

  1. plsql programming 11 记录类型

    记录类型非常类似数据库表中的行. 记录作为一个整体本身并没有值, 不过每个单独成员或字段都有值, 记录提供了一种把这些值当做一组进行操作的方法. 例如: 1: -- create a table 2: ...

  2. 第一百九十三节,jQuery EasyUI,Draggable(拖动)组件

    Draggable(拖动)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Draggable(拖动)组件的使用方法,这个组件不依赖于其 他组 ...

  3. Struts2 主题和模板

    实际本章教程开始之前,让我们看看由http://struts.apache.org给出的几个定义: Term 描述 tag A small piece of code executed from wi ...

  4. python 面试题 string int

    str1 = 'hello' str2 = str1 str3 = str1 str4 = str1 str1 = '' int1 = 1 int2 = int1 int3 = int1 int4 = ...

  5. org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [cn.facekee.cms.entity.CmsFansgroup#195]

    刚开始报错还是报的稀奇古怪的错误,让我纠结了好久,再三检查报错的位置,发现并没有错误,最后认真分析查看每行报错的信息才找到如题所述的错误!!!!! 报这种错误的原因可能是POJO映射文件中的字段和数据 ...

  6. splay tree旋转操作 hdu 1890

    很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...

  7. HashMap和ConcurrentHashMap的区别,HashMap的底层源码。

    Hashmap本质是数组加链表.根据key取得hash值,然后计算出数组下标,如果多个key对应到同一个下标,就用链表串起来,新插入的在前面. ConcurrentHashMap:在hashMap的基 ...

  8. mysql编译参数详解(./configure)

    1.--prefix=PREFIX:指定程序安装路径: 2.--enable-assembler:使用汇编模式:(文档说明:compiling in x86 (and sparc) versions  ...

  9. WPF中DPI的问题

    先搞清楚一下几个概念: DPI:dots  per  inch ,每英寸的点数.我们常说的鼠标DPI,是指鼠标移动一英寸的距离滑过的点数:打印DPI,每英寸的长度打印的点数:扫描DPI,每英寸扫描了多 ...

  10. mongodb基础操作

    查询选择器>db.customers.find({age:{$lt:102}})查询age小于102的数据$lte表示小于或等于$gt表示大于$gte表示大于或等于>db.customer ...