本文共 2439 字,大约阅读时间需要 8 分钟。
和市面书籍不同,本书是作者经过15年以上运维工作及教学工作后,创新类企业级实战书籍,适合所有学习及从事Linux相关工作的读者。
《跟老男孩学习Linux运维:Shell高级编程实战》第6章,本书已于2017年1月出版,为了答谢读者,特将本章的部分代码分享如下:
有关代码的思路分析和详细注释,请支持作者购买正版书籍获得。
购作者签名书请访问下面地址:
如果你觉得看书枯燥乏味,可以购买本书配套视频和老男孩隔空对话学习。
第6章核心代码(最后更新日期2017年2月2日)
[root@oldboy C06]# cat 6_15.sh[ -f /etc ] || { echo 1 echo 2 echo 3}[root@oldboy C06]# cat 6_34_1.sh#!/bin/shecho -n "pls input a char:" read var[ "$var" == "1" ] && { echo 1 exit 0}[ "$var" == "2" ] && { echo 2 exit 0}[ "$var" != "2" -a "$var" != "1" ] && { echo error exit 0}[root@oldboy C06]# cat 6_34_2.sh#!/bin/shvar=$1[ "$var" == "1" ] && { echo 1 exit 0}[ "$var" == "2" ] && { echo 2 exit 0}[ "$var" != "2" -a "$var" != "1" ] && { echo error exit 0}[root@oldboy C06]# cat 6_35_1.sh#!/bin/shread -p "Pls input two num:" a b#no1[ -z "$a" ] || [ -z "$b" ] && { echo "Pls input two num again." exit 1}#no2expr $a + 10 &>/dev/nullRETVAL1=$?expr $b + 10 &>/dev/nullRETVAL2=$?test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || { echo "Pls input two "num" again." exit 2}#no3[ $a -lt $b ] && { echo "$a < $b" exit 0}#no4[ $a -eq $b ] && { echo "$a = $b" exit 0}#no5[ $a -gt $b ] && { echo "$a > $b"}[root@oldboy C06]# cat 6_35_2.sh#!/bin/sha=$1b=$2#no1[ $# -ne 2 ] && { echo "USAGE:$0 NUM1 NUM2" exit 1}#no1expr $a + 10 &>/dev/nullRETVAL1=$?expr $b + 10 &>/dev/nullRETVAL2=$?test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || { echo "Pls input two "num" again." exit 2}#no3[ $a -lt $b ] && { echo "$a < $b" exit 0}#no4[ $a -eq $b ] && { echo "$a = $b" exit 0}#no5[ $a -gt $b ] && { echo "$a > $b"}[root@oldboy C06]# cat 6_36_1.sh#!/bin/shcat </dev/null[ $? -ne 0 ] && { echo "the num you input must be {1|2|3}" exit 1}[ $num -eq 1 ] && { echo "start installing lamp." sleep 2; [ -x "$path/lamp.sh" ] || { echo "$path/lamp.sh does not exist or can not be exec." exit 1 } $path/lamp.sh exit $?}[ $num -eq 2 ] && { echo "start installing LNMP." sleep 2; [ -x "$path/lnmp.sh" ] || { echo "$path/lnmp.sh does not exist or can not be exec." exit 1 } $path/lnmp.sh exit $?}[ $num -eq 3 ] && { echo bye. exit 3}#[[ ! $num =~ [1-3] ]]&&{[ ! $num -eq 1 -o ! $num -eq 2 -o ! $num -eq 3 ] && { echo "the num you input must be {1|2|3}" echo "Input ERROR" exit 4}
书不是看会的,而是不断练习会的!
看书过程中遇到勘误错误,可以发到下面地址。
转载地址:http://qyadx.baihongyu.com/