shell中條件判斷if中的-a到-z的意思

轉自http://www.tuicool.com/articles/MbmiAf


[-a file] 如果file存在則爲真 

[-b file] 如果file存在且是一個塊特殊文件則爲真 

[-c file] 如果file存在且是一個字特殊文件則爲真 

[-d file] 如果file文件存在且是一個目錄則爲真 

-d前的!是邏輯非 

例如: 

if [ ! -d $lcd_path/$par_date ] 

表示後面的那個目錄不存在,則執行後面的then操作 

[-e file] 如果file文件存在則爲真 

[-f file] 如果file存在且是一個普通文件則爲真 

[-g file] 如果file存在且已經設置了SGID則爲真(SUID 是 Set User ID, SGID 是 Set Group ID的意思) 

[-h file] 如果file存在且是一個符號連接則爲真 

[-k file] 如果file存在且已經設置粘制位則爲真 

當一個目錄被設置爲"粘制位"(用chmod a+t),則該目錄下的文件只能由 

一、超級管理員刪除 

二、該目錄的所有者刪除 

三、該文件的所有者刪除 

也就是說,即便該目錄是任何人都可以寫,但也只有文件的屬主纔可以刪除文件。 

具體例子如下: 

#ls -dl /tmp 

drwxrwxrwt 4 root    root  ......... 

注意other位置的t,這便是粘連位。 

[-p file] 如果file存在且是一個名字管道(F如果O)則爲真 

管道是linux裏面進程間通信的一種方式,其他的還有像信號(signal)、信號量、消息隊列、共享內存、套接字(socket)等。 

[-r file] 如果file存在且是可讀的則爲真 

[-s file] 如果file存在且大小不爲0則爲真 

[-t FD] 如果文件描述符FD打開且指向一個終端則爲真 

[-u file] 如果file存在且設置了SUID(set userID)則爲真 

[-w file] 如果file存在且是可寫的則爲真 

[-x file] 如果file存在且是可執行的則爲真 

[-O file] 如果file存在且屬有效用戶ID則爲真 

[-G file] 如果file存在且屬有效用戶組則爲真 

[-L file] 如果file存在且是一個符號連接則爲真 

[-N file] 如果file存在and has been mod如果ied since it was last read則爲真 

[-S file] 如果file存在且是一個套接字則爲真 

[file1 –nt file2] 如果file1 has been changed more recently than file2或者file1 exists and file2 does not則爲真 

[file1 –ot file2] 如果file1比file2要老,或者file2存在且file1不存在則爲真 

[file1 –ef file2] 如果file1和file2指向相同的設備和節點號則爲真 

[-o optionname] 如果shell選項“optionname”開啓則爲真 

[-z string] “string”的長度爲零則爲真 

[-n string] or [string] “string”的長度爲非零non-zero則爲真 

[sting1==string2] 如果2個字符串相同。“=”may be used instead of “==”for strict posix compliance則爲真 

[string1!=string2] 如果字符串不相等則爲真 

[string1<string2] 如果“string1”sorts before“string2”lexicographically in the current locale則爲真 

[arg1 OP arg2] “OP”is one of –eq,-ne,-lt,-le,-gt or –ge.These arithmetic binary oprators return true if “arg1”is equal to,not equal to,less than,less than or equal to,greater than,or greater than or equal to“agr2”,respectively.“arg1”and “agr2”are integers. 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章