[SOLVED] Another "syntax error" while using awk when accessing remote servers
by brh1 from LinuxQuestions.org on (#5N5MC)
Good day.
This command works nicely on a local server to display the CPU usage:
Code:-bash-4.2$ grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
26.5474%
-bash-4.2$However, when I try to query the CPU usage on remote servers I get a 'syntax error':
Code:-bash-4.2$ for h in {12..17} ; do echo;ssh -q -o "StrictHostKeyChecking no" 192.168.210.$h "uname -n;grep 'cpu ' /proc/stat | awk '{cpu_usage=(\$2+\$4)*100/(\$2+\$4+\$5)} END {print cpu_usage "%"}'";done
remote-vm101sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm102sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm103sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm104sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm105sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm106sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
-bash-4.2$I've tried backslashes, ^, quotes, to no avail. It also works without the '"%"' but I thought it would look better with the percentage sign.
Please advise.
Bjoern
This command works nicely on a local server to display the CPU usage:
Code:-bash-4.2$ grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
26.5474%
-bash-4.2$However, when I try to query the CPU usage on remote servers I get a 'syntax error':
Code:-bash-4.2$ for h in {12..17} ; do echo;ssh -q -o "StrictHostKeyChecking no" 192.168.210.$h "uname -n;grep 'cpu ' /proc/stat | awk '{cpu_usage=(\$2+\$4)*100/(\$2+\$4+\$5)} END {print cpu_usage "%"}'";done
remote-vm101sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm102sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm103sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm104sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm105sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
remote-vm106sip001
awk: cmd. line:1: {cpu_usage=($2+$4)*100/($2+$4+$5)} END {print cpu_usage %}
awk: cmd. line:1: ^ syntax error
-bash-4.2$I've tried backslashes, ^, quotes, to no avail. It also works without the '"%"' but I thought it would look better with the percentage sign.
Please advise.
Bjoern