passing variable to function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers passing variable to function
# 1  
Old 04-03-2007
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  
Old 04-03-2007
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  
Old 04-03-2007
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  
Old 04-04-2007
Hammer & Screwdriver

Yeah thanks reborg. I was thinking that there was something that I was doing wrong. The positional parameter referencing thinglymijig. Thanks again.
# 5  
Old 04-05-2007
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

passing argument from one function to another

Hi all, In the given script code . I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob(). Any help? Thanks #!/bin/bash ... (55 Replies)
Discussion started by: nrjrasaxena
55 Replies

4. Shell Programming and Scripting

passing variable content to a function

following on from below link https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 i will be using file reading in while loop say for example while read line123 do echo "line read is $line123" insert_funct $line123 done< mysqldump.sql... (6 Replies)
Discussion started by: vivek d r
6 Replies

5. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

6. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

7. UNIX for Advanced & Expert Users

Passing a unix variable value to a Plsql function

Suppose I have a unix variable called RGNM which is holding a value. Now I want to call a plsql function in my script. THis plsql function takes one IN parameter. I want to pass my UNIX VARIABLE Value to the plsql function. Can i just give it by giving $RGNM in the function after calling sqlplus... (1 Reply)
Discussion started by: cobroraj
1 Replies

8. Shell Programming and Scripting

Passing string from function with '*'

Hi I have a shell function which returns string(ksh). The string is an sql statement. This statement can have '*' in its content (i.e. select 100 / 2 *100 from dual). When this happens ret_str will have contents of current directry I run the script from build in sql. Is there any way to fix it... (2 Replies)
Discussion started by: zam
2 Replies

9. UNIX for Advanced & Expert Users

Parameter passing in a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (2 Replies)
Discussion started by: fastgoon
2 Replies

10. Shell Programming and Scripting

Passing a variable name to be created within a function

Is it possible to pass a variable name, as a parameter to a function, so it can be created within this function ? Something like this: func_uppercase abcdefgh var_name where the 1st parameter is the string I want to convert and the 2nd is the desired variable name... $2=`echo "$1" |... (2 Replies)
Discussion started by: 435 Gavea
2 Replies
Login or Register to Ask a Question