test命令格式:
test condition
通常,在if-then-else語句中,用[]代替,即[ condition ]。注意:方括號兩邊都要用空格。
1、數值比較
===========================================================
比 較 描 述
-----------------------------------------------------------
n1 -eq n2 檢查n1是否與n2相等 (equal)
n1 -ge n2 檢查n1是否大於或等於n2 (greater and equal)
n1 -gt n2 檢查n1是否大於n2 (greater than)
n1 -le n2 檢查n1是否小於或等於n2 (less and equal)
n1 -lt n2 檢查n1是否小於n2 (less than)
n1 -ne n2 檢查n1是否不等於n2 (not equal)
===========================================================
例子:
#!/bin/bash val1=10 val2=11 if [ $val1 -gt 5 ] then echo "The test value $val1 is greater than 5" fi if [ $val1 -eq $val2 ] then echo "The vaules are equal" else echo "The values are different" fi
2、字符串比較
===========================================================
比 較 描 述
-----------------------------------------------------------
str1 = str2 檢查str1是否和str2相同
str1 != str2 檢查str1是否和str2不同
str1 < str2 檢查str1是否比str2小
str1 > str2 檢查str1是否比str2大
-n str1 檢查str1的長度是否非0
-z str1 檢查str1的長度是否為0
===========================================================
需要注意的是:
- 字符串比較大於小於號必須轉義,即加反斜線。
- 字符串比較的順序是按ASCII表的順序的,大寫字母比小寫字母的值小。
文件比較
=======
這是shell編程中最強大的也是最常用到的比較。test命令允許你測試Linux文件系統上文件和目錄的狀態,如下表:
==================================================================
比 較 描 述
------------------------------------------------------------------
-d file 檢查file是否存在並是一個目錄
-e file 檢查file是否存在
-f file 檢查file是否存在並是一個文件
-r file 檢查file是否存在並可讀
-s file 檢查file是否存在並非空
-w file 檢查file是否存在並可寫
-x file 檢查file是否存在並可執行
-O file 檢查file是否存在並屬當前用戶所有
-G file 檢查file是否存在並且默認組與當前用戶相同
file1 -nt file2 檢查file1是否比file2新
file1 -ot file2 檢查file1是否比file2舊
==================================================================
3、檢查目錄
-d測試會檢查指定的文件名是否在系統上以目錄形式存在。當寫文件到某個目錄之前,或者是將文件放置到某個目錄位置之前時,這會非常有用。
#!/bin/bash if [ -d $HOME ] then echo "Your HOME directory exists" cd $HOME ls -a else echo "There is a problem with your HOME directory" fi
上面代碼使用了-d測試條件來檢查用戶的$HOME目錄是否存在。如果它存在的話,它將繼續使用cd命令來切到$HOME目錄並進行顯示目錄下的文件。
4、檢查對象是否存在
-e比較允許你在腳本中使用對象前檢查文件或目錄對象是否存在:
#!/bin/bash # checking if a directory exists if [ -e $HOME ] then echo "OK on the directory, now to check the file" # checking if a file exists if [ -e $HOME/testing ] then # the file exists, append date to it echo "Appending date to existing file" date >> $HOME/testing else # the file does not exist, create a new file echo "Creating new file" echo > $HOME/testing fi else echo "Sorry, you do not have a HOME directory" fi
5、檢查文件
-e比較適用於文件和目錄。但要確定指定的對象是個文件,你必須用-f比較:
#!/bin/bash # check if a file if [ -e $HOME ] then echo "The object exists, is it a file?" if [ -f $HOME ] then echo "Yes, it is a file!" else echo "No, it is not a file!" if [ -f $HOME/.bash_history ] then echo "But this is a file!" fi fi else echo "Sorry, the object does not exist" fi
6、檢查是否可讀
在嘗試從文件中讀取數據之前,最好先測試一下是否能讀文件。可以用-r測試。
7、檢查空文件
用-s來檢查文件是否為空,尤其是在你要刪除文件時。當-s比較成功時要特別小心,它說明文件中有數據。
8、檢查是否可寫
-w比較用來判斷你是否對文件有可寫權限。
9、檢查是否可執行
-x比較是一個簡便的斷判斷你對某個特定文件是否有執行權限的方法。雖然可能大多數命令用不到它,但如果你要在shell中運行大量腳本,它可能很方便。
10、檢查所屬關系
-O比較允許你輕松地測試你是否是文件的屬主。
11、檢查默認屬組關系
-G比較會檢查文件的默認組,如果它匹配了用戶的默認組,那就通過了。