Question

Can somebody please help me to write a KSH script to calculate the percentage of memory used in AIX server?

Here are my requirements

  1. I want my script to execute the svmon -G command and my script should cut the first 2 values
  2. My script should divide 3rd value by 1st value and result should be multiplied by 100 to get percentage of the memory used.

Below is the sample script which has already been written.

#!bin/ksh
echo $(
    svmon -G | grep memory | 
        sed 's/  */ /g' | 
        cut -d" " -f 3
)/$(
    svmon -G | grep memory | 
        sed 's/  */ /g' | 
        cut -d" " -f 2
)

I am getting below Output

4915483/ 7864320

But I need the result of the division operation as output

Was it helpful?

Solution 2

You can take the result of your command and pipe it into bc :

echo "scale=16; 4915483/ 7864320" | bc

This results in

.6250359853108723

So in your case the following should work (edited to show multiplication by 100):

echo "scale=16; 100 * " $(svmon -G | grep memory | sed 's/  */ /g' | cut -d" " -f 3)/$(svmon -G | grep memory | sed 's/  */ /g' | cut -d" " -f 2) | bc

I have added 100 * after the scale=16; so you get the result multiplied by 100.

You can change the scale factor to change the number of significant digits in the division (by default bc works with integer math). 16 is probably far more than you need for a percentage, especially after multiplying by 100. Experiment until you get what you like.

** other thought ** I think that awk is a MUCH better tool for this. I don't have svmon available on my machine (Mac OS) but based on what you wrote I think I can figure out that it produces multiple lines, one of which has the word memory in it. And you want to compute the ratio of two fields (it seems you divide field 3 by field 2). In awk that would be:

svmon -G | awk '{if($0 ~ /memory/) {printf("memory used: %.1f%%\n", 100.0 * $3 / $2)}}'

I think it is much more compact... you may need to tweak it - I'm not sure I got the field numbers right. As I said, I don't have svmon.

one more update

To get the result in a shell variable, you can take your entire command and wrap it in a $() construct, like so:

memPct=$(echo "scale=1; 100 * " $(svmon -G | grep memory | sed 's/  */ /g' | cut -d" " -f 3)/$(svmon -G | grep memory | sed 's/  */ /g' | cut -d" " -f 2) | bc)
echo $memPct "%"

will put the percentage in the shell variable $memPct, then print it out with a % sign after it...

Note - it is important that you don't have any spaces in the first part of this command or it won't work.

OTHER TIPS

This is much easier in awk, nawk* or mawk:

svmon() { [[ $1 == -G ]] || return

echo memory 7864320 4915483 junk
echo foo bar baz
echo

}   

test_it() {
    svmon -G | nawk '/memory/ {
        print $3 / $2; exit
    }'
} && test_it

This sample includes a mock function for those of us without svmon. The actual solution is in the three lines of code in the function test_it

test_it() {
    svmon -G | nawk -v OFMT="%.12f" '/memory/ {
        print $3 / $2; exit
    }'
} && test_it

Increase precision with OFMT, see nawk(1)


* nawk is New Awk, also sometimes called the 'one true awk'. It's the 1985 revision of the 1977 original awk (after that named oawk). Nawk is the basic reference for the language and its features have been adopted by many others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top