bash、查找、执行和回声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4470673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:07:19  来源:igfitidea点击:

bash, find, exec and echo

bashfor-loopcommand-linefind

提问by Roberto

find . \
  -name 'user_prefs' \
  -exec echo "whitelist_from [email protected]" >> {} \;'

I would like to add the line whitelist_from [email protected]to all files that are found by find, but my command does not work. It just creates the file '{}'.

我想将该行添加whitelist_from [email protected]到由 找到的所有文件中find,但我的命令不起作用。它只是创建文件“{}”。

Shoud i use the forcommand?

我应该使用for命令吗?

thanks!

谢谢!

回答by Balázs Pozsár

You have to escape the '>>', for example like this:

您必须转义“>>”,例如像这样:

find . -name 'user_prefs' -exec sh -c 'echo "whitelist_from [email protected]" >> {}' \;

回答by Balázs Pozsár

As said already, using xargs is encouraged, but you can also avoid executing sh many times by:

如前所述,鼓励使用 xargs,但您也可以通过以下方式避免多次执行 sh:

find . -name 'user_prefs' | while read filename; do echo "whitelist_from [email protected]" >>"$filename"; done