我有一个目录数组要从find命令的结果中排除,类似于EXCLUDE=("foo" "bar")
。
我可以从交互式终端运行它,如下所示:find . -name 'hvr.yml' -not -path "foo/*" -not -path "bar/*"
所以我试着这样建立论点:
getServersToCheck() {
# Build command arguments to exclude given dirs
local exclude_command="-not -path"
local excount=${#EXCLUDE[@]}
for ((i=0;i<excount;i++)); do
EXCLUDE[i]="${exclude_command} ${EXCLUDE[i]}/*"
done
find . -name 'hvr.yml' "${EXCLUDE[@]}"
}
但是这会导致find抛出一个未知的 predicate 错误:'-not -path foo/*'
有没有办法做到这一点?当我回显命令时,它看起来是正确的,但一定有一些bash语法规则导致它不能按我期望的方式工作。
更新:
我在路径周围添加了\"
以排除,因为我读到globbing只发生在带引号的字符串中。xtrace显示如下:
find . -name hvr.yml -not -path '"foo/*"' -not -path '"bar/*"'
单引号可能是问题所在
删除\"
并运行xtrace显示,globbing正在for循环中应用,结果是:
find . -name hvr.yml -not -path "foo/fileinfoo" "foo/somethingelseinfoo" -not -path "bar/*" "bar/fileinbar" "bar/otherfilesinbar"
所以find在抱怨随机路径作为参数。
有没有办法扩展EXCLUDE数组并在命令中的每个元素的末尾添加/*?
2条答案
按热度按时间3yhwsihp1#
找到了一个替代解决方案,我试图通过使用grep来实现:
doinxwow2#
输出: