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 }' 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 |
dmidecode -t 17 | grep Size | awk ' { RAM+=$2 } END { print RAM }'
|
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 }' Code:
variablename="$( commands )" 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 |
Quote:
Can you let me know how to take the total and multiply it by 2. |
Hello
I managed to get the result using: 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? Thanks all |
Quote:
Code:
dmidecode -t 17 | awk '( /Size/ && $2 ~ /^[0-9]+$/ ) { x+=$2 } END{ print x }' |
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.
|
@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. |