addition of two numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting addition of two numbers
# 1  
Old 10-05-2011
Question addition of two numbers

by the script, two files Q1 and Q2 will be generated on the system. Q1 will contain an integer number and Q2 also contain an integer number. i would like to add those numbers and put into new file.

excerpt from my script
Code:
22 subcount=`echo $dir/Q$qid.txt` + `echo $dir/Q$qid.txt`
23 echo $subcount > $dir/s1.txt

by running the full script i am getting the following error
Code:
./test.sh[22]: +: not found [No such file or directory]

thanks in advance

Last edited by Franklin52; 10-06-2011 at 03:19 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-05-2011
1) Shell arithmetic doesn't work that way. You can't just 'A=B+C'.
2) echo doesn't work that way. You're printing filenames.

So you're ending up with a filename like "/path/to/file1 + /path/to/file2".

How about:
Code:
awk '{ T+=$1 }END{print T}' /path/to/file1 /path/to/file2 > file3

---------- Post updated at 02:38 PM ---------- Previous update was at 02:37 PM ----------

If you need to do it in pure shell, you get:

Code:
read N1 < /path/to/file1
read N2 < /path/to/file2

N3=`expr $N1 + $N2` # any bourne shell can do this

N3=$((N2+N1)) # More efficient KSH/BASH version

echo $N3 > file3

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 figuring division and addition based on column data and line numbers

I have a data file in the format of 1234 xxx 1234 xxx 1234 xxx 1234 xxxI want to be able to calculate the following - COLUMN1+((LINENUMBER-1)/365) The output needs to preserve the 2nd column - 1234 xxx 1234.00274 xxx 1234.00548 xxx What is the best way to do this? I am somewhat... (9 Replies)
Discussion started by: ncwxpanther
9 Replies

2. Shell Programming and Scripting

addition of both positive and negative numbers

Let, I have three numbers +00123.25 -00256.54 +00489.23 I need to sum up all those three numbers, after storing them in three variables (say var1, var2, var3). I used both expr and BC, but they didn't work for me. But, I am not able to sum up them, as I don't have any idea how to... (13 Replies)
Discussion started by: mady135
13 Replies

3. Shell Programming and Scripting

Addition

Hi all, I am very new to shell programming and trying to learn out the basics. I tried this: $ echo `expr 20 + 30` and it worked. But when i tried this,it does not work. $ a=20 $ b=30 $ echo `expr a + b` The error is: expr: non-numeric argument I cant understand why its... (3 Replies)
Discussion started by: gautamshaw
3 Replies

4. Shell Programming and Scripting

Help with addition of 2 numbers that are read from a file

I am trying to add free and used memory (so that i can compute percentage used)of remote nodes using shell script. I use the openssh-server,expect tool and ssh script. 1)login.txt (info of nodes): ip1|username|password ip2|username|password . . . 3)sshlogin.sh #!/bin/bash ... (1 Reply)
Discussion started by: marmik1903
1 Replies

5. Shell Programming and Scripting

Addition of floating numbers

Hi, i want to add two decimal values to $ set a= 12.4 $ set b=3.6 $ w=`expr $a - $b` expr: non-numeric argument or is there any other method to do this mathematics operation. i need to use this into my script. (4 Replies)
Discussion started by: dear_abhi2007
4 Replies

6. Shell Programming and Scripting

Help with addition

Hi all, I am getting following output by using commands like sort, uniq and awk to the standard output. 110 d 40 a 59 c 9 b 3 e Now at the end I would like to add all the numbers in column 1 and display the count of all numbers i.e. (110 + 40 + 59 + 9 + 3). Also the output may... (3 Replies)
Discussion started by: tenderfoot
3 Replies

7. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

8. Shell Programming and Scripting

Addition of all the numbers in a single file

Hi All , Want to know the "sum" of all the digits in below file called "/sample" .Could some one please let me know the script either command . cat /sample 12 34 23 23 Best Regards, Chinni . (5 Replies)
Discussion started by: chinni-script
5 Replies

9. Solaris

How to perform addition of two numbers in shell scripting in Solaris-10

Hi, I have a sh script which contains the following line TOTAL=$((e4-s4)) -> Where e4 and s4 are input got from the user. At the time of execution of this line the following error occurs test.sh: syntax error at line 8: `TOTAL=$' unexpected How to solve this issue?. Can any... (9 Replies)
Discussion started by: krevathi1912
9 Replies

10. Shell Programming and Scripting

Addition of numbers in unix

Hi I have a file with specified format . Hxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr xxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr xxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr xxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr... (3 Replies)
Discussion started by: asinha63
3 Replies
Login or Register to Ask a Question