Linux的 test 命令使用

來源:互聯網
上載者:User
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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.