bash [-d: 未找到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22768533/
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
[-d: command not found
提问by user886596
As per this answer: Unix Bash Shell Programming if directory exists, I'm trying to check if a directory exists. However, when I run this, I get line 1: [-d: command not found
. What am I doing wrong here?
根据这个答案:Unix Bash Shell Programming if directory exists,我正在尝试检查目录是否存在。但是,当我运行它时,我得到line 1: [-d: command not found
. 我在这里做错了什么?
if [-d "~/.ssl"]; then
echo '~/.ssl directory already exists'
else
sudo mkdir ~/.ssl/
fi
回答by kojiro
[-d
is not a command.
不是命令。
[ -d
is the test
command with the -d option.
是test
带有 -d 选项的命令。
Space matters.
空间很重要。
(Also, the [
command needs to end with a ]
parameter, which likewise has to be separated from other arguments by whitespace.)
(此外,该[
命令需要以]
参数结尾,同样必须用空格将其与其他参数分开。)
That's the crux of the matter. There are a couple of other issues, though:
这就是问题的关键。但是,还有一些其他问题:
- If you quote the tilde, it doesn't expand. (This is one of the rare place where you may want to avoid quotes.) Quotes are great, though, so why not write
"$HOME/.ssl"
? (There's a subtle difference between ~ and "$HOME", but it doesn't matter for most uses.) - You probably have your checks reversed – right now you're trying to create the directory only if it already exists.
- 如果你引用波浪号,它不会扩展。(这是您可能希望避免引用的罕见地方之一。)不过,引用很棒,那么为什么不写
"$HOME/.ssl"
呢?( ~ 和 "$HOME" 之间有细微的区别,但对于大多数用途来说并不重要。) - 您可能已经取消了检查 – 现在您仅在目录已经存在的情况下才尝试创建该目录。
Honestly, all you really need is probably:
老实说,您真正需要的可能是:
if mkdir -p ~/.ssl; then
# Do stuff with new directory
else
# Handle failure (but keep in mind `mkdir` will have its own error output)
fi