1

Basically memory is a variable. In a script, I am trying to remote login to one server and execute free command to check memory usage and using awk I'm getting the usage in percentage. But I am getting syntax error for the below.

memory=`ssh $line -n "free | grep Mem | awk '{ print $3/$2 *100.0 }'"`

echo -e "The memory usage is: $memory" >>$LOGFILE

Error is:

awk: { print / *100.0 }
awk:          ^ unterminated regexp
awk: cmd. line:1: { print / *100.0 }
awk: cmd. line:1:                   ^ unexpected newline or end of string
4

Problem is that $3 and $2 are substituted by the shell (with nothing when unassigned) before the command runs, because they are enclosed in double quotes.

The single quotes inside the double quotes don't prevent that:

$ echo "'$unassigned'"
''

You should escape the $:

memory=$(ssh $line -n "free | awk '/Mem/ { print \$3/\$2 *100.0 }'")

Alternatively, you could run only free on the server, and awk on the client.

memory=$(ssh $line -n "free" | awk '/Mem/ { print $3/$2 *100.0 }')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.