Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 4 Dec 1996 12:04:28 +0100 (MET)
From:      Robert Eckardt <roberte@mep.ruhr-uni-bochum.de>
To:        sln@public.jn.sd.cn
Cc:        questions@freebsd.org
Subject:   Re: shell programming
Message-ID:  <199612041104.MAA16455@ghost.mep.ruhr-uni-bochum.de>
In-Reply-To: <Pine.SOL.3.91.961204164334.2113A-100000@public.jn.sd.cn> from "sln@public.jn.sd.cn." at "4. Dec. 96 16:49:58"

next in thread | previous in thread | raw e-mail | index | archive | help
> Hi,
> 
> I want to calculate intergers under Bourne Shell, I input the following:
> 
> a=10 <enter>
> a='expr $a+10' <enter>
> 
> when I typed "echo $a", the result from system is "expr $a+1" just the 
> same as I typed it in the command, but as the "expr' manul says, the result 
> should be "20", what's the problem? 

You have to be very careful what you type.
$ a=10
$ a=`expr $a + 10`
$ echo $a
20

First, notice the difference between ' and `.
Then you have to put spaces around operands and operators of expr.

In csh you would type
% @ a = 10
% echo $a
10
% @ a = $a + 10
% echo $a
20
% @ a += 10
% echo $a
30

> Further more, I want to know a general way to do some calculation using 
> variables under unix shell.

What do you mean ?
What type of calculation ?
Large integers ?
Floating point ?

For simple integer arithmetic you can use expr, [t]csh (which has integer
arithmetic build in). For more complex or FP calculations you can use a
command language like perl or a calculator language like dc or bc ...

> Song

Robert

-- 
Robert Eckardt                \\ FreeBSD -- solutions for a large universe.(tm)
RobertE@MEP.Ruhr-Uni-Bochum.de \\       What do you want to boot tomorrow ?(tm)
http://WWW.MEP.Ruhr-Uni-Bochum.de/~roberte
For PGP-key finger roberte@gluon.MEP.Ruhr-Uni-Bochum.de



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199612041104.MAA16455>