如何在 Bash 腳本中使用強大的 Linux test 命令

Linux test 命令是 Shell 內置命令,用來檢測某個條件是否成立。test 通常和 if 語句一起使用,並且大部分 if 語句都依賴 test。可以將一個元素與另一個元素進行比較,但它更常用於 BASH shell 腳本中,作爲控制邏輯和程序流程 的條件語句的一部分。

test 命令有很多選項,可以進行數值、字符串和文件三個方面的檢測。

一個基本的例子

在終端窗口中嘗試這些命令。

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ test 1 -eq 2 && echo "yes" || echo "no"
no

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                   
⚡ test 1 -eq 1 && echo "yes" || echo "no"
yes

上面的命令分解如下:

小貼士:從 shell 提示符運行,test 不會將值返回到標準輸出,它僅返回退出狀態代碼。這就是爲什麼需要鏈接 echo 命令的原因。

本質上一樣的,該命令將 1 與 2 進行比較,如果它們匹配,則執行 echo“yes”語句並顯示 “yes”,如果它們不匹配,則執行 echo“no” 語句,顯示“no”。

比較數字

如果要比較解析爲數字的元素,可以使用以下比較運算符:

示例測試

test 1 -eq 2 && echo "yes" || echo "no"

(在屏幕上顯示 “no”,因爲 1 不等於 2)

test 1 -ge 2 && echo "yes" || echo "no"

(在屏幕上顯示 “no”,因爲 1 不大於或等於 2)

test 1 -gt 2 && echo "yes" || echo "no"

(在屏幕上顯示 “no”,因爲 1 不大於 2)

test 1 -le 2 && echo "yes" || echo "no"

(在屏幕上顯示 “yes”,因爲 1 小於或等於 2)

test 1 -lt 2 && echo "yes" || echo "no"

(在屏幕上顯示 “yes”,因爲 1 小於或等於 2)

test 1 -ne 2 && echo "yes" || echo "no"

(在屏幕上顯示 “yes”,因爲 1 不等於 2)

比較文本

比較解析爲字符串的元素時,請使用以下比較運算符:

例子

test "string1" = "string2" && echo "yes" || echo "no"

(在屏幕上顯示 “no”,因爲“string1” 不等於“string2”)

test "string1" != "string2" && echo "yes" || echo "no"

(在屏幕上顯示 “yes”,因爲“string1” 不等於“string2”)

test -n "string1" && echo "yes" || echo "no"

(在屏幕上顯示 “yes”,因爲“string1” 的字符串長度大於零)

test -z "string1" && echo "yes" || echo "no"

(在屏幕上顯示 “no”,因爲“string1” 的字符串長度大於零)

比較文件

比較文件時,請使用以下比較運算符:

例子

⚡ test linuxmi -nt linux && echo "yes"

(如果 linuxmi 比 linux 新,則顯示 “yes” 字樣,如上圖)

⚡ test -e /home/linuxmi/linuxmi && echo "yes"

(如果 linuxmi 存在,將顯示 “yes”)

test -O /home/linuxmi/linuxmi && echo "yes"

(如果您擁有 file1,則顯示 “yes” 字樣”)

塊特殊:文件是塊設備,這意味着數據以字節塊的形式讀取。這些通常是設備文件,例如硬盤驅動器。
特殊字符:文件在您寫入時立即執行,通常是串行端口等設備

比較多個條件

到目前爲止,一切都在將一件事與另一件事進行比較,但是如果您想比較兩個條件怎麼辦?

例如,如果一隻動物有四條腿並且會發出 “哞哞” 的聲音,它可能是一頭奶牛。簡單地檢查四隻腿並不能保證你有一頭牛,但檢查它發出的聲音肯定可以。

要同時測試這兩個條件,請使用以下語句:

test 4 -eq 4 -a "moo" = "moo" && echo "it is a cow" || echo "it is not a cow"

這裏的關鍵部分是 - a 標誌,它代表 and。

有一種更好和更常用的方法來執行相同的測試,如下所示:

test 4 -eq 4 && test "moo" = "moo" && echo "it is a cow" || echo "it is not a cow"

test 命令的分支很重要。如果第一個測試 (4 = 4) 失敗,則 test 命令以非零退出代碼終止。因此,我們跳轉到雙管道符號並且 “it is not a cow” 打印到標準輸出。但是,如果第一個測試成功並因此 test 導致退出代碼 0,那麼我們跳轉到第一個雙與號(&&)。下一條語句是另一個測試條件!

如果第二次 test 失敗,我們再次跳到雙管並從那裏繼續。然而,如果第二個 test 成功,我們跳轉到第二個雙 & 語句,在這個例子中,它只是將 “it is a cow” 回顯到標準輸出,然後終止返回到 shell 提示符。

另一個測試比較兩個語句,如果其中一個爲真,則輸出一個字符串。例如,要檢查是否存在名爲 “linuxmi.txt” 的文件或名爲 “linuxmi.py” 的文件,可以使用以下命令:

這裏的關鍵部分是 - o 代表 or。

test -e linuxmi.txt -o -e linuxmi.py && echo "linuxmi exists" || echo "linuxmi does not exist"

有一種更好和更常用的方法來執行相同的測試,如下所示:

test -e linuxmi.txt || test -e linuxmi.py && echo "linuxmi exists" || echo "linuxmi does not exist"

排除 test 關鍵字

您實際上不需要使用單詞 test 來執行比較。您所要做的就是將語句括在方括號中,如下所示:

⚡ [ -e linux.py ] && echo "linux.py exists" || echo "file1 does not exist"
linux.py exists

[and] 基本上與 test 含義 相同。

現在您知道這一點,您可以改進比較多個條件,如下所示:

[ 4 -eq 4 ] && [ "moo" = "moo" ] && echo "it is a cow" || echo "it is not a cow"

[ -e linuxmi.py ] || [ -e linuxmi.txt ] && echo "linuxmi exists" || echo "linuxmi does not exist"

總結

test 命令在腳本中更有用,因爲您可以對照另一個變量測試一個變量的值並控制程序流程。在命令行上,使用它來測試文件是否存在。

來自:Linux 迷

鏈接:https://www.linuxmi.com/bash-linux-test-command.html

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/2cSPFTfjcGKt8UUUH-5YwQ