开发者

Execute awk output

while rea开发者_JS百科d line;
do
  awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}';
done < diffs.txt

This prints the command exactly as I want it. How do I tell it to execute the command?


| bash does the trick...

while read line;
do
  awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}' | bash;
done < diffs.txt


You can use the "system" command for these kinds of tasks.

awk '/ differ$/ {system("diff "$2" "$4" > "$2".diff")} diffs.txt


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'..

Following code works for those cases also.

while read line;
do

  $(awk '/ differ$/ {print "diff "$2" "$4" > "$2".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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜