Adding variables in a unix script

Hi
I am trying to add variables(float values) in a unix script but am getting an error


value=`expr $a + $b + $c`

The error I am getting is "expr: non-numeric argument"
I guess it has got to something with the decimal points.
Plz help

look at the arithmetic evaluation section in the manpage for the shell you are using...
(shell calculations are limited to integer values)

read this post. This works for me

var=$(echo "$a +$b +$c" | bc)

ant:/home/vbe $ echo "$A+$B+$C"
3+4+9.5
ant:/home/vbe $ echo "$A+$B+$C"|bc
16.5

Thanks a lot....:slight_smile:

total=`echo $numone + $numtwo | bc`

worked fine.

There is also one more prob......The variable sometimes contains "" attached to it also eg "23.45".
How to handle this in the addition?It is throwing an error"syntax error on line 1 stdin"
:confused:

ant:/home/vbe $ echo $(expr "23.45")+"$A+$B+$C"|bc
39.95

Tried implementing it but got the same error.

The value "23.45" will be in a variable

a=2
b="23.45" 
c=45.56

total=`echo $a + $(expr b) + $c | bc`

Also tried:
total=`echo $a + $(expr $b) + $c | bc`

:confused:

post the whole script if you could

#!/bin/ksh

a="2.33"
b="4.33"
g=9.7805
value=`echo ""$a"" + $(expr $b) + $g | bc`
echo $value

even this code works for me perfectly....

Hi
Following is my code:

#!/usr/bin/ksh

#Combined_amount=`echo $(expr $Interchange_value) + $(expr $Assesments_value) + $(expr $Transfees_value) + $(expr $TE_value) | bc`

Combined_amount=`echo ""$Interchange_value"" + $(expr $Assesments_value) + $(expr $Transfees_value) + $(expr $TE_value) | bc`

Both are not working.Here Interchange_value will have the vale "23.45" and others float values.

#!/usr/bin/ksh
Interchange_value="23.45"
Assesments_value=34.65
Transfees_value=3
TE_value="9"
Combined_amount=`echo ""$Interchange_value"" + $(expr $Assesments_value) + $(expr $Transfees_value) + $(expr $TE_value) | bc`
echo $Combined_amount

output here

+ Interchange_value=23.45
+ Assesments_value=34.65
+ Transfees_value=3
+ TE_value=9
+ + bc
+ expr 34.65
+ expr 3
+ expr 9
+ echo 23.45 + 34.65 + 3 + 9
Combined_amount=70.10
+ echo 70.10
70.10

whatz your unix?

If I make a sample script and use it.It works perfectly fine.
But does not work in my script.

The only difference is that I am getting these variables from a file.:

Interchange_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f13`
Assesments_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f14`

I think you are getting these values with some other characters (may be spaces)
try to echo each one of them

Put some echo statements.:

Interchange_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f13`
echo "Interchange_value is"
echo $Interchange_value
Assesments_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f14`
echo "Assesment value is"
echo $Assesments_value
Transfees_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f15`
echo $Transfees_value
TE_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f16`
echo $TE_value

The output I got:

Interchange_value is
"-2,387.01"
Assesment value is
-143.71
-8.77
0.00

---------- Post updated at 09:26 AM ---------- Previous update was at 09:20 AM ----------

Tried with

echo "Interchange_value is"
echo "<$Interchange_value>"
Assesments_value=`cat $CHASEFILE |head -$line_counter | tail -1 |cut -f14`
echo "Assesment value is"
echo "<$Assesments_value>"

and got

Interchange_value is
<"-2,387.01">
Assesment value is
<-143.71>
-8.77
0.00