我需要列出目录的所有子目录,不包括那些与作为参数给出的任何列表匹配的子目录:“SUBDIR1,SUBDIR2,...,SUBDIRN”。
我带来了使用 ls 和 glob 模式的解决方案(来自许多来源)。为了测试这个概念,我在命令行中尝试了以下似乎有效的序列:
DIR="/path/to/dirs"
EXCLUDELIST="subdir1,subdir2"
#transform in a glob pattern for excluding given subdirectories
EXCLUDE="!(${EXCLUDELIST//,/|})"
LIST=$(cd $DIR && ls -l -d $EXCLUDE | grep -E "(^d)" | awk '{print $9}')
Run Code Online (Sandbox Code Playgroud)
但是,当我将它放入未修改的 bash 脚本中时,我收到此错误
ls: cannot access !(subdir1|subdir2): No such file or directory
Run Code Online (Sandbox Code Playgroud)
将此代码放入脚本时我做错了什么?