2013-06-18 25 views
1

我的KornShell(ksh)手冊說-d表達式返回true,如果文件存在並且它是一個目錄。 因此,如果file是目錄,則if [[ -d file ]]應返回TRUE。但在我的情況下,這不是它的工作原理。如果文件存在並且不是目錄,它會返回TRUE,但是shell的手冊說「它是一個目錄」。那麼,有沒有人知道它爲什麼與它應該是相反的?-d條件表達式的奇怪行爲

+1

http://unix.stackexchange.com/? – eis

+0

如果您嘗試'[-d文件] && echo「yes」'會怎麼樣? – fedorqui

+0

什麼操作系統/版本?在osx上適用於我10.7.5 – SheetJS

回答

3

工作正常;這是你的期望是錯誤的。在shell中,返回值爲爲真,並且爲非零返回值爲爲假

$ true ; echo $? 
0 
$ false ; echo $? 
1 
+0

可能的猜測,但是OP沒有說他正在檢查'$?'。 –

0

ksh文件操作員|如果是:

  • -a |文件存在
  • -d |文件是目錄
  • -f |文件是常規文件(即,不是目錄或其他特殊類型的文件)
  • -r |您已經閱讀文件許可
  • -s |文件存在並且不爲空
  • -w |您有文件的寫入權限
  • -x |您擁有文件執行權限或目錄搜索權限(如果是目錄)
  • -O |文件您自己的文件
  • -G |提交您的組ID是相同的文件

kshFileOperatorsFunction.ksh

#***Function to demo ksh file Operators.***# 
fileOperators(){ 
    echo "Entering fileOperators function." 
    if [[ ! -a $1 ]]; then 
     print "file $1 does not exist." 
     return 1 
    fi 
    if [[ -d $1 ]]; then 
     print -n "$1 is a directory that you may " 
     if [[ ! -x $1 ]]; then 
      print -n "not " 
     fi 
     print "search." 
    elif [[ -f $1 ]]; then 
     print "$1 is a regular file." 
    else 
     print "$1 is a special type of file." 
    fi 
    if [[ -O $1 ]]; then 
     print 'you own the file.' 
    else 
     print 'you do not own the file.' 
    fi 
    if [[ -r $1 ]]; then 
     print 'you have read permission on the file.' 
    fi 
    if [[ -w $1 ]]; then 
     print 'you have write permission on the file.' 
    fi 
    if [[ -x $1 && ! -d $1 ]]; then 
     print 'you have execute permission on the file.' 
    fi 
    echo "Exiting fileOperators function." 
} 

參考:O'Reilly, Learning the KornShell Volume 1