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?
if [-d "~/.ssl"]; then
echo '~/.ssl directory already exists'
else
sudo mkdir ~/.ssl/
fi
[-d
is not a command.
[ -d
is the test
command with the -d option.
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 is another issue, 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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With