LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   floating-point in script (https://www.linuxquestions.org/questions/programming-9/floating-point-in-script-478964/)

mitsos 08-30-2006 09:41 PM

floating-point in script
 
hi
i'm writting a script that contains the following variable
size2=$(($size1 + $size2))
but when it tries to add numbers like 6.1
all i get is syntax error in expression (error token is ".1")

i tried bc
size2=$(($size1 + $size2)) | bc
and i get the same

any hints?

kaz2100 08-30-2006 09:49 PM

Which language?? or shell script??

Happy Penguins!!

mitsos 08-30-2006 09:53 PM

Quote:

Originally Posted by kaz2100
Which language?? or shell script??

Happy Penguins!!

shell script

ilikejam 08-30-2006 10:15 PM

Hi.

Your bc command should look like:
Code:

size2=`echo "$size1 + $size2" | bc`
Dave

mitsos 08-30-2006 10:28 PM

thanx dave for your reply
but i already tried that and it produces the same error!!!

KenJackson 08-31-2006 06:00 AM

Code:

#!/bin/sh
size1=3.5 size2=2.6
size2=$(echo $size1 + $size2 | bc)
echo $size2

It prints 6.1 for me.

mitsos 08-31-2006 07:14 AM

thanx but i just rewrite it all in korn and worked.
Code:

#!/bin/ksh
size2=$((size1 + size2))

So, if you're going to do math, better use ksh. :)


All times are GMT -5. The time now is 07:10 PM.