How to perform addition of two numbers in shell scripting in Solaris-10 | Unix Linux Forums | Solaris

  Go Back    


Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

How to perform addition of two numbers in shell scripting in Solaris-10

Solaris


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-24-2007
krevathi1912 krevathi1912 is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 3 December 2007, 7:36 AM EST
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Error How to perform addition of two numbers in shell scripting in Solaris-10

Hi,

I have a sh script which contains the following line

TOTAL=$((e4-s4)) -> Where e4 and s4 are input got from the user.

At the time of execution of this line the following error occurs

test.sh: syntax error at line 8: `TOTAL=$' unexpected


How to solve this issue?. Can any one help me please?.........................



Regards
Revathi
Sponsored Links
    #2  
Old 11-24-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 November 2014, 10:48 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
We need to see the rest of the script to tell why that statement was unexpected.
Sponsored Links
    #3  
Old 11-24-2007
prowla prowla is offline
Read Only
 
Join Date: Nov 2007
Last Activity: 16 July 2012, 4:02 PM EDT
Posts: 165
Thanks: 0
Thanked 2 Times in 2 Posts
I don't know the $((...)) syntax, but it could be from a more exotice shell and not recognised by the Bourne shell.
This should work (for integer arithmetic):
let total=e4-s4
    #4  
Old 11-24-2007
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 25 November 2014, 10:51 AM EST
Location: /dev/ph
Posts: 4,836
Thanks: 64
Thanked 434 Times in 402 Posts
The following is more portable:

TOTAL=`expr $e4 - $s4`

From the syntax $((expression)) it looks like your script was probably written to take advantage of
specific features in the bash shell. As a result your shell script will not be very portable to
non-Linux/GNU systems unless bash is available on these systems.

Last edited by fpmurphy; 11-24-2007 at 05:02 PM..
Sponsored Links
    #5  
Old 11-26-2007
krevathi1912 krevathi1912 is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 3 December 2007, 7:36 AM EST
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Perderabo View Post
We need to see the rest of the script to tell why that statement was unexpected.

Below is my script

#!/bin/bash
echo "Enter the start IP"
IFS="."
read s1 s2 s3 s4
echo "Enter the End IP"
IFS="."
read e1 e2 e3 e4
TOTAL=$(($e4 - $s4))
echo "Total is $TOTAL"


Any idea
Sponsored Links
    #6  
Old 11-26-2007
krevathi1912 krevathi1912 is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 3 December 2007, 7:36 AM EST
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Error

Quote:
Originally Posted by prowla View Post
I don't know the $((...)) syntax, but it could be from a more exotice shell and not recognised by the Bourne shell.
This should work (for integer arithmetic):
let total=e4-s4
You suggestion does not work.
Below is my script

#!/bin/bash
echo "Enter the start IP"
IFS="."
read s1 s2 s3 s4
echo "Enter the End IP"
IFS="."
read e1 e2 e3 e4
TOTAL=$(($e4 - $s4))
echo "Total is $TOTAL"

Any idea
Sponsored Links
    #7  
Old 11-26-2007
krevathi1912 krevathi1912 is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 3 December 2007, 7:36 AM EST
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Error

Quote:
Originally Posted by fpmurphy View Post
The following is more portable:

TOTAL=`expr $e4 - $s4`

From the syntax $((expression)) it looks like your script was probably written to take advantage of
specific features in the bash shell. As a result your shell script will not be very portable to
non-Linux/GNU systems unless bash is available on these systems.




You suggestion does not work.
Below is my script

#!/bin/bash
echo "Enter the start IP"
IFS="."
read s1 s2 s3 s4
echo "Enter the End IP"
IFS="."
read e1 e2 e3 e4
TOTAL=$(($e4 - $s4))
echo "Total is $TOTAL"

Any idea
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
addition of two numbers lookinginfo Shell Programming and Scripting 1 10-05-2011 05:38 PM
Help with addition of 2 numbers that are read from a file marmik1903 Shell Programming and Scripting 1 08-30-2010 01:13 PM
Addition of floating numbers dear_abhi2007 Shell Programming and Scripting 4 11-17-2009 06:09 AM
Addition of all the numbers in a single file chinni-script Shell Programming and Scripting 5 04-05-2009 09:46 AM
Addition of numbers in unix asinha63 Shell Programming and Scripting 3 03-14-2006 12:38 PM



All times are GMT -4. The time now is 03:37 AM.