linux test命令詳解

2021-06-19 07:05:07 字數 1719 閱讀 6197

1. 關於某個檔名的『型別』偵測(存在與否),如test -e filename

-e 該『檔名』是否存在?(常用)  

-f 該『檔名』是否為檔案(file)?(常用)  

-d 該『檔名』是否為目錄(directory)?(常用)  

-b 該『檔名』是否為乙個block device 裝置?

-c 該『檔名』是否為乙個character device 裝置?

-s 該『檔名』是否為乙個socket 檔案?

-p 該『檔名』是否為乙個fifo (pipe) 檔案?

-l 該『檔名』是否為乙個鏈結檔?

2. 關於檔案的許可權偵測,如 test -r filename

-r 偵測該檔名是否具有『可讀』的屬性?

-w 偵測該檔名是否具有『可寫』的屬性?

-x 偵測該檔名是否具有『可執行』的屬性?

-u 偵測該檔名是否具有『suid』的屬性?

-g 偵測該檔名是否具有『sgid』的屬性?

-k 偵測該檔名是否具有『sticky bit』的屬性?

-s 偵測該檔名是否為『非空白檔案』?

3. 兩個檔案之間的比較,如: test file1 -nt file2

-nt (newer than)判斷file1 是否比file2 新

-ot (older than)判斷file1 是否比file2 舊

-ef 判斷file2 與file2 是否為同一檔案,可用在判斷hard link 的判定上。 主要意義在判定,兩個檔案是否均指向同乙個inode 哩!

4. 關於兩個整數之間的判定,例如test n1 -eq n2

-eq 兩數值相等(equal)  

-ne 兩數值不等(not equal)  

-gt n1 大於n2 (greater than)  

-lt n1 小於n2 (less than)  

-ge n1 大於等於n2 (greater than or equal)  

-le n1 小於等於n2 (less than or equal) 

5. 判定字串的資料

test -z string 判定字串是否為0 ?若string 為空字串,則為true  

test -n string 判定字串是否非為0 ?若string 為空字串,則為false。

注:-n 亦可省略

test str1 = str2 判定str1 是否等於str2 ,若相等,則回傳true  

test str1 != str2 判定str1 是否不等於str2 ,若相等,則回傳false 

6. 多重條件判定,例如: test -r filename -a -x filename

-a (and)兩狀況同時成立!例如test -r file -a -x file,則file 同時具有r 與x 許可權時,才回傳true。

-o (or)兩狀況任何乙個成立!例如test -r file -o -x file,則file 具有r 或x 許可權時,就可回傳true。

! 反相狀態,如test ! -x file ,當file 不具有x 時,回傳true

linux test命令詳解

test命令是shell環境中測試條件表示式的實用工具。test 選項 b 檔案 如果檔案為乙個塊特殊檔案,則為真 c 檔案 如果檔案為乙個字元特殊檔案,則為真 d 檔案 如果檔案為乙個目錄,則為真 e 檔案 如果檔案存在,則為真 f 檔案 如果檔案為乙個普通檔案,則為真 g 檔案 如果設定了檔案的...

Linux test命令別名

linux中test命令可以用來檢測表示式的真假,如果為真,則返回乙個0值,如果為假則返回乙個大於0的值。test命令還有乙個別名 test與其別名 都可以在 usr bin或 bin中找到。test可以單獨使用也可以與if while或until命令結合使用來對程式的流程進行控制。test和 的語...

Linux test 命令的使用

關於某個檔名的 型別 偵測 存在與否 如 test e filename e 該 檔名 是否存在?常用 f 該 檔名 是否為檔案 file 常用 d 該 檔名 是否為目錄 directory 常用 b 該 檔名 是否為乙個 block device 裝置?c 該 檔名 是否為乙個 character...