如何使用变量指定要在`find`中排除的路径?

ton*_*y19 1 bash quoting

我正在尝试使用变量在find命令中指定排除路径.

此代码段有效:

x="*test/.util/*"
y="*test/sh/*"

find test -type f -name "*.sh" -not -path "$x" -not -path "$y"
Run Code Online (Sandbox Code Playgroud)

但是我想-not -path进入变量,如:

x="-not -path *test/.util/*"
y="-not -path *test/sh/*"

# error: find: -not -path *test/.util/*: unknown primary or operator
find test -type f -name "*.sh" "$x" "$y"

# Tried removing quotes
# error: find: test/.util/foo.sh: unknown primary or operator
find test -type f -name "*.sh" $x $y 
Run Code Online (Sandbox Code Playgroud)

我还尝试在变量中的路径中添加引号,但这不会导致路径过滤.

# no syntax error but does not exclude the paths
x="-not -path '*test/.util/*'"
y="-not -path '*test/sh/*'"
Run Code Online (Sandbox Code Playgroud)

我正在使用OSX Mavericks; GNU bash,版本3.2.51(1)-release(x86_64-apple-darwin13).

我究竟做错了什么?谢谢

che*_*ner 5

find-not -path *test/.util/*作为单个参数接收,而不是它需要的3个单独的参数.您可以使用数组.

x=(-not -path "*test/.util/*")
y=(-not -path "*test/sh/*")

find test -type f -name "*.sh" "${x[@]}" "${y[@]}"
Run Code Online (Sandbox Code Playgroud)

引用时,${x[@]}扩展为一系列单独的单词,每个数组元素一个,并且每个单词保持正确引用,因此模式按字面意思传递给find.