Skip to content
Advertisement

how to run complex awk command on remote machine

I am trying to capture the total used memory in % on remote machine as the following

 sshpass -p pasa123 ssh root@server01 "  /usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%n"), $3/$2*100}' "
awk: cmd. line:1: /Mem/{printf(RAM Usage: %.2f%n), /*100}
awk: cmd. line:1:                       ^ syntax error
awk: cmd. line:1: /Mem/{printf(RAM Usage: %.2f%n), /*100}
awk: cmd. line:1:                                ^ syntax error
awk: cmd. line:1: /Mem/{printf(RAM Usage: %.2f%n), /*100}
awk: cmd. line:1:                                   ^ unterminated regexp
awk: cmd. line:1: /Mem/{printf(RAM Usage: %.2f%n), /*100}
awk: cmd. line:1:                                        ^ unexpected newline or end of string

so we add backslash as before the “$” as

 sshpass -p pasa123 ssh root@server01 "  /usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%n"), $3/$2*100}' "
 

but still with the same errors

any advice how to fix this syntax?

Advertisement

Answer

You also have to escape the double quotes ":

sshpass -p pasa123 ssh root@server01 "/usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%n"), $3/$2*100}' "
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement