确认存在一个或多个目录 [英] Confirm the existence of one or more directories

查看:79
本文介绍了确认存在一个或多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用通配符检查Bash脚本中是否存在一个或多个目录.

I'd like to check of the existence of one or more directories in a Bash script using a wildcard.

我已经尝试过了

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".

有人建议这样做吗?

推荐答案

在没有&&的情况下,您不能使用-d来同时检查多个目录. (和)在您的表情中.我会用这个:

You can't use -d to check multiple directories at the same time without && (and) in your expressions. I would use this:

for dir in app/*management; do
   if [[ -d $dir ]]; then
      ...
   fi
done

您应该使用glob而不是解析ls的输出.有关更多信息,请参见以下链接: http://mywiki.wooledge.org/ParsingLs

You should use globs instead of parsing the output of ls. See the following link for more information: http://mywiki.wooledge.org/ParsingLs

这篇关于确认存在一个或多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆