Operating System - HP-UX
1833059 Members
2469 Online
110049 Solutions
New Discussion

Re: how can add two variables?

 
mariano_4
Occasional Contributor

how can add two variables?

Example:

in the next scrip i have the next script:

-----------------------------------------
#Progrma que calcula el total de memoria #utilizada

ps aux |cut -b 34-37 |grep K | awk '{s=s+$1}\{r=s/1024}END{print r,export $r}'

ps aux | cut -b 34-37 | grep M | awk '{t=t+$1}\END{print t}'

-----------------------------------------

but i need sum both and print the result
how can doing it?

thanks
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: how can add two variables?

Mariano,

TIMEOUT


export $r ???

Might work in LINUX but not in any UNIX that I know of, UNLESS you plan on installing GNU's AWK!


Now, how to do it:

ps aux | cut -b 34-37 | grep -e K -e M |
awk '{lenofvar=length($1);
if (substr($1,lenofvar,1) eq "K") {s=s+$1;r=s/1024}
if (substr($1,lenofvar,1) eq "M") {t=t+$1}
}
END{printf "%d/n",r;
print t}'

Can't test it, but looks good???



live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: how can add two variables?


I'd like you to take a look at this thread. It helps you and everyone else in the future:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x022718276953d61190040090279cd0f9,00.html

And I'd like to add that you need to be more interactive with your thread so that people know what works and what doesn't.


live free or die
harry
Live Free or Die
Frank Slootweg
Honored Contributor

Re: how can add two variables?

First you have to assign the output to the variables, then add them, then print the sum.

Assign:
variable=`command`
or
variable=$(command)

Add:
((result=$var1+$var2))

Print:
echo $result
Allan Pincus
Frequent Advisor

Re: how can add two variables?

Postscript to Frank's email:

((result = var1 + var2))

or

let result=var1+var2

are also both acceptable.

-Allan
Martin Johnson
Honored Contributor

Re: how can add two variables?

Harry,

Nicely, put! You are getting more diplomatic in your old age? Or are you getting tired of the flaming tirades?

:-)
Marty