bash 执行 awk 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7483705/
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
Execute awk output
提问by Michael
while read line;
do
awk '/ differ$/ {print "diff "" "" > "".diff"}{}';
done < diffs.txt
This prints the command exactly as I want it. How do I tell it to execute the command?
这完全按照我的需要打印命令。我如何告诉它执行命令?
回答by Michael
| bashdoes the trick...
| bash有诀窍吗...
while read line;
do
awk '/ differ$/ {print "diff "" "" > "".diff"}{}' | bash;
done < diffs.txt
回答by Chris
You can use the "system" command for these kinds of tasks.
您可以将“系统”命令用于这些类型的任务。
awk '/ differ$/ {system("diff "" "" > "".diff")} diffs.txt
回答by iankit
The accepted answer (by @micheal) for this question is only partially correct. It works for almost all cases, except when the command requires creation of a new terminal or pseudo terminal. Like 'ssh' commands, or 'tmux new'..
这个问题的公认答案(@micheal)只是部分正确。它几乎适用于所有情况,除非命令需要创建新终端或伪终端。像'ssh'命令,或'tmux new'..
Following code works for those cases also.
以下代码也适用于这些情况。
while read line;
do
$(awk '/ differ$/ {print "diff "" "" > "".diff"}{}')
done < diffs.txt
$() is the bash command substitution pattern. You can read more about command substitution in Linux Documentation Project here : http://www.tldp.org/LDP/abs/html/commandsub.html.
$() 是 bash 命令替换模式。您可以在此处阅读 Linux 文档项目中有关命令替换的更多信息:http: //www.tldp.org/LDP/abs/html/commandsub.html。

