如果目录在由 update-alternatives 管理时不存在,则签入 bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6433248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check in bash if directory does not exist when managed by update-alternatives
提问by Hedgehog
I have a path that is managed by update-alternatives. It seems this is confusing bash.
我有一个由 update-alternatives 管理的路径。这似乎是令人困惑的 bash。
The following if should not print yes. But it does because bash thinks the directory does not exist - when in fact it does. This is all done as root, and I've tried the single bracket versions.
下面的 if 不应该打印 yes。但这确实是因为 bash 认为该目录不存在 - 实际上它确实存在。这一切都是以 root 身份完成的,我已经尝试过单括号版本。
RUBY_ROOT=/usr/bin/ruby
CHK_DIR_PATH=`readlink -f "${RUBY_ROOT}"`
if [[ ! -d "${CHK_DIR_PATH}" ]] ; then echo yes; fi
The path does exist:
路径确实存在:
ls -la ${CHK_DIR_PATH}
-rwxr-xr-x 1 root root 6993617 2011-06-21 15:37 /usr/bin/ruby1.9.2-p180
Is there an alternative way to check if a directory does not exist?
有没有其他方法来检查目录是否不存在?
回答by geekosaur
The path exists, but it's not a directory; according to your ls -l, it's a file. So test -dcorrectly returns false, whereas test -eand test -fwould return true. I suspect you have something installed incorrectly.
路径存在,但不是目录;根据你的ls -l,它是一个文件。所以test -d正确返回false,而test -eandtest -f将返回true。我怀疑你安装了错误的东西。

