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


    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}
    "
    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
    
  • 相关阅读:
    VHDL硬件描述语言(三)——基本数据对象和数据类型
    VHDL硬件描述语言(二)——子程序
    VHDL硬件描述语言(一)——基本结构
    C#类
    C#基本语法
    C#的简单数据类型
    C#与.NET
    ARP
    IPv6
    以太网
  • 原文地址:https://www.cnblogs.com/tongxiaoda/p/7454663.html
Copyright © 2020-2023  润新知