6.2bash編程入門之邏輯運算和條件判斷

實現某種操做:老是 測試(經過判斷條件來執行) 前提是否知足linux

    /tmp/test
        10shell

    邏輯運算:
        布爾運算有兩種狀態:真,假express

        與、或、非、異或編程

        與運算:  操做符  &&  特色:有一個操做數爲假時,結果必爲假。兩個操做數同時真時,才爲真
            真,假執行與運算:
                真 && 真 = 真
                真 && 假 = 假
                假 && 真 = 假
                假 && 假 = 假bash

        或運算: 操做符 ||
            真,假執行或運算     特色:有一個操做數爲真時,結果必爲真。兩個操做數同時假時,才爲假
                真 || 真 = 真
                真 || 假 = 真
                假 || 真 = 真
                假 || 假 = 假
---------------------------------------------------------------------
[root@localhost ~]# which --skip-alias ls &> /dev/null || [ $? -ne 0 ] && echo "No such command."
No such command.
[root@localhost ~]# which --skip-alias fdjfs &> /dev/null || [ $? -ne 0 ] && echo "No such command."
No such command.
which --skip-alias ls &> /dev/null || [ $? -ne 0 ] || [ $? -ne 0 ] && echo "No such command."
0  || #   = 0
                                   && echo "No such command."
                0  && 0     =  0
               
which --skip-alias fdjfs &> /dev/null || [ $? -ne 0 ] && echo "No such command."
#  || 0    = 0
                           && echo "No such command."
                   0  &&  0    =   0
因此這裏不論which --skip-alias fdjfs &> /dev/null爲真仍是爲假   都會進行 && 的運算的
---------------------------------------------------------------------測試

        非運算:
            真,假ui

        異或運算:
          相同爲假,不一樣則爲真this

    命令都有其狀態返回值:是有兩種狀態的
        成功:0,真
        失敗:1-255, 假lua

    bash條件測試:
        命令執行成功與否即爲條件測試
            test EXPR
[root@linux_basic scripts]#type test
test is a shell builtin
[root@linux_basic scripts]#help test
test: test [expr]
    Evaluate conditional expression.
   
    Exits with a status of 0 (true) or 1 (false) depending on
    the evaluation of EXPR.  Expressions may be unary or binary.  Unary
    expressions are often used to examine the status of a file.  There
    are string operators as well, and numeric comparison operators.
      兩端和中間是要有空白符的  
            [ EXPR ]
            [[ EXPR ]]spa

        比較運算:
            >, <, >=, <=, ==, !=

        測試類型:根據比較時的操做數的類型
            整型測試:整數比較
            字符測試:字符串比較
            文件測試:判斷文件的存在性及屬性等

            注意:比較運算一般只在同一種類型間進行

            整型測試操做符:
                -gt: 例如 [ $num1 -gt $num2 ]  大於
                -lt:  小於
                -ge:  大於等於
                -le:  小於等於
                -eq:  等於
                -ne:  不等於

            字符串測試:
                雙目
                    >: [[ "$str1" > "$str2" ]]
                    <:
                    >=
                    <=
                    ==
                    !=
          =~ 判斷左邊的字符串是否可以被右邊的模式所匹配,一般用於[[]];
          [[ "$vopt" = ~ pattern ]],通常作行首、行尾錨定,但模式不要加引號
         
                單目:
                      -n String: 是否不空,不空則爲真,空則爲假
                      -z String: 是否爲空,空則爲真,不空則假

    過程式編程:
        順序
        選擇
        循環:for

    選擇:if和case

    if: 三種使用格式
        單分支的if語句:
            if 測試條件; then
                 選擇分支
            fi
                表示條件測試狀態返回值爲真,則執行選擇分支;
                 !命令取反
                if ! id $username &> /dev/null; then  只須要狀態值,不須要返回值
                    useradd $username
                fi

            練習:寫一個腳本,接受一個參數,這個參數是用戶名;若是此用戶存在,則顯示其ID號;
自定義shell進程的狀態返回值:
    exit [n]
   
[root@linux_basic scripts]#type exit
exit is a shell builtin
[root@linux_basic scripts]#help exit
exit: exit [n]
    Exit the shell.  退出shell
   
    Exits the shell with a status of N.  If N is omitted, the exit status
    is that of the last command executed.   

        雙分支的if語句:
            if 測試條件; then
                選擇分支1
            else
                選擇分支2
            fi

            兩個分支僅執行其中之一。

            練習:經過命令行傳遞兩個整數參數給腳本,腳本能夠返回其大者。

            練習:經過命令行傳遞任意個整數給腳本,腳本能夠返回其大者。

            練習:經過命令行給定一個文件路徑,然後判斷:
                若是此文件中存在空白行,則顯示其空白行的總數;
                不然,則顯示無空白行;

                if grep "^[[:space]]*$" $1 &> /dev/null; then  只取狀態結果
                    echo "$1 has $(grep "^[[:space]]*$" $1 | wc -l) blank lines."
                else
                    echo "No blank lines"
                fi

                注意:若是把命令執行成功與否看成條件,則if語句後必須只跟命令自己,而不能引用。

                if [ $(grep "^[[:space:]]*$" $1 | wc -l) -lt 1 ]

        多分支的if語句:有多個測試條件
            if 條件1; then
                分支1
            elif 條件2; then
                分支2
            elif 條件3; then
                分支3
            ...
            else
                分支n
            fi

            練習:傳遞一個參數給腳本:   
                若是參數爲quit,則顯示說你要退出;
                若是參數爲yes,則顯示說你要繼續
                其它任意參數,則說沒法識別;

            練習:傳遞一個用戶名給腳本:
                若是此用戶的id號爲0,則顯示說這是管理員
                若是此用戶的id號大於等於500,則顯示說這是普通用戶
                不然,則說這是系統用戶;

                #!/bin/bash
                #
                if [ $# -lt 1 ]; then   1.必須得參數大於1判斷。
                    echo "Usage: `basename $0` username"
                    exit 1
                fi

                if ! id -u $1 &> /dev/null; then  2.執行條件必須存在判斷。
                    echo "Usage: `basename $0` username"
                    echo "No this user $1."
                    exit 2
                fi

                if [ $(id -u $1) -eq 0 ]; then
                    echo "Admin"
                elif [ $(id -u $1) -ge 500 ]; then
                    echo "Common user."
                else
                    echo "System user."
                fi

        if 測試條件; then
            測試條件:在bash中是命令 (test EXPR, [ EXPR ] ) 或由 [[ EXPR ]]
            if 命令;
                在bash運行至if時,其後的命令會被執行,其狀態結果則做爲判斷標準:
                    0:表示真
                    1-255:表示假

                若是條件包含比較之意,則必須使用

相關文章
相關標籤/搜索