I'd like to check of the existence of one or more directories in a Bash script using a wildcard.
I've tried this;
if [ -d app/*management ]
then
for mscript in `ls -d app/*management`
do
...
done
fi
Which works if there is one match but throws the error "binary operator expected".
Any suggestion on a good way to do this?
Why don't you do it like this:
You can't use -d to check multiple directories at the same time without && (and) in your expressions. I would use this:
You should use globs instead of parsing the output of ls. See the following link for more information: http://mywiki.wooledge.org/ParsingLs
After the glob expands to your list of management directories, the
if
statement looks likeSince
-d
takes only one argument, bash doesn't know what to do with the remaining directories. To bash, it looks like you forgot to include a binary operator.You can do just do the following:
EDIT: As jordanm commented, the following probably isn't necessary, but I'll leave it here for reference, as
nullglob
is good to know about.One caveat. If there is a possibility that app/*management won't expand to anything, you need to set the shell option
nullglob
before your loop, or else "app/*management" will be treated as a literal string, not a shell glob.