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.