The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 12:39 PM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM
Passing a unix variable value to a Plsql function cobroraj UNIX for Advanced & Expert Users 1 10-30-2007 06:59 AM
Passing a variable name to be created within a function 435 Gavea Shell Programming and Scripting 2 02-04-2004 03:20 PM
Passing Argument to Function AkumaTay UNIX for Dummies Questions & Answers 2 10-18-2001 07:24 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 04-03-2007
Knotty Knotty is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 11
passing variable to function

Hi,

I am trying to sum up numbered columns and in order to tidy up the program I have wrote a function to do the adding of some numbers. I have a problem though with passing a variable to the function in the UNIX bash shell. The function only gives the first number in the variable list and does not add the rest. Can a variable not be passed to the function or am I missing something quite obvious here. The function that I wrote and the function call is shown below;

function add_num () {
sum=0
for NUM in $1
do
sum=`expr $sum + $NUM`
done
}

I call this function by function_name Variable_name as so:
add_num $column1
add_num $column2

Very puzzled,
Knotty.
  #2 (permalink)  
Old 04-03-2007
tumblez tumblez is offline
Registered User
  
 

Join Date: Nov 2002
Posts: 15
This works...

add_num () {
for NUM in $1
do
sum=`expr $sum + $NUM`
echo sum $sum
done
}


sum=0
add_num 1
add_num 2

and gives this result...

sum 1
sum 3
  #3 (permalink)  
Old 04-03-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,246
please use code tags for code

$1 means the first argument.


Code:
function add_num () {
sum=0
for NUM in $@
do
sum=`expr $sum + $NUM`
done
}

  #4 (permalink)  
Old 04-04-2007
Knotty Knotty is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 11
Wink

Yeah thanks reborg. I was thinking that there was something that I was doing wrong. The positional parameter referencing thinglymijig. Thanks again.
  #5 (permalink)  
Old 04-05-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by Knotty
I am trying to sum up numbered columns and in order to tidy up the program I have wrote a function to do the adding of some numbers. I have a problem though with passing a variable to the function in the UNIX bash shell. The function only gives the first number in the variable list and does not add the rest. Can a variable not be passed to the function or am I missing something quite obvious here. The function that I wrote and the function call is shown below;

function add_num () {
sum=0
for NUM in $1
do
sum=`expr $sum + $NUM`
done
}

I call this function by function_name Variable_name as so:
add_num $column1
add_num $column2
If you want a running total that is increased with every call to the function, don't reset sum to 0:


Code:
add_num()
{
  sum=${sum:-0} ## set to 0 only if sum is empty
  for num
  do
    sum=$(( $sum + $num ))
  done 
}

That assumes that the contents of your variable is something like:
column1="12 34 56 78 90"

If you want to add columns in a file, use awk:

Code:
awk '{
   column1 += $1
   column2 += $2
}
END {
  print "Column1: " column1
  print "Column2: " column2
}' FILE

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 06:01 AM.


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