Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
expr And Decimals
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Danrol
n00b
n00b


Joined: 23 Feb 2003
Posts: 23

PostPosted: Sat May 31, 2003 1:11 pm    Post subject: expr And Decimals Reply with quote

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
View user's profile Send private message
grant.mcdorman
Apprentice
Apprentice


Joined: 29 Jan 2003
Posts: 295
Location: Toronto, ON, Canada

PostPosted: Sat May 31, 2003 3:49 pm    Post subject: Re: expr And Decimals Reply with quote

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
Code:
echo $[2+1]
instead of
Code:
echo `expr 2+1`
. In addition, the newer way of doing the shell substitution (back quotes) is $():
Code:
echo $(expr 2+1)
However, neither of these ways will work with the Bourne shell, which is the standard scripting language on most Unix systems.
Back to top
View user's profile Send private message
Danrol
n00b
n00b


Joined: 23 Feb 2003
Posts: 23

PostPosted: Sat May 31, 2003 8:24 pm    Post subject: Reply with quote

Many thanks for your help, it's all starting to make sense now.
Back to top
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Sun Jun 01, 2003 12:45 am    Post subject: Reply with quote

I thought integer arithmetic was done using $((1+1))...
Back to top
View user's profile Send private message
grant.mcdorman
Apprentice
Apprentice


Joined: 29 Jan 2003
Posts: 295
Location: Toronto, ON, Canada

PostPosted: Sun Jun 01, 2003 1:08 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum