getting error with expr

sum=0;
cat op_api2 |while read word1 word2;
do
echo $word2
sum=`expr $word2 + $sum`;
done
echo $sum

op_api2 ( file has this data )
----------------------------
UsageSummary 1034
UsageSummary 1675
UnbilledUsage 175
UnbilledUsage 177
UnbilledUsage 177
UnbilledUsage 194

I want the total of second column. However am getting :- "expr: Syntax error".
Please let me know where is the error.

Hi,

I have executed the same under k shell and it works fine for me.

Which shell are you using?

Regards,
Chella

Hi.

I agree with Chella, it also worked for me, in that I did not get an expr error. I used the bash shell.

However, the sum was reported as zero. This is because a pipe-lined set of commands ( cat ... | while ... in your case) will be done in separate processes. This might be dependent on the shell; I have heard that the real Korn shell -- ksh -- does not exhibit this behavior. I tried it with pdksh and got a sum of zero. The variables in child processes are not available to the parent.

The usual solution to such a situation is to re-form the construction as:

while ...
do
... 
done < input_file

Best wishes ... cheers, drl