I would like to get the total memory of a host by using a ‘.exp’ script.
After getting inside the host, I execute the following line:
"send "top -n 1 | grep Mem: | awk '{ print $(NF-7) }' | cut -d 'k' -f1\r""
While executing the script, it returns the following error:
can't read "(NF-7)": no such variable
But if get manually inside the host, and execute the same line ( without the “send” part) , it returns me the expected number…
What I’m doing wrong? Any idea? Any proposal for other way of doing the same?
Best Regards and thanks in advance,
Antonio
The output of
topis not meant to be parsed this way. You can get the amount of memory in other, easier (better!) ways.Or you can parse the output of
free, which also knows how to display in convenient sizes:EDIT: You always have to ask yourself “Who expands what?”
To send these commands with expect, try
This escapes from
expectthings that it would otherwise process and which were intended forsed.And here’s a (properly escaped) version of your original.