標籤:
測試和判斷 測試 利用命令執行後的$?來判斷命令是否執行正常。$?==0 ? 正常:錯誤 測試結構: 1、test expression 2、[ expression ] #注意運算式兩側的空格 方式2增加了代碼的可讀性,且更容易與if,case,while這些條件判斷的關鍵字聯用。 檔案測試: 1、test file_operator FILE 2、[ file_operator FILE ] 例: test -e /var/log/message echo $? #檔案存在返回0,不存在返回非0值 [ -e /var/log/message ] echo $? 檔案比較符 -b 檔案存在且是塊檔案 -c 檔案存在且是字元裝置 -e 檔案或目錄是否存在 -d 檔案存在且是目錄 -f 檔案存在且是普通檔案 -x 檔案存在且是可執行檔 -w 檔案存在且為可寫檔案 -r 檔案存在且為可讀檔案 -l 檔案存在且為串連檔案 -p ...管道檔案 -S ...socket檔案 -s 大小不為0 FILE1 nt FILE2 FILE1比FILE2新返回真 FILE1 ot FILE2 FILE1比FILE2舊返回真 例:測試檔案的讀,寫,執行屬性 #!/bin/bash read -p "input a filename which you want to test:" filename if [ ! -e "$filename" ];then echo "The file doesn‘t exist." exit 1 fi if [ -w "$filename" ];then echo "The file is writeable." fi if [ -r "$filename" ];then echo "The file is readable." fi if [ -x "$filename" ];then echo "The file is executable" fi 注意: if [ expression ] 中if和[]之間要有空格 字串測試: 字串比較(字典序)主要有大於,小於,等於,不等於,空,不空 > < = != -z -n 例: str="" test -z "$str" echo $? #0 test -n "$str" echo $? #1 test -n $str echo $? #0,最好加上雙引號("") str="hello" [ -z "$str" ] echo $? #1 [ -n "$str" ] echo $? #0 str1="123" str2="234" [ "$str1" = "$str2"] #test $str1 = $str2,注意空格,否則一直返回0 echo $? #1 [ "$str1" != "$str2" ] echo $? [ "$str1" \> "$str2" ] echo $? [ "$str1" \< "$str2" ] echo $? [[ "$str1" > "$str2" ]] #不使用逸出字元 echo $? 整數比較 -eq = -lt < -gt > -le <= -ge >= -ne != 1、test num1 num_operator num2 2、[ num1 num_operator num2 ] 例: num1=10 num2=10 num3=9 num4=11 #測試相等 [ $num1 -eq $num2] echo $? test $num3 -eq $num2 echo $? 注意:一定要注意空格,否則得到的有可能錯誤 邏輯測試符和邏輯運算子 邏輯與,邏輯或,邏輯非 -a -o ! 非:!expression 與:expression1 -a expression2 或:expression1 -o expression2 && || ! [ -e /var/log/message ] && [ -e /var/log/message01 ] =[ -e /var/log/message -a -e /var/log/message01 ] [ -e /var/log/message ] || [ -e /var/log/message01 ] =[ -e /var/log/message -o -e /var/log/message01 ] 注意兩種不同的寫法 判斷 if語句 if expression;then command1 command2 ... fi 例:判斷學產生績 #!/bin/bash echo -n "input a score of a student:" #-n的意思是不換行 read score if [ $score -lt 60 ];then echo "D" fi if [ $score -lt 80 -a $score -ge 60 ];then echo "C" fi if [ $score -lt 90 -a $score -ge 80 ];then echo "B" fi if [ $score -le 100 -a $score -ge 90 ];then echo "A" fi if [ $score -gt 100 ];then echo "the number is invalid" fi 學過進階語言之後,其實就是學習shell的文法和編程特色,進階語言注重於大型項目,而shell注重於工作的便利,讓工作自動化 if/else語句 if expression ;then command else command fi 例:判斷一個檔案是否存在 #!/bin/bash echo -n "input a filename:" read filename if [ -e $filename ];then echo "file exists." else echo "file doesn‘t exists." fi if/elif/else 多重的if/else語句 例:修改學產生績指令碼 #!/bin/bash echo -n "input a score of a student:" read score if [ $score -lt 60 ];then echo "D" elif [ $score -lt 80 ];then echo "C" elif [ $score -lt 90 ];then echo "B" elif [ $score -le 100 ];then echo "A" elif [ $score -gt 100 ];then echo "the number is invalid" fi case 相當於switch...case 結構: case VAR in var1) command ;; var2) command ;; var3) command ;; ... *) command ;; 注意:case的var1、var2、var3等這些值只能是常量或Regex,還能是正則啊,不錯 例:檢測當前系統的類型 #!/bin/bash type=`uname -s` #print the kernel name case $type in L*) echo "Linux" ;; SunOS) echo "SunOS" ;; Darwin) echo "Darwin" ;; *) echo "other" ;; esac 檢測使用者輸入中是否含有大寫字母,小寫字母或者數字 #!/bin/bash echo -n "Input a string: " read str case $str in *[[:lower:]]*) echo "The string contains lower letters." ;; *[[:upper:]]*) echo "The string contains upper letters." ;; *[[:digit:]]*) echo "The string contains digit." ;; *) echo "other" ;; esac
shell學習筆記之六(測試和判斷)