LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 04-04-2011, 05:22 AM   #1
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 64

Rep: Reputation: 18
Question 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
 
Old 04-04-2011, 09:18 AM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 134

Rep: Reputation: 30
dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } END { print RAM }'
 
1 members found this post helpful.
Old 04-04-2011, 09:27 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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 09:38 AM. Reason: minor change for more accuracy
 
1 members found this post helpful.
Old 04-04-2011, 09:56 AM   #4
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 64

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by tshikose View Post
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.
 
Old 04-04-2011, 09:59 AM   #5
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 64

Original Poster
Rep: Reputation: 18
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
 
Old 04-04-2011, 10:54 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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.
Old 04-04-2011, 11:40 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
Old 04-04-2011, 01:00 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
@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 )
 
  


Reply

Tags
awk, shell script


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
installed system memory differs from mounted (available) memory landroni Debian 5 08-13-2007 07:43 AM
?? installed memory is not recognized kaz2100 Linux - General 6 07-13-2006 02:38 PM
calcualte the execution time of a function George2 Programming 4 06-15-2006 04:13 AM
How much memory is installed? xymian Linux - General 2 10-03-2005 03:07 AM
Memory reported less than installed (Mandrake 10) vo24 Linux - Hardware 17 03-29-2005 04:02 PM


All times are GMT -5. The time now is 01:04 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration