Checking for the existence of multiple directories
I would loop:
result=True
for dir in \
"$PWD/dir1" \
"$PWD/dir2" \
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
echo "$result"
The break
causes the loop to short-circuit, just like your chain of &&
A loop might be more elegant:
arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
for d in "${arr[@]}"; do
if [ -d "$d"]; then
echo True
else
echo False
fi
done
This is Bash. A more portable one is Sh. There you can use the positional array:
set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"
Then to loop over it use "$@"
.
If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls
utility to determine whether one or more "errors occurred":
ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir3" >/dev/null 2>&1 && echo All there
I redirect the output and stderr to /dev/null
in order to make it disappear, since we only care about the exit code from ls
, not its output. Anything that's written to /dev/null
disappears — it is not written to your terminal.