简体   繁体   中英

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 }'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM