Adding numbers in shell script (easy quest)

Adding numbers in shell script (easy quest)

Post by Brien M. Oberste » Mon, 11 Jul 1994 02:33:24



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

-brien

 
 
 

Adding numbers in shell script (easy quest)

Post by Mark Thom » Mon, 11 Jul 1994 03:38:02



: 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...)


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

:) :) :)

 
 
 

Adding numbers in shell script (easy quest)

Post by Johan Svenss » Mon, 11 Jul 1994 04:00:31



Quote:>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

>-brien

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,
__________________________________________________________

                   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

 
 
 

Adding numbers in shell script (easy quest)

Post by Douglas Bridge » Wed, 13 Jul 1994 00:37:01



: >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
: >
: >
: >-brien

: 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

 
 
 

Adding numbers in shell script (easy quest)

Post by Paul Stephen Bori » Wed, 13 Jul 1994 01:01:02



Quote:>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

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
--
----------------------------------------------------------------------------



 
 
 

Adding numbers in shell script (easy quest)

Post by David Thomas Richard Giv » Thu, 14 Jul 1994 17:56:25




Quote:>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`

--
__   _       _    _       _        | GCS -d+(?)(++) p(-+)(---) c++++ !l+(+)
| \ /_\\ /| | \  /   |\ /|_ |\ |   | u++ e*(++) m*(++) s !n h+(++) f+ g+
|_/ | | V | |_/  \_| | V |_ | \|   | w+(+++) t--(+) r y? (Archimedes owner)

 
 
 

Adding numbers in shell script (easy quest)

Post by Tim Lo » Thu, 14 Jul 1994 19:25:27





>>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.