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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 08-27-2009
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 27 March 2015, 5:52 AM EDT
Location: Switzerland - GE
Posts: 6,023
Thanks: 182
Thanked 452 Times in 423 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 Unix and Linux 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 Unix and Linux 08-27-2009
protocomm protocomm is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 27 March 2015, 9:50 AM EDT
Location: /france/LR/vendargues/home
Posts: 288
Thanks: 32
Thanked 18 Times in 18 Posts

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

Sponsored Links
    #5  
Old Unix and Linux 08-27-2009
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 27 March 2015, 5:52 AM EDT
Location: Switzerland - GE
Posts: 6,023
Thanks: 182
Thanked 452 Times in 423 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 Unix and Linux 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....Unix or Linux Image

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"
Unix or Linux Image
Sponsored Links
    #7  
Old Unix and Linux 08-27-2009
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 27 March 2015, 5:52 AM EDT
Location: Switzerland - GE
Posts: 6,023
Thanks: 182
Thanked 452 Times in 423 Posts
ant:/home/vbe $ echo $(expr "23.45")+"$A+$B+$C"|bc
39.95
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
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 01:19 PM.