(+ sign) Unexpected arithmetic expr script interpretation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers (+ sign) Unexpected arithmetic expr script interpretation
# 1  
Old 09-19-2006
(+ sign) Unexpected arithmetic expr script interpretation

I have created the following script to add 2 numbers together :
lazyadd2 script :
#!/bin/bash
#I have added the she-bang script above as I felt that this would be a good idea.
#The script takes in 2 arguments that are added together
num3=`expr $num1 + $num2`
echo $num3


I make the following variable assignments :

num1=1
num2=2


I type the following after script creation and relevant variable assignment :

$ sh lazyadd2

The following output is obtained :
+

I type the following to retrieve a value :
$ echo $num3

The result of this is blank (ie: the computer outputs no result).

What I expected to happen was that I should type the above to retrieve the sum of the values $num1 and $num2, so that "echo $num3" returns 3.

I know that this is *really simple* - so I'm sure the mistake is obvious....

Thanks for any help
# 2  
Old 09-19-2006
what you are doing is right
can you show us the script?
# 3  
Old 09-20-2006
Question (+ sign) Unexpected arithmetic expr script interpretation

In order to make things easier to follow (even thought I'm only dealing with simple scripts), the following are the scripts that are related to each other :

SCRIPT 1 :
#!/bin/bash
num3=`expr $1 + $2`
echo $num3


SCRIPT 2 :
#!/bin/bash
num3=`expr $num1 + $num2`
echo $num3


Here are the inputs and outputs for SCRIPT 1 :
$ sh lazyadd1 1 2
3
Here are the inputs and outputs for SCRIPT 2 :
$ num1=1
$ num2=2
$ sh lazyadd2
+
$ echo $num3

I have a problem with the second script - I think that it should not ouput a '+' at all! Also, when I echo for a num3 call, it should output the sum of the previous num assignments, namely '3'.

Anyone have any ideas as to why this is occurring?
Thanks.
# 4  
Old 09-20-2006
It's happening because when you run the lazyadd2 script, it is unaware of what you want the values of $num1 and $num2 to be. It is only going to know $1, $2, $3, etc. You can do this:

$ num1=1
$ num2=2
$ . lazyadd2

Or do it in the lazyadd2 script itself:
Code:
#!/bin/bash
num1=$1
num2=$2
num3=`expr $num1 + $num2`
echo $num3

...and run it the same way you run lazyadd1 (i.e., lazyadd2 1 2).
# 5  
Old 09-20-2006
Quote:
Originally Posted by Birdi6022
In order to make things easier to follow (even thought I'm only dealing with simple scripts), the following are the scripts that are related to each other :

SCRIPT 1 :
#!/bin/bash
num3=`expr $1 + $2`
echo $num3


SCRIPT 2 :
#!/bin/bash
num3=`expr $num1 + $num2`
echo $num3


Here are the inputs and outputs for SCRIPT 1 :
$ sh lazyadd1 1 2
3
Here are the inputs and outputs for SCRIPT 2 :
$ num1=1
$ num2=2
$ sh lazyadd2
+
$ echo $num3

I have a problem with the second script - I think that it should not ouput a '+' at all! Also, when I echo for a num3 call, it should output the sum of the previous num assignments, namely '3'.

Anyone have any ideas as to why this is occurring?
Thanks.

try this below
num1=1
num2=2
num3=`expr {$num1} + {$num2}`
echo $num3
# 6  
Old 09-20-2006
(+ sign) Unexpected arithmetic expr script interpretation

In response to Glenn Arndt's response, am I correct in stating that, if I declare a value of num1 and num2 outside of the script, I will find that these values are not automatically accessible from within the script?

I'm guessing that the answer to the last question is yes.
In regards to the script lines you advocate :
#!/bin/bash
num1=$1
num2=$2
num3=`expr $num1 + $num2`
echo $num3

I can see how that works - but am still interested in if the script can access variables written outside of it.

When typing the below into the script, could anyone tell me the significance of the 3rd line? I suppose that this is just a different way of evoking a shell script :

$ num1=1
$ num2=2
$ . lazyadd2


Finally, in regards to gkrishnag, I did try the below :
num1=1
num2=2
num3=`expr {$num1} + {$num2}`
echo $num3


But here's what I get :
$ num3=`expr {$num1} + {num2}`
expr: non-numeric argument
$ echo $num3


That is, what you suggested is not recognised as valid.
# 7  
Old 09-20-2006
An explanation of the dot is here: https://www.unix.com/aix/31018-pb-script-execution-variables.html

I believe gkrishnag meant ${num1} not {$num2}.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

2. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

3. Shell Programming and Scripting

Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this: cn=user.blr.ou=blr.india.o=company The line should be converted like this: cn=user.blr,ou=blr.india,o=comapny Was wondering how to do that using shell script. Please use tags where appropriate, thank you (4 Replies)
Discussion started by: saurabhkoar
4 Replies

4. Shell Programming and Scripting

SSH Script is sticking, & sign not doing what I would expect

I am having an issue where I am do an SSH to about 30 servers one at a time however my script is getting hung up sometimes on the SSH. I thought the & at the end as seen below would fire it and move on but that does not seem to be working. #!/bin/s for remsys in trumpetsnail angel delphin... (3 Replies)
Discussion started by: LRoberts
3 Replies

5. Shell Programming and Scripting

Interpretation of $variables inside programs run from a script

Hi, I am running a shell script that executes a program. Inside this program, variables are also referenced using a dollar symbol, eg. $a, $thething, and the shell script is misinterpreting them as variables relevant to the shell script rather than relevant to the program run from inside the... (2 Replies)
Discussion started by: TheBigH
2 Replies

6. Homework & Coursework Questions

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (8 Replies)
Discussion started by: Florinel76
8 Replies

7. Shell Programming and Scripting

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (1 Reply)
Discussion started by: Florinel76
1 Replies

8. Shell Programming and Scripting

help with expr command in script

Hi, I am trying to code a unix function to calculate date difference between two date variables. I am stuck at a point where I am trying to convert hours into minutes. Below is the code I am doing. function get_elapsed_time { export PROPS_FILE=temp.properties export... (8 Replies)
Discussion started by: Nutan
8 Replies

9. UNIX for Dummies Questions & Answers

problem in script involving month arithmetic

advance happy new year to all, i am having a script.The purpose of the scripts is as follows.If the current month is march,june,september or december ,inc_flg should be set to '1' otherwise inc_flg should be set to '2' month= date +"%m" if || || || ; then inc_flg = 1 else ... (6 Replies)
Discussion started by: rajarp
6 Replies

10. Shell Programming and Scripting

Sign on/Sign off logging script

I'd like to make a script that I can execute every time I sign on to my linux box that keeps track of the time and allows to me to add a remark to a file. So basically once I log in, I run the script, and it outputs the date and time to a text file (log.txt). But that isn't my problem. I need... (1 Reply)
Discussion started by: Glider
1 Replies
Login or Register to Ask a Question