shell 文件測(cè)試 蠻全的。
Posted on 2011-02-09 10:22 幻海藍(lán)夢(mèng) 閱讀(2250) 評(píng)論(0) 編輯 收藏 所屬分類: Linux 、Shell文件狀態(tài)測(cè)試
-b filename : 當(dāng)filename 存在并且是塊文件時(shí)返回真(返回0)
-c filename : 當(dāng)filename 存在并且是字符文件時(shí)返回真
-d pathname : 當(dāng)pathname 存在并且是一個(gè)目錄時(shí)返回真
-e pathname : 當(dāng)由pathname 指定的文件或目錄存在時(shí)返回真
-f filename : 當(dāng)filename 存在并且是正規(guī)文件時(shí)返回真
-g pathname : 當(dāng)由pathname 指定的文件或目錄存在并且設(shè)置了SGID 位時(shí)返回真
-h filename : 當(dāng)filename 存在并且是符號(hào)鏈接文件時(shí)返回真 (或 -L filename)
-k pathname : 當(dāng)由pathname 指定的文件或目錄存在并且設(shè)置了"粘滯"位時(shí)返回真
-p filename : 當(dāng)filename 存在并且是命名管道時(shí)返回真
-r pathname : 當(dāng)由pathname 指定的文件或目錄存在并且可讀時(shí)返回真
-s filename : 當(dāng)filename 存在并且文件大小大于0 時(shí)返回真
-S filename : 當(dāng)filename 存在并且是socket 時(shí)返回真
-t fd : 當(dāng)fd 是與終端設(shè)備相關(guān)聯(lián)的文件描述符時(shí)返回真
-u pathname : 當(dāng)由pathname 指定的文件或目錄存在并且設(shè)置了SUID 位時(shí)返回真
-w pathname : 當(dāng)由pathname 指定的文件或目錄存在并且可寫(xiě)時(shí)返回真
-x pathname : 當(dāng)由pathname 指定的文件或目錄存在并且可執(zhí)行時(shí)返回真
-O pathname : 當(dāng)由pathname 存在并且被當(dāng)前進(jìn)程的有效用戶id 的用戶擁有時(shí)返回真(字母O 大寫(xiě))
-G pathname : 當(dāng)由pathname 存在并且屬于當(dāng)前進(jìn)程的有效用戶id 的用戶的用戶組時(shí)返回真
file1 -nt file2 : file1 比f(wàn)ile2 新時(shí)返回真
file1 -ot file2 : file1 比f(wàn)ile2 舊時(shí)返回真
-c filename : 當(dāng)filename 存在并且是字符文件時(shí)返回真
-d pathname : 當(dāng)pathname 存在并且是一個(gè)目錄時(shí)返回真
-e pathname : 當(dāng)由pathname 指定的文件或目錄存在時(shí)返回真
-f filename : 當(dāng)filename 存在并且是正規(guī)文件時(shí)返回真
-g pathname : 當(dāng)由pathname 指定的文件或目錄存在并且設(shè)置了SGID 位時(shí)返回真
-h filename : 當(dāng)filename 存在并且是符號(hào)鏈接文件時(shí)返回真 (或 -L filename)
-k pathname : 當(dāng)由pathname 指定的文件或目錄存在并且設(shè)置了"粘滯"位時(shí)返回真
-p filename : 當(dāng)filename 存在并且是命名管道時(shí)返回真
-r pathname : 當(dāng)由pathname 指定的文件或目錄存在并且可讀時(shí)返回真
-s filename : 當(dāng)filename 存在并且文件大小大于0 時(shí)返回真
-S filename : 當(dāng)filename 存在并且是socket 時(shí)返回真
-t fd : 當(dāng)fd 是與終端設(shè)備相關(guān)聯(lián)的文件描述符時(shí)返回真
-u pathname : 當(dāng)由pathname 指定的文件或目錄存在并且設(shè)置了SUID 位時(shí)返回真
-w pathname : 當(dāng)由pathname 指定的文件或目錄存在并且可寫(xiě)時(shí)返回真
-x pathname : 當(dāng)由pathname 指定的文件或目錄存在并且可執(zhí)行時(shí)返回真
-O pathname : 當(dāng)由pathname 存在并且被當(dāng)前進(jìn)程的有效用戶id 的用戶擁有時(shí)返回真(字母O 大寫(xiě))
-G pathname : 當(dāng)由pathname 存在并且屬于當(dāng)前進(jìn)程的有效用戶id 的用戶的用戶組時(shí)返回真
file1 -nt file2 : file1 比f(wàn)ile2 新時(shí)返回真
file1 -ot file2 : file1 比f(wàn)ile2 舊時(shí)返回真
舉例: if [ -b /dev/hda ] ;then echo "yes" ;else echo "no";fi // 將打印 yes
test -c /dev/hda ; echo $? // 將打印 1 表示test 命令的返回值為1,/dev/hda 不是字符設(shè)備
[ -w /etc/passwd ]; echo $? // 查看對(duì)當(dāng)前用戶而言,passwd 文件是否可寫(xiě)
test -c /dev/hda ; echo $? // 將打印 1 表示test 命令的返回值為1,/dev/hda 不是字符設(shè)備
[ -w /etc/passwd ]; echo $? // 查看對(duì)當(dāng)前用戶而言,passwd 文件是否可寫(xiě)
測(cè)試時(shí)邏輯操作符
-a 邏輯與,操作符兩邊均為真,結(jié)果為真,否則為假。
-o 邏輯或,操作符兩邊一邊為真,結(jié)果為真,否則為假。
! 邏輯否,條件為假,結(jié)果為真。
舉例: [ -w result.txt -a -w score.txt ] ;echo $? // 測(cè)試兩個(gè)文件是否均可寫(xiě)
-o 邏輯或,操作符兩邊一邊為真,結(jié)果為真,否則為假。
! 邏輯否,條件為假,結(jié)果為真。
舉例: [ -w result.txt -a -w score.txt ] ;echo $? // 測(cè)試兩個(gè)文件是否均可寫(xiě)
常見(jiàn)字符串測(cè)試
-z string : 字符串string 為空串(長(zhǎng)度為0)時(shí)返回真
-n string : 字符串string 為非空串時(shí)返回真
str1 = str2 : 字符串str1 和字符串str2 相等時(shí)返回真
str1 != str2 : 字符串str1 和字符串str2 不相等時(shí)返回真
str1 < str2 : 按字典順序排序,字符串str1 在字符串str2 之前
str1 > str2 : 按字典順序排序,字符串str1 在字符串str2 之后
-n string : 字符串string 為非空串時(shí)返回真
str1 = str2 : 字符串str1 和字符串str2 相等時(shí)返回真
str1 != str2 : 字符串str1 和字符串str2 不相等時(shí)返回真
str1 < str2 : 按字典順序排序,字符串str1 在字符串str2 之前
str1 > str2 : 按字典順序排序,字符串str1 在字符串str2 之后
舉例: name="zqf"; [ $name = "zqf" ];echo $? // 打印 0 表示變量name 的值和字符串"zqf"相等
常見(jiàn)數(shù)值測(cè)試
int1 -eq int2 : 如果int1 等于int2,則返回真
int1 -ne int2 : 如果int1 不等于int2,則返回真
int1 -lt int2 : 如果int1 小于int2,則返回真
int1 -le int2 : 如果int1 小于等于int2,則返回真
int1 -gt int2 : 如果int1 大于int2,則返回真
int1 -ge int2 : 如果int1 大于等于int2,則返回真
int1 -ne int2 : 如果int1 不等于int2,則返回真
int1 -lt int2 : 如果int1 小于int2,則返回真
int1 -le int2 : 如果int1 小于等于int2,則返回真
int1 -gt int2 : 如果int1 大于int2,則返回真
int1 -ge int2 : 如果int1 大于等于int2,則返回真
舉例: x=1 ; [ $x -eq 1 ] ; echo $? // 將打印 0 表示變量x 的值等于數(shù)字1
x=a ; [ $x -eq "1" ] // shell 打印錯(cuò)誤信息 [: a: integer expression expected
轉(zhuǎn)貼自:http://blog.chinaunix.net/u/21352/showart_135230.html
x=a ; [ $x -eq "1" ] // shell 打印錯(cuò)誤信息 [: a: integer expression expected