Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.