View previous topic :: View next topic |
Author |
Message |
Danrol n00b
Joined: 23 Feb 2003 Posts: 23
|
Posted: Sat May 31, 2003 1:11 pm Post subject: expr And Decimals |
|
|
I'm trying to learn more about BASH scripting, so I've decided to write a script to calculate the cost of fuel for a car journey.
The problem I have is that expr doesn't seem to like decimal numbers. For example, echo `expr 2.5 + 1` returns with 'expr: non-numeric argument'.
After Googling it seems it it should work the way I am trying.
I also tried different combinations of quotes without success....
Can anyone give me suggestion? Am I missing something obvious? |
|
Back to top |
|
|
grant.mcdorman Apprentice
Joined: 29 Jan 2003 Posts: 295 Location: Toronto, ON, Canada
|
Posted: Sat May 31, 2003 3:49 pm Post subject: Re: expr And Decimals |
|
|
Danrol wrote: | I'm trying to learn more about BASH scripting, so I've decided to write a script to calculate the cost of fuel for a car journey.
The problem I have is that expr doesn't seem to like decimal numbers. For example, echo `expr 2.5 + 1` returns with 'expr: non-numeric argument'.
After Googling it seems it it should work the way I am trying.
|
The quoted reference page isn't for the expr program, but for a Perl operator (i.e. it won't work for shell scripting).
The expr program doesn't support decimals - only integers. If you want to decimal arithmetic, try something like this:
Code: | value=`echo 2.5+1 | bc`
echo $value
|
By the way, if you are using bash (or the Korn shell, ksh) then integer arithmetic can be done more effectively by instead of . In addition, the newer way of doing the shell substitution (back quotes) is $(): However, neither of these ways will work with the Bourne shell, which is the standard scripting language on most Unix systems. |
|
Back to top |
|
|
Danrol n00b
Joined: 23 Feb 2003 Posts: 23
|
Posted: Sat May 31, 2003 8:24 pm Post subject: |
|
|
Many thanks for your help, it's all starting to make sense now. |
|
Back to top |
|
|
zhenlin Veteran
Joined: 09 Nov 2002 Posts: 1361
|
Posted: Sun Jun 01, 2003 12:45 am Post subject: |
|
|
I thought integer arithmetic was done using $((1+1))... |
|
Back to top |
|
|
grant.mcdorman Apprentice
Joined: 29 Jan 2003 Posts: 295 Location: Toronto, ON, Canada
|
Posted: Sun Jun 01, 2003 1:08 am Post subject: |
|
|
zhenlin wrote: | I thought integer arithmetic was done using $((1+1))... | It's the same thing. bash tends to incorporate scripting features from many shells, so it may be that one Korn shell variant used $(()), and another $[]. You're correct that the bash man and info pages only mention $(()), though - I'm not sure why. |
|
Back to top |
|
|
|