LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2009, 08:28 AM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
Lightbulb Calculating Values of Fields


Hi,

Kindly have a look at this output:
Code:
[root@localhost ~]# free
             total       used       free     shared    buffers     cached
Mem:        506940     423120      83820          0      26744     120692
-/+ buffers/cache:     275684     231256
Swap:      1052248          0    1052248
[root@localhost ~]# free | tee diskInfo | grep Swap | cut -d: -f2
      1052248          0    1052248
[root@localhost ~]#

Now, what I want to do is calculate a percentage value of Swap utilization so as to show how much space is free or utilized- in percentage.

I want to capture/store all the three values (in variables), as shown above in the result of the second command, separately so that I can further use those variables to perform the calculation.

Some correction is needed to be done in the "cut" fragment of the second command.

Can you show it?
 
Old 09-01-2009, 09:28 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If all you want is just the numbers and a percentage output, awk would be a better choice. It can even do the calculations for you.
Code:
david:[~]$ free | awk '/Swap/ {print "total:",$2,"\nused:",$3,"\nfree:",$4,"\n% used:",($3/$2*100),"\n% free:",($4/$2*100)}'
total: 1461872
used: 5108
free: 1456764
% used: 0.349415
% free: 99.6506
If you really need them in variables for other purposes, then it's easy to add the values to an array in the same fashion:
Code:
david:[~]$ freearray=( $(free|awk '/Swap/ {print $2,$3,$4}') )

david:[~]$ echo "${freearray[0]}
1461872
david:[~]$ echo "${freearray[1]}
5108
david:[~]$ echo "${freearray[2]}
1456764
 
Old 09-01-2009, 09:35 AM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
just a small addition id prefer adding free -om
 
Old 09-01-2009, 10:16 AM   #4
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Thumbs up

I have tried the following and it is great! I will try the first one also, but tomorrow. It's time to go and take rest!

Quote:
Originally Posted by David the H. View Post

If you really need them in variables for other purposes, then it's easy to add the values to an array in the same fashion:
Code:
freearray=( $(free|awk '/Swap/ {print $2,$3,$4}') )

echo "${freearray[0]}"

echo "${freearray[1]}"

echo "${freearray[2]}"

Last edited by Hi_This_is_Dev; 09-01-2009 at 10:17 AM.
 
  


Reply

Tags
calculation



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script that assign's values to fields NsearchOf Programming 16 06-01-2009 10:00 AM
Calculating seconds newbie_adm Programming 8 10-21-2008 12:29 PM
calculating throughput ilnli Programming 6 07-17-2007 01:58 AM
calculating in bash ovince Programming 3 03-28-2007 10:50 AM
creating tr fields according to session values? ati Programming 6 05-09-2006 03:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration