Adding variables in a unix script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Adding variables in a unix script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-27-2009
akashtcs akashtcs is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 August 2010, 7:45 AM EDT
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Adding variables in a unix script

Hi
I am trying to add variables(float values) in a unix script but am getting an error


Code:
value=`expr $a + $b + $c`

The error I am getting is "expr: non-numeric argument"
I guess it has got to something with the decimal points.
Plz help
Sponsored Links
    #2  
Old 08-27-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 24 October 2014, 4:42 PM EDT
Location: Switzerland - GE
Posts: 5,835
Thanks: 162
Thanked 415 Times in 390 Posts
look at the arithmetic evaluation section in the manpage for the shell you are using...
(shell calculations are limited to integer values)
Sponsored Links
    #3  
Old 08-27-2009
sagar_evc sagar_evc is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 9 November 2009, 9:22 AM EST
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
http://www.unix.com/shell-programmin...t-numbers.html

read this post. This works for me
    #4  
Old 08-27-2009
protocomm protocomm is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 9 October 2014, 1:36 AM EDT
Location: /france/LR/vendargues/home
Posts: 266
Thanks: 28
Thanked 17 Times in 17 Posts

Code:
var=$(echo "$a +$b +$c" | bc)

Sponsored Links
    #5  
Old 08-27-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 24 October 2014, 4:42 PM EDT
Location: Switzerland - GE
Posts: 5,835
Thanks: 162
Thanked 415 Times in 390 Posts
ant:/home/vbe $ echo "$A+$B+$C"
3+4+9.5
ant:/home/vbe $ echo "$A+$B+$C"|bc
16.5
Sponsored Links
    #6  
Old 08-27-2009
akashtcs akashtcs is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 August 2010, 7:45 AM EDT
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot....

total=`echo $numone + $numtwo | bc`

worked fine.

There is also one more prob......The variable sometimes contains "" attached to it also eg "23.45".
How to handle this in the addition?It is throwing an error"syntax error on line 1 stdin"
Sponsored Links
    #7  
Old 08-27-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 24 October 2014, 4:42 PM EDT
Location: Switzerland - GE
Posts: 5,835
Thanks: 162
Thanked 415 Times in 390 Posts
ant:/home/vbe $ echo $(expr "23.45")+"$A+$B+$C"|bc
39.95
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
Script that takes in variables, and outputs to another text or script file orozcom Shell Programming and Scripting 1 05-19-2009 03:59 PM
sqlplus session being able to see unix variables session within a script 435 Gavea Shell Programming and Scripting 2 07-03-2006 10:11 AM
adding variables for, for loop jazz Programming 3 03-13-2006 03:23 AM
Adding a New IP Addresses to a SCO Unix Box dman110168 IP Networking 1 02-17-2002 12:57 PM
adding printer in unix bong m UNIX for Dummies Questions & Answers 2 12-07-2000 01:28 PM



All times are GMT -4. The time now is 07:54 AM.