I'd like to create directories on a remote VM, but only if they do not exist. What's more, if creating the directory results in an error (exit status != 0) I'd like the script to exit as well, or at least print an error. Is there a more succinct way to do this than what I have below?
UPDATE: Just to clarify, I want to test -d
first so mkdir -p
won't produce output about the directory already existing.
#!/bin/bash
mydirs=(/var/www/files /var/www/photos /var/www/info)
for d in ${mydirs[@]}; do
ssh remotehost "test -d $d"
res=$?
test $res -ne 0 && { ssh remotehost "mkdir -p $d"; res=$?; }
test $res -ne 0 && { echo "error during mkdir on remote"; exit 1; }
done
$res
variables but as far as I see, that's about it. Just thought I'd try this forum before comitting the code in. \$\endgroup\$