Add Two Variables

I hope this is not a stupid question. I need to add two script variables and expr give me an error message saying "non-numeric argument." But they have numeric values. BTW, I am using Borne shell. Any comments will be appreciated.

Could you post the command?

Sure. Here it is.

#!/bin/sh
A=0
L=0
i=0
for L in `cat abc.txt`
do
i=`expr $i + 1`
if [ $i -eq 28 ]; then
echo "A is $A, and L is $L."
A=`expr $A + $L`
echo "A is $A"
fi
done
exit

So, line 28 of abc.txt has a numeric value and nothing else.

There is a bc command. Use *, /, +, - for mult, division, add, subtract. It works with variables.

Try this.

echo 2*3 | bc > total.out

Assign variables...
sum1=4
sum2=2

echo $sum1*$sum2 | bc > var1
echo $sum1/$sum2 | bc > var2
echo $sum2-$sum1 | bc > var3

Hope this helps!!

:wink:

Thanks for the information about bc. It sounds very promising. I tried bc from the terminal and from simple scripts. But I do get a strange error:

syntax error on line 1, teletype

Any suggestion?

It may be an OS or shell related error. What OS do you have? Mine is HPUX, (posix shell, aka bourne/korn shell)

Post your command. So everyone can see it.

You could try and do chsh to ksh first if you are not using that already.

You can also type bc at the prompt and then it acts as a calculator, type quit to exit...

:cool:

Thanks. I finally found the problem. I was reading a text file and trying to add the value on line 28 to another variable. The problem was the text file was zipped in DOS and then sent to me. So, it contains an extra control code (control-M). Once I delete the control code, bc works great. Really appreciate your help.