Shell编程之循环控制及状态返回值

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

break、continue在条件语句和循环语句中用于控制程序走向;
exit用于终止所有语句并退出当前脚本,还可以返回上一次程序或命令的执行状态值给当前shell;
return用于在函数内部返回函数执行的状态值。
1219690-20170830180837530-1836164861.png

1219690-20170830180906921-2013100938.png
1219690-20170830180930765-1089238221.png
1219690-20170830181017905-1069862583.png

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

转载于:https://www.cnblogs.com/tongxiaoda/p/7454663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值