Bash If語句

在本小節中,我們將瞭解如何在Bash腳本中使用if語句來完成自動化任務。

if語句用於在順序執行語句的流程中執行條件任務。If語句通常用於在Bash腳本中做出決定。它們根據可能設置的條件來決定是否運行一段代碼。

1. if語句基礎

基本if語句命令判斷如果特定條件為true,則僅執行給定的一組操作。如果條件為false,則不要執行這些操作。if語句基本語法格式如下:

if [ expression ];
then
statements
fi

僅當運算式(在方括號之間)為真時,才會執行thenfi之間的語句。

注意:觀察第一行中使用的空格,在第一行末尾使用分號。兩者都必須使用,if條件語句以fi結尾。

  • 使用AND運算符使用多個條件:

    if [ expression_1 ] && [ expression_2 ];
    then
    statements
    fi
    
  • 使用OR運算符使用多個條件:

    if [ expression_1 ] || [ expression_2 ];
    then
    statements
    fi
    
  • 對於具有AND&OR運算符的複合運算式,可以使用以下語法:

    if [ expression_1 && expression_2 || expression_3 ];
    then
    statements
    fi
    

以下是一些演示if語句用法的示例:

示例1

在此示例中,讀取用戶輸入任意數字,並檢查該值是否大於100

腳本檔:if-example1.sh

#!/bin/bash

read -p " Enter number : " number

if [ $number -gt 100 ]
then
echo "Value is greater than 100"
fi

執行上面示例代碼,得到以下結果:

示例2

在此示例中,通過比較兩個字串的值,簡單演示了if語句的用法。
腳本檔:if-example2.sh

#!/bin/bash

# if condition is true
if [ "zaixian" == "zaixian" ];
then
echo "true condition"
fi

# if condition is false
if [ "xuhuhu.com" == "xntutor.com" ];
then
echo "false condition"
fi

執行上面示例代碼,得到以下結果:

示例3

在此示例中,演示如何使用if語句比較數字。
腳本檔:if-example3.sh

#!/bin/bash

#if condition (greater than) is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
fi

#if condition (greater than) is false
if [ 3 -gt 10 ];
then
echo "3 is not greater than 10."
fi

#if condition (lesser than) is true
if [ 3 -lt 10 ];
then
echo "3 is less than 10."
fi

#if condition (lesser than) is false
if [ 10 -lt 3 ];
then
echo "10 is not less than 3."
fi

#if condition (equal to) is true
if [ 10 -eq 10 ];
then
echo "10 is equal to 10."
fi

#if condition (equal to) is false
if [ 10 -eq 9 ];
then
echo "10 is not equal to 9"
fi

執行上面示例代碼,得到以下結果:
示例3

示例4

在此示例中,將演示如何在if運算式中使用AND運算符包括多個條件。

腳本檔:if-example4.sh

#!/bin/bash

# TRUE && TRUE
if [ 8 -gt 6 ] && [ 10 -eq 10 ];
then
echo "Conditions are true"
fi

# TRUE && FALSE
if [ "mylife" == "mylife" ] && [ 3 -gt 10 ];
then
echo "Conditions are false"
fi

執行上面示例代碼,得到以下結果:

maxsu@ubuntu:~/bashcode$ vi if-example4.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-example4.sh
maxsu@ubuntu:~/bashcode$ ./if-example4.sh
Conditions are true
maxsu@ubuntu:~/bashcode$

示例5

在此示例中,將演示如何在if運算式中使用OR運算符包括多個條件。
腳本檔:if-example5.sh

#!/bin/bash

# TRUE || FALSE
if [ 8 -gt 7 ] || [ 10 -eq 3 ];
then
echo " Condition is true. "
fi

# FALSE || FALSE
if [ "mylife" == "yourlife" ] || [ 3 -gt 10 ];
then
echo " Condition is false. "
fi

執行上面示例代碼,得到以下結果:

maxsu@ubuntu:~/bashcode$ vi if-example5.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-example5.sh
maxsu@ubuntu:~/bashcode$ ./if-example5.sh
 Condition is true.

示例6

在此示例中,將演示如何在if運算式中使用ANDOR包括多個條件:
腳本檔:if-example6.sh

#!/bin/bash

# TRUE && FALSE || FALSE || TRUE
if [[ 10 -eq 10 && 5 -gt 4 || 3 -eq 4 || 3 -lt 6 ]];
then
echo "Condition is true."
fi

# TRUE && FALSE || FALSE
if [[ 8 -eq 8 && 8 -gt 10 || 9 -lt 5 ]];
then
echo "Condition is false"
fi

執行上面示例代碼,得到以下結果:

maxsu@ubuntu:~/bashcode$ vi if-example6.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-example6.sh
maxsu@ubuntu:~/bashcode$ ./if-example6.sh
Condition is true.

2. Bash If語句選項

if語句包含許多執行特定任務的選項。這些選項可用於檔操作,字串操作等。以下是一些最常用的選項:

選項(操作符) 描述
! EXPRESSION 檢查EXPRESSION是否為假。
-n STRING 檢查STRING的長度是否大於零。
-z STRING 檢查STRING的長度是否為零(即為空)
STRING1 == STRING2 檢查STRING1是否等於STRING2
STRING1 != STRING2 檢查STRING1是否不等於STRING2
INTEGER1 -eq INTEGER2 檢查INTEGER1在數值上是否等於INTEGER2
INTEGER1 -gt INTEGER2 檢查INTEGER1在數值上是否大於INTEGER2
INTEGER1 -lt INTEGER2 檢查INTEGER1在數值上是否小於INTEGER2
-d FILE 檢查FILE是否存在並且它是一個目錄。
-e FILE 檢查FILE是否存在。
-r FILE 檢查FILE是否存在,並授予讀取許可權。
-s FILE 檢查FILE是否存在並且其大小大於零(表示它不為空)。
-w FILE 檢查FILE是否存在並授予寫許可權。
-x FILE 檢查FILE是否存在並授予執行許可權。

3. 嵌套if

可以在bash腳本中根據需要應用多個if語句。也可以在一個if語句中使用另一個if語句。這種情況稱為嵌套If語句。

示例
在此示例中,將通過使用嵌套的if運算式來找到“給定數字是否大於50,並且是否為偶數”。
腳本檔:if-nested.sh

#!/bin/bash
#Nested if statement

if [ $1 -gt 50 ]
then
  echo "Number is greater than 50."

  if (( $1 % 2 == 0 ))
  then
    echo "and it is an even number."
  fi
fi

執行上面示例代碼,得到以下結果:

maxsu@ubuntu:~/bashcode$ vi if-nested.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-nested.sh
maxsu@ubuntu:~/bashcode$ ./if-nested.sh 100
Number is greater than 50.
and it is an even number.

上一篇: Bash算術運算符 下一篇: Bash if-else語句