-brien
: Here's an easy question, how do you add numbers in a shell script?
: I want to hav a script which does something like echo '$2+20'
: ie add 20 to the value of the argument $2
In the c-shell ( csh ) :
(btw, variable names must begin with a letter...)
set $foo = 2;
@ foo = $foo + 20;
echo $foo;
(produces: 22)
In the bourne shell ( sh ) :
foo=2;
foo=`expr $foo + 20`;
echo $foo;
(produces: 22)
Hope it helps,
-mark
--
Mark Thomas
Assistant System Administrator | Student System Administrator
O'Reilly & Associates, Inc. | Boston University CS Department
90 Sherman Street | 111 Cummington Street
Cambridge, MA 02140 | Boston, MA 02215
ma...@ora.com | mth...@cs.bu.edu
:) :) :)
Try this:
echo `expr $2 + 20`
PLEASE NOTE: The "'" should be typed the other way around
("accent grave" in French)!
Unfortunately, my News editor wouldn't let me
do that trick...
Regards,
__________________________________________________________
Johan Svensson Email: jo...@ECSDG.lu.se
Tel: +46-46-104505
EkonomiCentrum Software Development Group, Lund University
URL: <A HREF="http://ecsdg.lu.se/">ECSDG Home Page</A>
----------------------------------------------------------
Address: JoS-Ware Comp Tech, Box 739, 220 07 LUND, SWEDEN
Email: JoS-...@kuai.se Fax: +46-46-188445
: Try this:
: echo `expr $2 + 20`
This works in the ksh, not sure about others :
echo $(($2 + 20))
unlike the example about this doesn't create a new process.
cheers
Doug
Try "man expr". You will find out that it is as simple as :
b=`expr $2 + 20`
echo $b
expr(1) also does a lot of other things.
-Paul
--
----------------------------------------------------------------------------
Paul Stephen Borile email : pa...@cusun.sublink.org
: pa...@sixcom.it
: pa...@ghost.dsi.unimi.it
Kludge time!
Try:
a=`bc $2 + 20`
--
__ _ _ _ _ | GCS -d+(?)(++) p(-+)(---) c++++ !l+(+)
| \ /_\\ /| | \ / |\ /|_ |\ | | u++ e*(++) m*(++) s !n h+(++) f+ g+
|_/ | | V | |_/ \_| | V |_ | \| | w+(+++) t--(+) r y? (Archimedes owner)
dt...@st-andrews.ac.uk | ***In search of better V's***
>In article <2vmn14$r...@gap.cco.caltech.edu>,
>Brien M. Oberstein <br...@cco.caltech.edu> wrote:
>>Here's an easy question, how do you add numbers in a shell script?
>>I want to hav a script which does something like echo '$2+20'
>>ie add 20 to the value of the argument $2
>Kludge time!
>Try:
>a=`bc $2 + 20`
And the korn shell has a builtin called 'let' to do arithmetic.