我想確定一個文件或目錄是否不存在。我想下面的命令,看是否有文件確實存在,他們正常工作:與bash不是運營商的奇怪
if [ -a 'settings.py' ]; then echo 'exists'; fi
exists #output
if [ -a 'foo' ]; then echo 'exists'; fi #outputs nothing
但當我嘗試這個辦法:
if [ ! -a 'settings.py' ]; then echo 'does not exist'; fi
does not exist #shouldn't be output since the file does exist
if [ ! -a 'foo' ]; then echo 'does not exist'; fi
does not exist
「不存在」輸出不管。代替-a
本頁面使用-e
的Linux Documentation Project
呀。進一步解釋:當'-a'不是''後的第一個事物時,'-a'被解釋爲「和」而不是「存在測試」。 「不」和「無意義,你說? Ayup。這是你不應該在他們的「和」/「或」含義中使用'-a' /'-o'的原因之一。使用'[...] && [...]'/'[...] || [...]而不是。令人討厭的冗長,你說? Ayup。殼牌吸十一點電影。 – zwol 2010-09-28 05:11:29