本文共 3446 字,大约阅读时间需要 11 分钟。
本书历史上已出版最实战的Shell高级编程实战书籍,没有之一,和市面书籍不同,本书是作者经过18年的运维工作及教学工作后,创新类企业级实战书籍,适合所有学习及从事Linux相关工作的读者。
《跟老男孩学习Linux运维:Shell高级编程实战》第8章,本书预计于12月底出版,本章的部分代码如下:
有关代码的思路分析和详细注释,请支持作者购买正版书籍获得。
[root@oldboy C12]# for n in `ls *.sh`;do echo;echo;echo "[root@oldboy C12]# cat $n";cat $n;done[root@oldboy C12]# cat 12_1_1.sh#!/bin/bash #Author:oldboy training#Blog:http://oldboy.blog.51cto.com#Time:2016-09-21 23:16:11#Name:12_1_1.sh#Version:V1.0#Description:test{break|continue|exit|return}script.if [ $# -ne 1 ];then echo $"usage:$0 {break|continue|exit|return}" exit 1fitest(){for((i=0; i<=5; i++))do if [ $i -eq 3 ] ;then $*; fi echo $idoneecho "I am in func."}test $*func_ret=$?if [ `echo $*|grep return|wc -l` -eq 1 ] then echo "return's exit status:$func_ret" fiecho "ok"[root@oldboy C12]# cat 12_2_1.sh#!/bin/bash #Author:oldboy training#Blog:http://oldboy.blog.51cto.com#Time:2016-09-22 00:11:22#Name:12_2_1.sh#Version:V1.0#Description:config ip script.[ -f /etc/init.d/functions ] && . /etc/init.d/functionsRETVAL=0add(){for ip in {1..16}do if [ $ip -eq 10 ] then continue fi ip addr add 10.0.2.$ip/24 dev eth0 label eth0:$ip &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ] then action "add $ip" /bin/true else action "add $ip" /bin/false fidonereturn $RETVAL}del(){for ip in {16..1}do if [ $ip -eq 10 ] then continue fi #ip addr del 10.0.2.$ip/24 dev eth0 &>/dev/null ifconfig eth0:$ip down &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ] then action "del $ip" /bin/true else action "del $ip" /bin/false fidone}case "$1" in start) add RETVAL=$? ;; stop) del RETVAL=$? ;; restart) del sleep 2 add RETVAL=$? ;; *) printf "USAGE:$0 {start|stop|restart}\n"esacexit $RETVAL[root@oldboy C12]# cat 12_2_2.sh#!/bin/bash #Author:oldboy training#Blog:http://oldboy.blog.51cto.com#Time:2016-09-22 00:36:18#Name:12_2_2.sh#Version:V1.0#Description:config ip new script.[ -f /etc/init.d/functions ] && . /etc/init.d/functionsRETVAL=0op(){if [ "$1" == "del" ] then list=`echo {16..1}`else list=`echo {1..16}`fifor ip in $listdo if [ $ip -eq 10 ] then continue fi ip addr $1 10.0.2.$ip/24 dev eth0 label eth0:$ip &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ] then action "$1 $ip" /bin/true else action "$1 $ip" /bin/false fidonereturn $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"esacexit $RETVAL[root@oldboy C12]# cat 12_3_1.sh#!/bin/bashsum=0exec <$1while read linedo size=`echo $line|awk '{print $10}'` expr $size + 1 &>/dev/null if [ $? -ne 0 ];then continue fi ((sum=sum+$size))doneecho "${1}:total:${sum}bytes =`echo $((${sum}/1024))`KB"[root@oldboy C12]# cat 12_4_1.sh#!/bin/bashfor n in {0..32767}do echo "`echo $n|md5sum` $n" >>/tmp/zhiwen.logdone[root@oldboy C12]# cat 12_4_2.sh#!/bin/bash#for n in {0..32767}#do # echo "`echo $n|md5sum` $n" >>/tmp/zhiwen.log#donemd5char="4fe8bf20ed"while read linedo if [ `echo $line|grep "$md5char"|wc -l` -eq 1 ] then echo $line break fidone
转载地址:http://oypta.baihongyu.com/