Mohsin Inayat Khan
Mohsin Inayat Khan

Reputation: 1121

How to get Memory Usage in a variable using shell script

I am using the following script to get memory usage value, I want this value in variable so that i can apply 'if' condition on that value.Please suggest

@echo off
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2 }'

Upvotes: 3

Views: 4994

Answers (1)

songyuanyao
songyuanyao

Reputation: 172884

You might want

memory_usage=`free -m | awk 'NR==2{print $3*100/$2 }'`
if [ `echo "${memory_usage} > 90.0" | bc` -eq 1 ] ; then
    echo " > 90.0 "
else
    echo " <= 90.0 "
fi

Note how to save the result of command into variable, and how to compare floating-point number.

Upvotes: 4

Related Questions