|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Could you post the command?
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|