我的KornShell(ksh)手冊說-d
表達式返回true,如果文件存在並且它是一個目錄。 因此,如果file
是目錄,則if [[ -d file ]]
應返回TRUE。但在我的情況下,這不是它的工作原理。如果文件存在並且不是目錄,它會返回TRUE,但是shell的手冊說「它是一個目錄」。那麼,有沒有人知道它爲什麼與它應該是相反的?-d條件表達式的奇怪行爲
1
A
回答
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."
}
相關問題
- 1. 正則表達式 - 奇怪的行爲
- 2. 正則表達式奇怪的行爲
- 3. scala正則表達式奇怪行爲
- 4. Excel條件格式奇怪行爲
- 5. 奇怪的條件格式
- 6. Lambda表達式爲的ThreadStart奇怪的行爲
- 7. C:奇怪的條件printf行爲
- 8. 條件運算符的奇怪行爲?
- 9. 的Javascript:奇怪的正則表達式的行爲
- 10. 布爾表達式中的strlen()的奇怪行爲
- 11. 奇怪的正則表達式的行爲
- 12. Python中的括號奇怪的行爲正則表達式
- 13. JavaScript中的正則表達式的奇怪行爲
- 14. 奇怪的JS正則表達式的行爲
- 15. JFlex的字符串正則表達式奇怪的行爲
- 16. Perl的正則表達式的奇怪行爲
- 17. 奇怪的Python行爲與正則表達式模塊
- 18. 奇怪的行爲正則表達式在delphi X5
- 19. JavaScript正則表達式奇怪的行爲
- 20. bash正則表達式的多個匹配行爲奇怪?
- 21. 在正則表達式中的奇怪行爲
- 22. 列控制PowerBuilder表達式控制奇怪的行爲
- 23. bash正則表達式(或測試)奇怪的行爲
- 24. 在Java中使用正則表達式的奇怪行爲
- 25. AngularJS ng-pattern奇怪的行爲(正則表達式)
- 26. Python正則表達式奇怪的行爲
- 27. 奇怪的正則表達式行爲與[^ pL]
- 28. javascript替換()函數奇怪的行爲與正則表達式
- 29. postgresql奇怪的正則表達式行爲
- 30. 用正則表達式奇怪的行爲
http://unix.stackexchange.com/? – eis
如果您嘗試'[-d文件] && echo「yes」'會怎麼樣? – fedorqui
什麼操作系統/版本?在osx上適用於我10.7.5 – SheetJS