1752797 Members
5871 Online
108789 Solutions
New Discussion юеВ

Addition

 
SOLVED
Go to solution
Qcheck
Super Advisor

Addition

Hi All,

I have a script which I get the line counts from three servers. For an instance, I get the output from the script 30, 34 and 37. I wanted to add these three numbers dynamically in the script itself so that I don't need to add manually. I know I can use "bc" command from unix prompt but I want add the number what I get from the output in the script itself. Any help is greatly appreciated?

Thanks,
Pratibha
9 REPLIES 9
Patrick Wallek
Honored Contributor

Re: Addition

An example of how the script is run and how you get the output would be helpful.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Addition

As long as the values are integers and are in the range -2^31 - +2^31 - 1 then it's quite simple:

typeset -i A=30
typeset -i B=34
typeset -i C=37
typeset -i TOT=0
TOT=$(( ${A} + ${B} + ${C} ))
echo "Total: ${TOT}"

You can also call the external command expr to add these values but the POSIX and Korn shell will handle this all internally.
If it ain't broke, I can fix that.
Qcheck
Super Advisor

Re: Addition

Please check the script I am using:

#!/bin/ksh

NODES="xx yy zz"
for i in $NODES
do

{

echo " "
echo "*************************${i}****************************"
echo " "
rsh ${i} "ps -ef|grep ora | wc -l"

}
done

# THE END!
A. Clay Stephenson
Acclaimed Contributor

Re: Addition

Easy:

#!/usr/bin/ksh

typeset NODES="xx yy zz"
typeset -i=""
typeset -i TOT=0
typeset -i K=0
for i in ${NODES}
do
{
echo " "
echo "*************************${i}****************************"
echo " "
K=$(remsh ${i} ps -ef | grep ora | wc -l)
((TOT+=K))
}
done
echo "Total: ${TOT}"

If it ain't broke, I can fix that.
Qcheck
Super Advisor

Re: Addition

Hi Clay,

Thank you for the response but erroring out at typeset, saying typeset: bad options. Any ideas?

Thanks
A. Clay Stephenson
Acclaimed Contributor

Re: Addition

This is legal Korn/POSIX shell syntax; I suspect that you are posting a non-HPUX question in an HP-UX forum. I got that idea from /bin/ksh which is the wrong place. It should be /usr/bin/ksh. In any event, change the
typeset -i K=0
to simply K=0

The typeset is optional but it does make the computations more efficient.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Addition

oops, this was a bad line:

typeset -i=""

It should be
typeset i=""
If it ain't broke, I can fix that.
Qcheck
Super Advisor

Re: Addition

Thank you Steve, it is working now. Thanks a lot for the sooner help.

Thanks again,
Pratibha
Arturo Galbiati
Esteemed Contributor

Re: Addition

Hi Pratibha,
looking at your script I found an error: using ps -ef|grep ora|wc -l we include in the count also the command line. To avoid this use: ps -ef|grep [o]ra|wc -l. In this way we will the correct counter.
But if your goal is know the numnber of Oracle process this is wrong because using ora you got also all teh process for user oracle.
Just my .02$
HTH,
Art