LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-04-2013, 08:02 AM   #1
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Rep: Reputation: Disabled
Smile Return free swap value


Hi Guys,

Do you know how I can return the value of the Free Swap memory to a variable in shell? I know that the free -k command gives you the info, but it gives me too much. It does not only give the Swap. All I need is the actual value which I can store in a variable and use later in my code. Any ideas?

This is the output of free -k:
total used free shared buffers cached
Mem: 3997796 3297512 700284 0 147936 2711580
-/+ buffers/cache: 437996 3559800
Swap: 2031608 47624 1983984

I need the last line, last value (1983984)

Thanks,
J
 
Old 10-04-2013, 08:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
VARIABLE="$(free -k | awk '/^Swap/ { print $4}')"

$ echo $VARIABLE
5857272
 
1 members found this post helpful.
Old 10-04-2013, 08:18 AM   #3
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Holy crap dude, that was a quick response. Thanks a lot...

I get the first part, that's just the command.
I get the second part as well. That is the scanning of the text part.
What I don't get it the last print part. Can you explain what is happening here?

Thanks,
J
 
Old 10-04-2013, 08:28 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by Jakkie View Post
Holy crap dude, that was a quick response. Thanks a lot...
You're welcome

Quote:
I get the first part, that's just the command.
I get the second part as well. That is the scanning of the text part.
What I don't get it the last print part. Can you explain what is happening here?
Code:
VARIABLE="$(free -k | awk '/^Swap/ { print $4 }')"
First the free -k | awk '/^Swap/ { print $4 }' part is executed.

The output of free -k is given to awk. awk looks for a line that starts with Swap (the /^Swap/ part). When awk finds such a line it prints field number 4 (the free swap space).

The output of free -k | awk '/^Swap/ { print $4 }' is used to set the variable.
 
Old 10-04-2013, 08:29 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
awk splits the input line, print $4 will print the fourth part
 
Old 10-04-2013, 08:30 AM   #6
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Clever stuff, thanks again. You helped me a lot.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
swap used up , even though RAM free vicky007aggrwal Linux - Newbie 8 08-09-2012 06:57 PM
Why SWAP partition is always free? TigerLinux Linux - General 9 01-16-2011 04:08 PM
LXer: The Return of Tax-Free Shopping in New York? LXer Syndicated Linux News 0 08-28-2008 08:00 PM
redhat never free the swap used mandrake7890 Red Hat 4 11-02-2007 03:47 AM
vm issue 2.4.26 free/swap mritch Debian 0 08-05-2004 03:26 PM


All times are GMT -5. The time now is 04:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration