Linux 之 shell script -- 條件判斷式(if ... then)

條件判斷式

很多時候,我們都必須需要某些數據來判斷程序該如何進行,簡單的方式可以利用 &&||,但如果我們還想要執行一堆指令呢?就可以用if then 來幫忙啦(當符合某個條件判斷的時候,就予以進行某項工作),這個if ... then的判斷還有多層次的情況,這裏我們分別介紹來學習。條件判斷式除了if ... then之外,還有case ... esac 這種形式,不過因爲內容量太大,case ... esac我們放在下一篇文章裏學習。

1.利用 if ... then

1.1 單層、簡單條件判斷式

  • 語法結構:

    把上一篇Linux 之 shell script -- 善用判斷式
    提到的ans_yn.sh代碼改一改,就是一個簡單的條件判斷式
  • 原本模樣:
    [ "${input}" == "Y" -o "${input}" == "y" ]
  • 可以替換爲
    [ "${input}" == "Y" ] || [ "${input}" == "y" ]
  • 把 ans_yn.sh 升級爲 ans_yn_2.sh
    vi ans_yn_2.sh 輸入如下代碼,運行可以獲得ans_yn.sh同樣的效果。
#!/bin/bash
read -p "Please input 'Y' or  'N' to execute this program:" input
if [ "${input}" == "Y" ] || [ "${input}" == "y" ]; then
    echo "OK,continue"
    exit 0
fi
if [ "${input}" == "N" ] || [ "${input}" == "n" ]; then
     echo "Oh,interrupt!"
     exit 0
fi
echo "I don't know what your choice is" && exit 0

1.2 多重、複雜條件判斷式

在同一個數據的判斷中,如果該數據需要進行多種不同的判斷時,就需要更加複雜的判斷式了

  • 一個條件判斷,分成功進行與失敗進行(else)


  • 多個條件判斷(if ... elif ... elif ... else)分多種不同情況執行


注意:elif後面都要接then來處理(因爲elif也是個判斷式);else已經是最後的沒有成立的結果了,所以else後面並沒有then
我們把ans_yn.sh進一步改進,升級爲 ans_yn_3.sh

vi ans_yn_3.sh
輸入如下代碼再次運行,可以獲得與ans_yn.sh同樣的結果。

#!/bin/bash
read -p "Please input 'Y' or  'N' to execute this program:" input
if [ "${input}" == "Y" ] || [ "${input}" == "y" ]; then
    echo "OK,continue"
elif [ "${input}" == "N" ] || [ "${input}" == "n" ]; then
    echo "Oh,interrupt!"
else
    echo "I don't know what your choice is"
fi

仔細觀察會發現,ans_yn_3.sh相對於ans_yn_2.shans_yn.sh,已經不需要exit 0來控制程序的執行了,正是由於if ... elif ... elif ... else提供了 多條件判斷 的功能,將條件“Y/y”,"N/n"非“Y/y/N/n”根據優先級分了3個層次,讓${input}依次比對,符合哪一個條件,就只執行 該條件 對應的那個程序段。

可能上面一個例子不夠爽,我們再來一個練習,加深理解~

撰寫shell的要求:
(1)判斷shell script的參數$1是否爲 hello,如果是的話,就顯示“Hello,how are you”;
(2)如果沒有加任何參數,就提示使用者必須要使用的參數下達法;
(3)而如果加入的參數不是hello,就提醒使用者僅能使用hello爲參數。

這裏涉及shell script的默認參數,不理解的話可以查看Linux 之 shell script -- 善用判斷式中的 3.shell script 的默認變數

我們爲這個shell命名 hello-2.sh ,輸入如下代碼,然後在命令行中按照👇3種方式執行,看有怎樣的結果?
(1) sh hello-2.sh hello
(2) sh hello-2.sh hehe
(3) sh hello-2.sh

#!/bin/bash
if [ "$1" == "hello" ]; then
    echo "Hello,how are you?"
elif [ "$1" == "" ]; then
    echo "Please input the parameter, ex> {${0} someword}" #這裏的${0}是變量,指shell的文本名
else
    echo "Please input "hello" as the parameter, ex> {${0} someword}"
fi

2.Postscript

嗯,接下來又是一個撰寫shell的練習(if ... then練習系列),我覺得很有用,儘管不是很明白網絡服務端口是什麼意思,還是把這裏的代碼抄下來了。

2.1 網絡服務端口相關

  • netstat這個指令
    這個指令可以查詢到目前主機有開啓的網絡服務端口(service ports),我們可以利用netstat -tuln來取得目前主機有啓動的服務,借鳥哥的圖展示一下:

  • 幾個常見的port與相關網絡服務的關係
    -- 80: WWW
    -- 22: ssh
    -- 21: ftp
    -- 25:mail
    -- 111:RPC(遠程過程調用)
    -- 631:CUPS(打印服務功能)

2.2 查看網絡服務端口是否存在

#!/bin/bash
#1.先作一些告知的動作而已
echo "Now, I will detect your Linux server's services!"
echo -e "The www,ftp,ssh,and mail(smtp) will be detect! \n"

#2.開始進行一些測試的工作,並且也輸出一些信息
testfile=/mnt/user/renyp/learning/netstat_checking.txt
netstat -tuln > ${testfile} #先轉存數據到內存當中!不用一直執行 netstat
testing=$(grep ":80" ${testfile}) #偵測看 port 80 是否存在
if [ "${testing}" != "" ]; then
        echo "WWW is running in your system."
fi
testing=$(grep ":22" ${testfile}) #偵測看 port 22 是否存在
if [ "${testing}" != "" ]; then
        echo "SSH is running in your system."
fi
testing=$(grep ":21" ${testfile}) #偵測看 port 21 是否存在
if [ "${testing}" != "" ]; then
        echo "FTP is running in your system."
fi
testing=$(grep ":25" ${testfile}) #偵測看 port 25 是否存在
if [ "${testing}" != "" ]; then
        echo "Mail is running in your system."
fi
 

提示:由於 每個port是否存在的查詢 都是平行關係,沒有比較條件的優先級,所以都是if ... then完成,而不需要採用if ... elif ... elif ... else這種多條件判斷的方式。
還有,變量的邏輯運算用$();數值運算用$((計算式))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章