Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-04-2011, 04:22 AM
|
#1
|
|
Member
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 61
Rep:
|
Script to calcualte installed memory.
Hello,
I am trying to write a script to calculate the total amount of installed memory to use during an anaconda kickscript, so the swap file is created at 2 x the installed memory.
I so far have the amount of installed RAM DIMMS but need a way to total them up and produce a varible I can use in the pre section of the install.
Code:
dmidecode -t 17 | grep Size | awk ' { print $2 }'
Output:
2048
2048
Note: on some servers there could be from 1 DIMM up to 16 DIMMS installed so the script needs to be able to handle this. I also can not use bc as it does not exist during the install stage.
I am guessing I need a while loop to do this and use expr but do not know where to start for this logic.
Cheers
|
|
|
|
04-04-2011, 08:18 AM
|
#2
|
|
Member
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 130
Rep:
|
dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } END { print RAM }'
|
|
|
1 members found this post helpful.
|
04-04-2011, 08:27 AM
|
#3
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
There's no need to use grep or anything else when awk can do everything itself. Awk is not just a column-printing program, it's a rich and complex scripting language. It can do just about everything grep and sed can do, and even has a built-in mathematical engine on a near-par with bc.
In this case, you just have to increment the value of an awk variable for each line it finds, then print the final value at the end.
But also, on my system I came up with a "No Module Installed" entry when I ran dmidecode, so you'll should check that the string is appropriate too.
Code:
dmidecode -t 17 | awk '( /Size/ && /^[0-9]+$/ ) { x+=$2 } END{ print x }'
If you want to save the output into a shell variable, simply use command substitution:
Code:
variablename="$( commands )"
Here are a few useful sed and awk references.
The grymoire links go to highly-recommended tutorials:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
Here are a few useful bash scripting references:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
Last edited by David the H.; 04-04-2011 at 08:38 AM.
Reason: minor change for more accuracy
|
|
|
1 members found this post helpful.
|
04-04-2011, 08:56 AM
|
#4
|
|
Member
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 61
Original Poster
Rep:
|
Quote:
Originally Posted by tshikose
dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } END { print RAM }'
|
Thanks tshikose for your help.
Can you let me know how to take the total and multiply it by 2.
|
|
|
|
04-04-2011, 08:59 AM
|
#5
|
|
Member
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 61
Original Poster
Rep:
|
Hello
I managed to get the result using:
Quote:
|
dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } {TOTAL=RAM+RAM} END { print TOTAL }'
|
thanks for your help.
Thanks David The H too, unfortunately I could not get any output from your version on the screen, it may be due to the Size line starts with a tab?
Thanks all
|
|
|
|
04-04-2011, 09:54 AM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,320
|
Quote:
|
Thanks David The H too, unfortunately I could not get any output from your version on the screen, it may be due to the Size line starts with a tab?
|
Actually there is a slight error in David's script, but a simple addition fixes it. Also, '*2' should be easy enough for you to include where needed:
Code:
dmidecode -t 17 | awk '( /Size/ && $2 ~ /^[0-9]+$/ ) { x+=$2 } END{ print x }'
|
|
|
1 members found this post helpful.
|
04-04-2011, 10:40 AM
|
#7
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Ah, Thanks grail. I originally posted a simpler version, then decided I should expand the regex to make the test more stringent. I tested it in the shell with the full $2 ~ /^[0-9]+$/ , but I forgot to add that part when I edited my post. Sorry.
|
|
|
|
04-04-2011, 12:00 PM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,320
|
@David - no need for sorry big fella ... I am sure you will point out my omissions when I make them (as I get forgetful now and then too  )
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:58 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|