Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file
i want to multiply this input with value .43, and assign it to variable b.
i tried it as below:

#!/bin/sh
a=$1
b=`expr $1\*0.43`
echo b=$b

error : expr: non-integer argument

Please tell me , how to do this.

Thanks

This way:

#!/bin/sh

a=$1
b=$(echo "$a * 0.43"|bc)
echo $b

Hi Klashxx,

if i try that code, it gives error:

./test: syntax error at line 5: `b=$' unexpected

If you're using an old SH (probably on Solaris), you might have to revert to using backquotes, as in your original code

b=`...`

You can also use printf in place of bc

b=`printf "%.2f"  $1*0.43`

Hi Scott,

It would b great if u correct this code itself and show me:

#!/bin/sh  a=$1 b=$(echo "$a * 0.43"|bc) echo $b

I already did, or so I thought. Do you have all that code on one line? If so, why? If not, please post it as it is in your script. We don't need to be second guessing exactly what you have written.

It would be nice if you used Code Tags so I wouldn't have to spend my time adding them for you.

#!/bin/sh

a=$1
b=$(echo "$a * 0.43"|bc)
echo $b

---------- Post updated at 04:04 PM ---------- Previous update was at 04:00 PM ----------

#!/bin/sh

a=$1
b=`printf "%.2f"  $1*0.43`
##b=$(echo "$a * 0.43"|bc)
echo $b

Result:

printf: 10*0.43: value not completely converted
10.00

expected ans 4.3

But did you try Klashxx's solution with backquotes?

#!/bin/sh

a=$1
b=`echo "$a * 0.43"|bc`
echo $b

hey,

It worked :smiley:

Thank you very much Klashxx and Scott

... could utilize awk, which natively handles floats:

b=`echo $1|awk '{print $0*.43}'`

... my bad, missed the second page where this had been resolved ...

Thanks Jethrow, ur suggestion works :smiley:

#!/bin/sh

a=$1
c=$2
##b=`printf "%.2f"  $1*0.43`
b=`echo $1|awk '{print $0*.43}'`
##b=`echo "$a * $c"|bc`
echo $b