shell學習筆記之六(測試和判斷)

來源:互聯網
上載者:User

標籤:

測試和判斷    測試        利用命令執行後的$?來判斷命令是否執行正常。$?==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學習筆記之六(測試和判斷)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.