【发布时间】:2018-12-11 22:20:01
【问题描述】:
我有一组目录要从我的 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 抛出未知谓词错误:'-not -path foo/*'
有没有办法做到这一点?当我回显命令时,它看起来是正确的,但一定有一些 bash 语法规则导致它无法按我的预期工作。
更新:
我在排除路径周围添加了\",因为我读到只有带引号的字符串才会发生通配。 xtrace 显示如下:
find . -name hvr.yml -not -path '"foo/*"' -not -path '"bar/*"'
单引号可能是问题
删除 \" 并使用 xtrace 运行表明在 for 循环中应用了 globbing,导致:
find . -name hvr.yml -not -path "foo/fileinfoo" "foo/somethingelseinfoo" -not -path "bar/*" "bar/fileinbar" "bar/otherfilesinbar"
因此 find 抱怨将随机路径作为参数给出。
有没有办法扩展 EXCLUDE 数组并将 /* 添加到命令中每个元素的末尾?
【问题讨论】: