LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 04:22 AM   #1
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 70

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, 08:18 AM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } END { print RAM }'
 
1 members found this post helpful.
Old 04-04-2011, 08:27 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
Old 04-04-2011, 08:56 AM   #4
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 70

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, 08:59 AM   #5
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 70

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, 09:54 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 10:40 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 12:00 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 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
Open Source Consulting | Domain Registration