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.
精彩评论