LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 07: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, 07:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 07: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, 07:28 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 07:29 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
awk splits the input line, print $4 will print the fourth part
 
Old 10-04-2013, 07: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:02 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