1. 程式人生 > >shell邏輯判斷、文件屬性判斷、if特殊用法、case判斷

shell邏輯判斷、文件屬性判斷、if特殊用法、case判斷

case判斷 屬性 輸出 gre 取值 是否 期待 ash 報錯

shell 腳本中的邏輯判斷

邏輯表達式

在[](中括號)中使用:

  • -lt:=little than 小於
  • -le:=little && wqual 小於等於
  • -eq:=equal 等於
  • -ne:=no equal 不等於
  • -gt:=greater than 大於
  • -ge:=greater && equal 大於等於

在(())小括號中使用:

  • <,<=,==,!=,>,>=

需使用雙括號括起來

格式一

  • if 條件; then 語句;fi

    #!/bin/bash
    a=5
    if [ $a -gt 3 ]
    then
    echo "ok"

    fi
    ~
    // []中,變量和括號及邏輯表達式間有空格

格式二

  • if條件;then 語句1;else 語句2;fi

    #!/bin/bash
    a=5
    if [ $a -gt 3 ]
    then
    echo "ok"
    else
    echo "fault"
    fi

格式三

  • if條件;then 語句1;elif 條件2;then 語句2;else 語句3;fi

    #!/bin/bash
    a=5
    if [ $a -lt 3 ]
    then
    echo "a<3"
    elif [ $a -gt 6 ]
    then
    echo "a>6"
    else
    echo "nook"

    fi

關系

  • 各個條件之間的關系可以使用邏輯連接符
    • 條件A&&條件B:A並且B
    • 條件A||條件B:A或者B

文件目錄屬性判斷

  • shell腳本中if經常用於判斷文檔的屬性,比如判斷是普通文件還是目錄文件,判斷文件是否有讀寫執行權限等。

文件目錄屬性判斷選項

  • -e:判斷文件或目錄是否存在
  • -d:判斷是不是目錄文件以及是否存在
  • -f:判斷是不是普通文件以及是否存在
  • -r:判斷是否有讀權限
  • -w:判斷是否有寫權限
  • -x:判斷是否有執行權限

    #!/bin/bash
    f="/tmp/test/123456"
    if [ -e $f ]
    then
    echo "ok"

    else
    echo "no"
    fi
    // 判斷這個文件或目錄是否存在,存在返回ok,不存在返回no

if 特殊用法

  • if [ -z "$a" ]:表示當變量a的值為空時會怎樣

  • if [ -n "$a" ]:表示當變量a的值不為空時會怎樣

  • -n和-z,都不能作用在文件上。只能作用於變量。

  • -z和-n為相反的兩個條件

    #!/bin/bash
    n=wc -l /tmp/test.txt
    if [ $n -gt 20 ]
    then
    echo 1
    else
    echo 0
    fi
    // 在該腳本中無語法錯誤,只是我們預設/tmp/test.txt是存在的
    // 如果該文件不存在,該腳本執行的時候就會報錯

    [root@localhost shell]# sh file.sh
    wc: /tmp/test.txt: 沒有那個文件或目錄
    if.sh: 第 3 行:[: -gt: 期待一元表達式

    // 所以,為了避免這種錯誤的發生,需要將腳本寫的更加嚴謹
    // 需要在執行“if [ $n -gt 20 ]”之前先確認文件“/tmp/test.txt”是否存在

    #!/bin/bash
    n=wc -l /tmp/test.txt
    if [ -z $n ]
    then
    echo "error"
    exit
    // 如果文件不存在執行到這裏將會推出腳本
    // 下面的將不在執行
    elif [ $n -lt 20 ]
    then
    echo 1
    else
    echo 0
    fi

    執行結果:
    [root@localhost shell]# sh if.sh
    wc: /tmp/test.txt: 沒有那個文件或目錄
    error

  • if grep ‘123‘ test.txt;then 表示當test.txt中含有123會怎樣

    判斷某參數不存在
    #!/bin/bash
    if
    grep -wq ‘user1‘ /etc/passwd
    then
    echo "user1 exist."
    fi
    [root@localhost sbin]# sh if1.sh

    判斷某參數不存在:
    #!/bin/bash
    if
    ! grep -wq ‘user1‘ /etc/passwd
    then
    echo "no user1"
    fi

    // -w:精準匹配,過濾一個單詞
    // -q:安靜模式,不打印過濾結果
    // !:取反

    grep 非安靜模式:
    [root@localhost shell]# ./file.sh
    user1:x:1005:1006::/home/user1:/bin/bash
    user1 exist.

    grep 安靜模式:
    [root@localhost shell]# ./file.sh
    user1 exist.

case 判斷

  • case語句為多選語句,可以用case語句匹配一個值與一個模式,如果匹配成功,執行相匹配的命令。

    case 值($變量名) in
    value1)
    command
    ;;
    value2)
    command
    ;;
    value3)
    command
    ;;
    *)
    command
    ;;
    esac

  • case 的工作方式如上所示,取值後面必須加in,每一個模式必須以有括號結束。取值將檢測匹配的每一個模式。一但模式匹配,則執行完匹配模式相應命令後不再繼續其他模式。

  • 如果沒有匹配到任何一個模式,則執行*模式後的命令。

  • 在case中,可以在條件中使用|,表示或的意思

    2|3)
    command
    ;;

  • 實例

    #!/bin/bash
    read -p "please input a number:" n
    if [ -z "$n" ]
    then
    echo "null,please input a number."
    exit 1
    // exit 1 表示執行該部分命令後的返回值
    // 即,命令執行完後使用echo $? 的值
    fi

    n1=echo $n | sed ‘s/[0-9]//g‘
    // 將變量n中的數字換成空,賦值給n1
    if [ -n "$n1" ]
    // 判斷,如果n1不為空,則輸出下面結果,為空則繼續往下執行
    then
    echo "please input a number."
    exit 1
    fi

    if [ $n -lt 60 ] && [ $n -ge 0 ]
    then
    tag=1
    elif [ $n -ge 60 ] && [ $n -lt 80 ]
    then
    tag=2
    elif [ $n -ge 80 ] && [ $n -lt 90 ]
    then
    tag=3
    elif [ $n -ge 90 ] && [ $n -lt 100 ]
    then
    tag=4
    else
    tag=0
    fi
    // tag是變量名,作為一個標簽,方便引用。
    case $tag in
    1)
    echo "D"
    ;;
    2)
    echo "C"
    ;;
    3)
    echo "B"
    ;;
    4)
    echo "A"
    ;;
    *)
    echo "the number is range 0-100"
    ;;
    esac

    case $tag in
    1)
    echo "D"
    ;;
    2)
    echo "C"
    ;;
    3)
    echo "B"
    ;;
    4)
    echo "A"
    ;;
    *)
    echo "the number is range 0-100"
    ;;
    esac

    // 該腳本作用是輸入考試成績,判斷考試成績的等級

  • 測試

    [root@localhost shell]# ./case.sh
    please input a number:95
    A
    [root@localhost shell]# ./case.sh
    please input a number:85
    B
    [root@localhost shell]# ./case.sh
    please input a number:75
    C
    [root@localhost shell]# ./case.sh
    please input a number:55
    D
    [root@localhost shell]# ./case.sh
    please input a number:101
    the number is range 0-100
    [root@localhost shell]# ./case.sh
    please input a number:7y
    ZM,please input a number.
    [root@localhost shell]# ./case.sh
    please input a number:
    null,please input a number.

shell邏輯判斷、文件屬性判斷、if特殊用法、case判斷