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. Linux Linux程序练习三

    /* index1 = 45 index2 = 36 index3 = 231 index4 = 43 index5 = 100 index6 = 123 index7 = 51 * * 通过读取读取 ...

  2. Linux 文件管理(C语言库函数二--程序日志)

    文件删除和改名 int remove(const char *pathname); int rename(const char *oldpath,const char *newpath); remov ...

  3. 学习spring1--跟我一起学Spring 3(2)–开发环境配置

    http://www.importnew.com/13185.html#spring     首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所 ...

  4. Eclipse 菜单

    Eclipse 菜单 Eclipse 查看的菜单栏通常包含以下几个菜单: File 菜单 Edit 菜单 Navigate 菜单 Search 菜单 Project 菜单 Run 菜单 Window ...

  5. ios 关于collectionView.performBatchUpdates()方法 --时时更新

    今天想实现一个简单的collectionView动画效果,查阅相关资料发现,实现 collectionView.performBatchUpdates()方法即可,于是掉坑里了. 文档: public ...

  6. EditText相关属性设置

    1.默认不弹出软件盘 在AndroidManifest.xml设置: <activity            android:name="com.demo.Activity" ...

  7. iOS面试题--Model层--数据持久化储存方案

    数据持久化储存方案有哪些? iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 [NSU ...

  8. iptables 实际操作 之 规则查询 2

    在之前的文章中,我们已经总结过,iptables 为我们预定义了4张表,他们分别是raw 表,mangle表,nat表,filter表,不同的表拥有不同的功能. filter 负责过滤功能,比如允许那 ...

  9. iOS学习笔记(三)——iOS系统架构

    iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch l ...

  10. 1677 treecnt(贡献)

    1677 treecnt 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 给定一棵n个节点的树,从1到n标号.选择k个点,你需要选择一些边使得这k个点通过选择的边联 ...