Reputation: 59
I would like to calculate the percentage of free memory using Linux bash shell scipts.
Example:
bash-4.1$ free
total used free shared buffers cached
Mem: 12223100 11172812 1050288 316 714800 629944
-/+ buffers/cache: 9828068 2395032
Swap: 6266872 5852824 414048
Ex. (1050288/12223100) * 100 = %free memory-I want to do this using scripts.
Thanks Puspa
Upvotes: 2
Views: 8103
Reputation: 73
memfree=`cat /proc/meminfo | grep MemFree | awk '{print $2}'`;
memtotal=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`;
bc -l <<< "$memfree * 100 / $memtotal"
the proc/meminfo file displays everything you should need about memory.
You use grep to isolate the line about Free Memory and Total memory, and store it in variables. Then you use bc -l for the float division.
EDIT: If there is no bc installed, you may use echo :
echo $(($memfree.0 * 100 / $memtotal))
Upvotes: 5