Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Calculate total memory using free -m

Shell Programming and Scripting


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
sam@sam sam@sam is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 25 July 2017, 1:00 PM EDT
Posts: 34
Thanks: 11
Thanked 0 Times in 0 Posts
Calculate total memory using free -m

Hi

I am trying to calculate memory used by Linux System
free -m
total used free shared buffers cached
Mem: 32109 31010 1099 0 3600 7287
-/+ buffers/cache: 20121 11987
Swap: 10239 1282 8957

Now according to my requirement Im calculating memory using below cmd

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

But in the above code 32109 is hard coded(Which is total memory highlighted in red color) now I want to make this as generic, how do I pass that value 32109 as a variable in code. How can this be achieved

Last edited by sam@sam; 1 Week Ago at 01:30 AM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 27 July 2017, 2:44 PM EDT
Location: Chennai
Posts: 2,563
Thanks: 564
Thanked 1,209 Times in 1,088 Posts
Hello sam@sam,

Could you please let me know if this helps you.

Code:
free -m | awk 'NR==2{val=$2;next} NR==3{printf("Memory Usage: %.2f%\n", ($3*100)/val)}'

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
sam@sam (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
sam@sam sam@sam is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 25 July 2017, 1:00 PM EDT
Posts: 34
Thanks: 11
Thanked 0 Times in 0 Posts
Perfect its working
Thanks you RavinderSingh, for your help.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Nagios script to get total and free memory berveglieri Shell Programming and Scripting 4 01-28-2014 07:37 PM
How much total and free memory I have in my aix 5.3 server? systemoper AIX 4 10-24-2012 03:29 AM
Calculate total space, total used space and total free space in filesystem names matching keyword bigben1220 Shell Programming and Scripting 4 11-25-2009 10:37 AM
How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9 0ktalmagik Solaris 4 02-03-2009 08:37 AM
How to check free/total Memory in AIX vimalbhan AIX 6 01-16-2008 08:34 PM



All times are GMT -4. The time now is 02:51 PM.