Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Memory Usage in a variable using shell script

Tags:

shell

memory

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 }'
like image 610
Mohsin Inayat Khan Avatar asked Apr 12 '26 18:04

Mohsin Inayat Khan


1 Answers

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.

like image 72
songyuanyao Avatar answered Apr 14 '26 13:04

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!