It is currently Tue, 30 Nov 2021 18:14:13 GMT



 
Author Message
 aritmetic functions in Bourne shell
Hi, I'm new with shell programming.  I'm having trouble to do some very
simple aritmetic calculation.  For example:

#!/usr/bin/sh

A=5
B=9
c=$($B-$A)
echo $C

When I run this script, I got error:

syntax error at line 3: `c=$' unexpected

Please let me know what would be the right way to do what I try to do.
Thank you!

Sent via Deja.com http://www.**-**.com/
Before you buy.



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

There are some shells that give you syntatic sugar for artithmetic, but
true Bourne shell only uses "expr":

A=5
B=9
C=`expr $A + $B`
echo $C

-Derek



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

You wanted to write

c=$(($A-$B))

In general: $((expression))

/A

--
# Andreas K?h?ri, <URL:http://hello.to/andkaha/>.
# ...brought to you from Uppsala, Sweden.
# All junk e-mail is reported to the appropriate authorities.
# Criticism, cynicism and irony available free of charge.



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell
You must use the expr command for arithmetic functions in the bourne shell.

For example:

C=`expr $B - $A`

That will give you what you want.



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

uh...no.  Not at all necessary.

use square brackets instead of parentheses.  The line should be

c=$[$B-$A]

here you want a lower case 'c'

echo $c

- Show quoted text -



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

Whatchewbeensmokin?

Square brackets here do nothing in Bourne shell, and will be
treated literally.

Expr will work as noted above, as will 'bc'

echo "$B - $A" | bc -l # omit the '-l' for integer math only

-----------------------------------------------------------

Got questions?  Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

The stuff that makes bourne look like bash  8^(



 Sun, 26 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

you are trying Korn shell stuff under Bourne shell.

Bourne Shell (sh)
------------

c=`expr $B - $A`

Korn Shell (ksh)
----------

integer c
c=$((b - a))

or :

integer c
(( c = b - a ))

or :

integer c
let c=b-a

Bourne Again Shell (bash)
------------------

same as the Korn Shell plus the following deprecated syntax :

c=$[b - a]

Posix Shell (sh) ?
-----------

c=$(( $b - $a ))

bourne/posix sh requires $variable syntax while ksh/bash doesn't
need $variable syntax while accepted. bourne shell form always
works but is not efficient at all under other shells.

Cyrille.
--
home: mailto:clefevre%no-s...@citeweb.net.invalid
work: mailto:Cyrille.Lefevre%no-s...@edf.fr.invalid
Supprimer "%no-spam" et ".invalid" pour me repondre.
Remove "%no-spam" and ".invalid" to answer me back.



 Mon, 27 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell
On 10 Aug 2000 05:01:42 +0200, Cyrille Lefevre <clefevre%no-s...@citeweb.net.invalid> wrote:

One minor point: the $(( )) syntax does permit you to omit the <$>s
from the variable names:
  c=$(( b - a ))

Otherwise, the summary looks good.

                --Ken Pizzini



 Mon, 27 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

using FreeBSD /bin/sh (posix?), it is require to use $var, not just var.

also, look at these pointers for more informations :

http://www.opengroup.org/onlinepubs/7908799/xcu/chap2.html#tag_001_00...
http://www.opengroup.org/onlinepubs/7908799/xcu/chap2.html#tag_001_003
http://www.opengroup.org/onlinepubs/7908799/xcu/chap2.html#tag_001_006
http://www.opengroup.org/onlinepubs/7908799/xcu/chap2.html#tag_001_00...

thanks.

Cyrille.
--
home: mailto:clefevre%no-s...@citeweb.net.invalid
work: mailto:Cyrille.Lefevre%no-s...@edf.fr.invalid
Supprimer "%no-spam" et ".invalid" pour me repondre.
Remove "%no-spam" and ".invalid" to answer me back.



 Mon, 27 Jan 2003 03:00:00 GMT   
 aritmetic functions in Bourne shell

The $(..) construct does not exist in Bourne shell. "man
sh" for what it can do. Something not mentioned in the
man page in all probability does not exist. That's why
they write man pages -- to tell you what's there. "man
expr", "man dc", "man bc" for ways to do arithmetic in
Bourne shell.



 Mon, 27 Jan 2003 03:00:00 GMT   
 
   [ 11 post ] 

Similar Threads

1. Using aritmetic functions in KSH

2. Light Speed Bourne Shell! (was: Bourne shell tricks)

3. Bourne shell string length function?

4. Bourne Shell Function Syntax

5. Bourne shell: Testing the existance of a function?

6. Bourne shell string length function?

7. Bourne shell returning a string from a function

8. Bourne shell function to check variables?

9. Learning about math/algebraic functions (Bourne shell/SCO)

10. functions in Bourne shell


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software