The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
addition email-lalit Shell Programming and Scripting 2 08-06-2008 06:58 PM
Addition problem onlyroshni Shell Programming and Scripting 2 12-10-2007 02:11 PM
Unix addition ( Row wise) gauravgoel Shell Programming and Scripting 3 05-17-2007 05:27 AM
floating point addition ravi raj kumar Shell Programming and Scripting 8 12-22-2006 02:47 AM
Addition of numbers in unix asinha63 Shell Programming and Scripting 3 03-14-2006 12:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-23-2008
Bandit390 Bandit390 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 19
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 (permalink)  
Old 09-23-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,346
Some possibilities:


Code:
let count=count+1

let "count+=1"

let "count++"

Regards
  #3 (permalink)  
Old 09-23-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
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 (permalink)  
Old 09-23-2008
fimblo fimblo is offline
Registered User
  
 

Join Date: Feb 2008
Location: stockholm sweden
Posts: 31
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0