Simple addition, help.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple addition, help.
# 1  
Old 09-23-2008
Simple addition, help.

I add up the number of args that are not blank. It works, but the printout is a string that just keep concatenating on +1. So Ex. it goes through input of: bob toto " " tom ...I get 0+1+1+1, when all I want is 3. Any help is appreciated.

Code:
count=0

for name in $*
do

  if [ "$name" != "" ]; then
  count="$count+1"
  fi

done

echo $count

# 2  
Old 09-23-2008
Some possibilities:

Code:
let count=count+1

let "count+=1"

let "count++"

Regards
# 3  
Old 09-23-2008
Might be better to test for a zero length string instead of comparing it to a blank one...
Code:
let count=0

for i in $*
do
   if [ ! -z "$name" ]; then
      let count+=1
   fi
done

echo $count

# 4  
Old 09-23-2008
If you're in ksh or bash, you can add stuff using $(( )) operator:

i=1
echo $(( $i + 1 ))

Nice thing with this construction is that you can nest the math:

a=1
b=10
echo $(( $a + $(( $b + 1 )) ))

/fimblo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Addition of new line

Hi I have a file whose contents are as follows: sorce1 LEN assumption 695 3570 0.770047 - . ID=f000001.1;source_id=A.off_LEN_10008424; sorce1 LEN descriptive 3334 3570 . - 0 Parent=f000001.1; sorce1 LEN ... (8 Replies)
Discussion started by: sa@@
8 Replies

2. Shell Programming and Scripting

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 22 subcount=`echo $dir/Q$qid.txt` + `echo $dir/Q$qid.txt` 23 echo... (1 Reply)
Discussion started by: lookinginfo
1 Replies

3. Shell Programming and Scripting

addition of decimal no

a=10.00 pattern=-11.00 b=`echo "$a $pattern" | awk ' printf("%d\n", $1 + $2)'` echo $b not working, also trined bc ,dc but thats not on my m/c. also expr not supporting. any clue? (6 Replies)
Discussion started by: saluja.deepak
6 Replies

4. Shell Programming and Scripting

Need help on addition in shell

i need shell script to add to numbers #!/usr/bin/sh a=1310601600 ------> epcho time of Thu, 14 Jul 2011 00:00:00 UTC b=864000 -------> 10 days in sec c=`expr $a+$b` echo $c----1311465600> this output i will use this value to delete the data from MySQL db next i need to set... (3 Replies)
Discussion started by: sreedhargouda
3 Replies

5. Shell Programming and Scripting

Substitution/Addition

Hi I need to do the following substitution I have to look for line starting with ABC and add 4 ":" before the first occurence of "+"in that line Input ABC:12:Lambert:C278472:1357:0:0:0:0:2:N::::N:9045123:NAP::+DEF output ABC:12:Lambert:C278472:1357.00:0.00:0:0:0:2:N::::N:9045123:NAP::::::+DEF... (5 Replies)
Discussion started by: mad_man12
5 Replies

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

addition in sh shell

I have to create un counter and I am unable to do an additition: #!/bin/sh count=$1 while ] do echo $count $count=$count+$1 done (1 Reply)
Discussion started by: cfg
1 Replies

9. Shell Programming and Scripting

addition

Hi all, I am new to perl. I need help adding bunch of numbers. I have a file look like this: 1 1 2 1 2 3 1 2 3 4 1 (2 Replies)
Discussion started by: email-lalit
2 Replies

10. Shell Programming and Scripting

Addition problem

Hello Seniors!!! I am trying to add to lines on a file which is delimited by character "|". input.txt Desired Output file should give simple addition. output.txt Can some one provide me simple awk solution to this. M struggling to solve this using awk & for loop inside awk. Thanks... (2 Replies)
Discussion started by: onlyroshni
2 Replies
Login or Register to Ask a Question