LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to calcualte installed memory. (https://www.linuxquestions.org/questions/programming-9/script-to-calcualte-installed-memory-872827/)

pobrika 04-04-2011 04:22 AM

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

tshikose 04-04-2011 08:18 AM

dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } END { print RAM }'

David the H. 04-04-2011 08:27 AM

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

pobrika 04-04-2011 08:56 AM

Quote:

Originally Posted by tshikose (Post 4313326)
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.

pobrika 04-04-2011 08:59 AM

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

grail 04-04-2011 09:54 AM

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 }'

David the H. 04-04-2011 10:40 AM

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.

grail 04-04-2011 12:00 PM

@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 :) )


All times are GMT -5. The time now is 07:59 AM.