Passing Input To Function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Passing Input To Function
# 1  
Old 05-23-2013
Passing Input To Function

May i please know why is it printing the script name for $0 when i pass those parameters to function.

Code:
#!/bin/bash -x
usage()
{
echo "In Usage Function"
echo $0
echo $1
echo $2
}

echo "printing first time"
echo $0
echo $1
echo $2

usage $0 $1 $2

Output:
Code:
test.sh first second

printing first time
/home/infrmtca/bin/test.sh
first
second

In Usage Function
/home/infrmtca/bin/test.sh
/home/infrmtca/bin/test.sh
first

# 2  
Old 05-23-2013
There are two ways to define a function. The subtle differences between them should be described in your shell's man page.

You need to use the other type to achieve the expected behaviour:

Code:
function Usage {
...
}

# 3  
Old 05-23-2013
Because parameters inside and outside a function go $1 $2 $3, not $0 $1 $2. $0 is a special variable which is the script name as given to exec (usually).

Last edited by Corona688; 05-23-2013 at 12:53 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Oracle function as file input to sqlplus

Apologies if this is the incorrect forum.There is an issue in the function call I am facing while calling the same from a unix shell scripts. Basically, I want the ref cursor to return values to a variable in sqlpus. The function call is currently saved in a ".txt" file in a unix location. I want... (7 Replies)
Discussion started by: amvip
7 Replies

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

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

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

5. Shell Programming and Scripting

Passing two variables to function

HI ,I am a new in Bash and ,I dont know how to pass a second parameter to this fuction,if the name of the passed argument is num works fine,but if I try to pass secondNum,dont recognized it thanks function check() { if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try... (6 Replies)
Discussion started by: lio123
6 Replies

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

7. Shell Programming and Scripting

Passing more than one argument in a function

Hi All, Calling a function with one argument and storing the return value in a shell script is as below:( so far I know) value="`fun_1 "argument1"`" Its working perfectly for me. Can u help me with passing more than one argument and storing the return value Thnaks in advance JS (1 Reply)
Discussion started by: jisha
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 Dummies Questions & Answers

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... (4 Replies)
Discussion started by: Knotty
4 Replies

10. 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
Login or Register to Ask a Question