Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 11-24-2007
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 447 Times in 264 Posts
We need to see the rest of the script to tell why that statement was unexpected.
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 11-24-2007
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 24 April 2015, 9:36 PM EDT
Location: /dev/ph
Posts: 4,914
Thanks: 68
Thanked 453 Times in 421 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 04:02 PM..
Sponsored Links
    #5  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 04:38 PM
Help with addition of 2 numbers that are read from a file marmik1903 Shell Programming and Scripting 1 08-30-2010 12:13 PM
Addition of floating numbers dear_abhi2007 Shell Programming and Scripting 4 11-17-2009 05:09 AM
Addition of all the numbers in a single file chinni-script Shell Programming and Scripting 5 04-05-2009 08:46 AM
Addition of numbers in unix asinha63 Shell Programming and Scripting 3 03-14-2006 11:38 AM



All times are GMT -4. The time now is 01:04 PM.