How to use variable as a path for find command

孤者浪人 提交于 2019-12-12 03:15:40

问题


Can someone give me a hint, how I can make the following bash script run properly:

path="/path/"
excludepath="! -path \"/path/to/exclude/*\" "

find "$path" "$excludepath" -name *."txt" -type f

When I try to run it I get:

find: `! -path "/path/to/exclude/*"': No such file or directory

Thank you in advance.


回答1:


Your problem was the fact that "$excludepath" is a single argument, even if it contains spaces. To put several arguments, use an array of strings instead of a string:

excludepath=(! -path "*webshow*")
find "$path" "${excludepath[@]}" -name "*.txt" -type f

This is a bashism, though (people that stumble onto this and don't use bash will need to do something else).



来源:https://stackoverflow.com/questions/34761793/how-to-use-variable-as-a-path-for-find-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!