有没有办法将选项作为数组传递以在 bash 中查找命令?

Tyl*_*den 1 arrays bash find gnu-findutils

我可以在rsync数组中定义我的排除/选项,并在后续命令中使用这些数组,例如

rsync "${rsyncOptions[@]}" "${rsyncExclusions[@]}" src dest

我想使用该find命令实现相同的目标,但我找不到让它工作的方法:

findExclusions=(
    "-not \( -name '#recycle' -prune \)"
    "-not \( -name '#snapshot' -prune \)"
    "-not \( -name '@eaDir' -prune \)"
    "-not \( -name '.TemporaryItems' -prune \)"
)
LC_ALL=C /bin/find src -mindepth 1 "${findExclusions[@]}" -print
Run Code Online (Sandbox Code Playgroud)

无论我尝试以何种组合来定义数组(单引号与双引号,转义括号),我总是会遇到错误:

find: unknown predicate `-not \( -name '#recycle' -prune \)'
# or
find: paths must precede expression: ! \( -name '#recycle' -prune \)
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

Pie*_*rce 5

这里的问题是,这"-not \( -name '#recycle' -prune \)"不是一个参数,而是 6 个参数。你可以这样写:

findExclusions=(
    -not '(' -name '#recycle' -prune ')'
    -not '(' -name '#snapshot' -prune ')'
)
Run Code Online (Sandbox Code Playgroud)