鳥哥的Linux私房菜讀書筆記--條件判斷式
1、利用if ... then
<1>單層、簡單的條件判斷式
if [ 條件判斷式 ]; then 當條件判斷式成立時,可以進行的指令工作內容;
fi <==將 if 反過來寫,就成為 fi 啦!結束 if 之意!
如果有多個條件需要進行判別時,除ans_yn.sh案例縮寫外,即將多個條件寫入以箇中括號內的情況,我們還可以用多箇中括號隔開,括號與括號間以&&(and)或 || (or)隔開。
修改: [ "${yn}" == "Y" -o "${yn}" == "y" ]
上式可替換為
[ "${yn}" == "Y" ] || [ "${yn}" == "y" ]
#!/bin/bash #program # this program shows the user's choice #history # 20181010 lile first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp export PATH echo -e "you should enter Y or N\n" read -p "please input Y/N to express your think:" yn if [ "${yn}" == "y" ] || [ "${yn}" == "Y"] ;then echo "ok,will eontinue" exit 2 fi if [ "${yn}" == "n" ] || [ "${yn}" == "N"] ;then echo "no.will stop" exit 8 fi echo "I don't know whar your choice is " && exit 0
<2>多重、複雜條件判斷
在通一個數據的判斷中,如果該資料需要進行多種不同的判斷,可以使用如下方法。
格式: #一個條件判斷式分成功進行與失敗進行(else)
if [ 條件判斷式 ];then #當條件成立時進行指令工作內容
else #當條件不成立時,執行該指令工作內容
fi
更復雜的情況如下
多個條件判斷 (if ... elif ... elif ... else) 分多種不同情況執行
if [ 條件判斷式一 ]; then # 當條件判斷式一成立時,可以進行的指令工作內容;
elif [ 條件判斷式二 ]; then #當條件判斷式二成立時,可以進行的指令工作內容;
else #當條件判斷式一與二均不成立時,可以進行的指令工作內容;
fi
例:當用戶輸入hello時,利用引數的方法可以進行判斷$1是否為hello,如果是顯示“hello,how are you ?”,如果沒有加任何引數,提示使用者必須使用引數下達法,而如果加入的引數不是hello,提醒使用者只能使用hello作為引數。
#!/bin/bash
#program
# this program will check $1 is egual to hello
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
if [ "${1}" == "hello" ] ;then
echo "hello how are you !"
elif [ "${1}" == "" ] ;then
echo "please input your parameter,example {${0} haha}"
else
echo "the only parameter is hello,example {${0} hello}"
fi
我們可以通過netstat -tuln來取得當前主機有啟動的服務,在執行指令後獲得的資訊重點為local address(本地主機的IP與埠對應)ip的部分說明的是該服務位於那個介面上,0.0.0.0或:::代表對整個網路開放,以下為常見的port與相關網路服務的關係。80:www 22:ssh 21:ftp 25:mail 111:RPC(遠端過程呼叫) 631:CUPS(列印服務功能)
問:偵測主機是否開啟相關的主要網路服務埠。
#!/bin/bash
#program
# using netstat and grep to detect www,ssh,ftp,and mail sevices.
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
echo "I will detect your linux server's services \n"
echo "the www,ftp,ssh and mail(smtp) will be detect \n"
testfile=~/temp/netstat_checking.txt
netstat -tuln > ${testfile}
testing=$(grep ":80" ${testfile})
if [ "${testing}" != "" ];then
echo "www is running in your system"
fi
testing=$(grep ":22" ${testfile})
if [ "${testing}" != "" ];then
echo "ssh is running in your system"
fi
testing=$(grep ":21" ${testfile})
if [ "${testing}" != "" ];then
echo "ftp is running in your system"
fi
testing=$(grep ":25" ${testfile})
if [ "${testing}" != "" ];then
echo "mail is running in your system"
fi
2、利用case……esac判斷
case $變數名稱 in <==關鍵詞為 case ,還有變數前有$
"第一個變數內容") <==每個變數內容建議用雙引號括起來,關鍵詞則為小括號 )
程式段
;; <==每個類別結尾使用兩個連續的分號來處理!
"第二個變數內容")
程式段
;;
*) <==最後一個變數內容都會用 * 來代表所有其他值
不包含第一個變數內容與第二個變數內容的其他程式執行段
exit 1
;;
esac <==最終的 case 結尾!『反過來寫』思考一下!
hello_2.sh程式使用case語句改寫如下
#!/bin/bash
#program
# this program will check $1 is equal to hello
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
case ${1} in
"hello")
echo "hello how are you !"
;;
"")
echo "please input your parameter,example {${0} haha}"
;;
*) #*代表任意字元,不需要“”
echo "the only parameter is hello,example ${0} {hello}"
;;
esac
一般來說,在使用case $變數 in 語法中,其中的變數的取得方式有兩種
(1)直接下達式:如上使用的$1變數
(2)互動式:透過read指令下達。
例:讓使用者能夠輸入one,two,three並且將使用者的變數顯示到螢幕上,如果不是其中之一,告知使用者僅有這三種選擇。
#!/bin/bash
#program
# this script only accepts the flowing parameter:one two three
#histpry
# 20181011 lile first release
PATH=/bin:/sbin;/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
echo "this program will print you selection"
read -p "input your number:" num #互動式
case ${num} in
"one")
echo "your choice is one"
;;
"two")
echo "your choice is two"
;;
"three")
echo "your choice is three"
;;
*)
echo "only usage "
;;
esac
3、利用function功能函式
函式在shell script中可以做出一個類似於定義指令執行的東西,最大的功能在於簡化很多的西程式程式碼。上一個程式利用function進行簡化。
function的語法如下:
function fname(){
程式段
}
其中fname是自定義的執行指令名稱,程式段即為我們需要執行的命令。注意,在shell script的執行方式是由上而下、由左至右,因此shell script的function的設定一定要在程式的最前面,由此才能夠在執行時被可用的程式找到
例:用function編寫上一script
#!/bin/bash
#program
# use function to repeat information
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
function print123() ##定義函式print123
{
echo -n "your choice is :"
}
echo -e "this program will print your selection\n"
case ${1} in
"one")
print123;echo -e ${1} | tr 'a-z' 'A-Z'
##呼叫函式print123 將引數的小寫轉換為大寫(tr)
;;
"two")
print123; echo -e ${1} | tr 'a-z' 'A-Z'
;;
"thre")
print123; echo -e ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "${0} only can input one|two|three,prlease x again "
;;
esac
除此之外,function還具有內建變數,它的內建變數與shell script很類似,函式名稱代表$0,而後續接的變數可以以$1,$2……來進行取代。
#!/bin/bash
#program
# use function to repeat information
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
function print123()
{
echo -n "your choice is :${1}" ##實驗時在加\n時無法換行
}
echo "this program will print your selection\n"
case ${1} in
"one")
print123 1 ##${1}表示的是自定義函式後的引數即1,2,3
;;
"two")
print123 2
;;
"thre")
print123 3
;;
*)
echo "${0} only can input one|two|three,prlease x again " ##${0}表示script名
;;
esac