Add Two Variables | 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.

Add Two Variables

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-02-2002
cdin2 cdin2 is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 3 May 2002, 9:22 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Add Two Variables

I hope this is not a stupid question. I need to add two script variables and expr give me an error message saying "non-numeric argument." But they have numeric values. BTW, I am using Borne shell. Any comments will be appreciated.
Sponsored Links
    #2  
Old 05-03-2002
Kevin Pryke Kevin Pryke is offline
Registered User
 
Join Date: Feb 2002
Last Activity: 5 November 2012, 12:05 PM EST
Location: Bradford, UK
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Could you post the command?
Sponsored Links
    #3  
Old 05-03-2002
cdin cdin is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 20 May 2002, 9:19 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Sure. Here it is.

#!/bin/sh
A=0
L=0
i=0
for L in `cat abc.txt`
do
i=`expr $i + 1`
if [ $i -eq 28 ]; then
echo "A is $A, and L is $L."
A=`expr $A + $L`
echo "A is $A"
fi
done
exit

So, line 28 of abc.txt has a numeric value and nothing else.
    #4  
Old 05-03-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
 
Join Date: Aug 2001
Last Activity: 18 April 2013, 3:03 PM EDT
Location: San Antonio, TX,
Posts: 1,070
Thanks: 0
Thanked 4 Times in 4 Posts
bc command, if you have it...

There is a bc command. Use *, /, +, - for mult, division, add, subtract. It works with variables.

Try this.

echo 2*3 | bc > total.out


Assign variables...
sum1=4
sum2=2

echo $sum1*$sum2 | bc > var1
echo $sum1/$sum2 | bc > var2
echo $sum2-$sum1 | bc > var3


Hope this helps!!


Sponsored Links
    #5  
Old 05-03-2002
cdin cdin is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 20 May 2002, 9:19 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the information about bc. It sounds very promising. I tried bc from the terminal and from simple scripts. But I do get a strange error:

syntax error on line 1, teletype

Any suggestion?
Sponsored Links
    #6  
Old 05-03-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
 
Join Date: Aug 2001
Last Activity: 18 April 2013, 3:03 PM EDT
Location: San Antonio, TX,
Posts: 1,070
Thanks: 0
Thanked 4 Times in 4 Posts
Does it still work?

It may be an OS or shell related error. What OS do you have? Mine is HPUX, (posix shell, aka bourne/korn shell)

Post your command. So everyone can see it.

You could try and do chsh to ksh first if you are not using that already.

You can also type bc at the prompt and then it acts as a calculator, type quit to exit...


Sponsored Links
    #7  
Old 05-03-2002
cdin cdin is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 20 May 2002, 9:19 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks. I finally found the problem. I was reading a text file and trying to add the value on line 28 to another variable. The problem was the text file was zipped in DOS and then sent to me. So, it contains an extra control code (control-M). Once I delete the control code, bc works great. Really appreciate your help.
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
sed with variables olkkis Shell Programming and Scripting 10 04-19-2012 04:35 AM
Using Variables with SED miyoung999 Shell Programming and Scripting 1 01-21-2011 03:39 PM
Using variables in SSH tsryn Shell Programming and Scripting 1 03-30-2009 04:58 AM
How to convert byteArray variables to HexaString variables for Linux? ritesh_163 Programming 2 08-10-2008 11:55 PM
naming variables with variables Allasso Shell Programming and Scripting 2 06-27-2008 10:45 AM



All times are GMT -4. The time now is 06:11 AM.