linux之判断式
1、判斷基礎(chǔ)知識(shí)
當(dāng)要檢測(cè)某個(gè)文件或者文件d的相關(guān)屬性,可以利用 test 命令
[root @loacalhost ~]#test -e /vitest使用 test 命令檢測(cè) 目錄vitest是否存在
但是不會(huì)顯示任何結(jié)果,因此進(jìn)行改進(jìn)為有“參數(shù)”輸出的形式
[root @loacalhost ~]#test -e /vitest && echo "The dir exist" || echo "Not exist"給出標(biāo)志判斷表
除了使用test之外,還可以使用 [] 判斷符號(hào)進(jìn)行判斷
[root @loacalhost ~]#[ -z "$HOME" ];echo %?注意:因?yàn)橹欣ㄌ?hào) [] 使用的地方很多,常見(jiàn)的有通配符和正則表達(dá)式,所以在bash語(yǔ)法重法中使用[] 作為shell的判斷時(shí),必須中括號(hào)[]的兩端需要有空格來(lái)分割
1、中括號(hào) [] 內(nèi)的每個(gè)組件都需要空格鍵來(lái)分割
2、中括號(hào) [] 內(nèi)的變量,最好都以雙引號(hào)括起來(lái)
3、中括號(hào) [] 內(nèi)的常量,最好以雙引號(hào)或者單引號(hào)括起來(lái)
[root @loacalhost ~]#name="rhx" #變量賦值時(shí)=左右兩側(cè)不能有空格 [root @loacalhost ~]#[ $name == "HOME" ] #判斷時(shí)=的左右兩側(cè)必須有空格2、條件判斷式
條件判斷式即是滿足某個(gè)條件時(shí)執(zhí)行一個(gè)動(dòng)作,不滿足會(huì)執(zhí)行相應(yīng)的其他動(dòng)作,下面一一介紹
a、單層 if...then 判斷
if [ 條件判定式 ];then #注意條件判定式左右兩側(cè)有空格action #條件成立時(shí),執(zhí)行action中的操作 fi #結(jié)束if判斷,相當(dāng)于是if左右翻轉(zhuǎn)示例如下:
#!/bin/bash #program: # This program shows "Hello World!" in your screen #History: #205/08/3 rhx First Release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATHread -p "Please input (Y/N): " ynif [ "$yn" == "Y" ] || [ "$yn" == "y" ];thenecho "Ok ,continue"exit 0 fiif [ "$yn" == "N" ] || [ "$yn" == "n" ];thenecho "Oh interrupt"exit 0 fiecho "I do not know what your choice is "&& exit 0b、多重復(fù)雜判斷 if elif else
if [ 條件判定式1 ];then #注意條件1判定式左右兩側(cè)有空格action1 #條件成立時(shí),執(zhí)行action1中的操作 elif [ 條件判定式2 ];then action2 #條件2成立時(shí),執(zhí)行action2中的操作 fi #結(jié)束if判斷,相當(dāng)于是if左右翻轉(zhuǎn) #!/bin/bash #program: # This program shows "Hello World!" in your screen #History: #205/08/3 rhx First Release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATHread -p "Please input (Y/N): " ynif [ "$yn" == "Y" ] || [ "$yn" == "y" ];thenecho "Ok ,continue" elif [ "$yn" == "N" ] || [ "$yn" == "n" ];thenecho "Oh interrupt" fi在shell scripts中,scritpt針對(duì)參數(shù)有一些特殊設(shè)置,參數(shù)的對(duì)應(yīng)關(guān)系如下,$0表示文件名
/tmp/scrpts opt1 op2 opt3 opt4$0 $1 $2 $3 $4 #!/bin/bash #program: # This program shows "Hello World!" in your screen #History: #205/08/3 rhx First Release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH#read -p "Please input (Y/N): "ynif [ "$1" == "hello" ];thenecho "Hello,how are you ?" elif [ "$1" == "" ];thenecho "You MUST input parameters,ex> {$0 someword}" elseecho "The only parameters is 'hello',ex> {$0 hello}" fic、case ...esac
case $variable in #關(guān)鍵字case,變量variable前有符號(hào)$"value1") #每個(gè)變量的內(nèi)容建議用雙括號(hào)括選起來(lái),關(guān)鍵字外面為小括號(hào)),注意這里不同于常見(jiàn)的c/c++語(yǔ)言;; #每個(gè)類型結(jié)束使用連續(xù)兩個(gè)分號(hào)"value2";;*) #最后一個(gè)變量?jī)?nèi)容會(huì)用*來(lái)表示其他的值,像c/c++中的default選項(xiàng);; esac #case語(yǔ)法結(jié)束示例如下:
#!/bin/bash #program: # This program shows "Hello World!" in your screen #History: #205/08/3 rhx First Release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATHfunction printit(){echo -n "Your choice is " }echo "This program will print your selection!"case $1 in"one")printit;echo $1 | tr 'a-z' 'A-Z';;"two")printit;echo $1 | tr 'a-z' 'A-Z'"three")printit;echo $1 | tr 'a-z' 'A-Z';; *)echo "Usage $0 {one|two|three}";; esacd、利用 function 功能
function funcname () {程序段 }注意function中的$0,$1,$2與script中有所不同
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: Linux之交互式scripts
- 下一篇: linux之循环