How to pass variables to FUNCTION ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass variables to FUNCTION ?
# 1  
Old 05-31-2007
How to pass variables to FUNCTION ?

Hi...

Actually, I want to pass a few variables to the function and print it. But, its looks like not working. Could some body help me how could i do that ?... below is my program...


PHP Code:
#!/usr/bin/ksh

usage()
{
    echo 
"Usage: $0 -n -a -s -w -d"
    
exit
}

rename()
{
    echo 
"rename $1 $2" 
}

if [ $
# -lt 1 ]; then
    
usage
fi

while getopts r:i:o opt
do
    case 
"$optin
        r
rename=1;;
        
iinput="$OPTARG";;
        
ooutput="$OPTARG";;
        \?) 
usage;;
    
esac
done

if [ $rename ]; then
    rename $input $output
fi 
# 2  
Old 05-31-2007
Try this...

This is what you want

myfunc()
{
echo "read var1 "$1
echo "read var1 "$2
}

myfunc 1 2
# 3  
Old 05-31-2007
As example:
lets say i run it as
#./script.sh -r -i input -o output

Then, the programm will passed "input" and "ouput" to rename function and print it as below:-
# rename input output

Now its blank and not print anything...

Anyway I have tried your solution but its same... and result has been print out..
# 4  
Old 05-31-2007
hmm... i have got it...

....Something wrong with the getopt declaration.....
# 5  
Old 05-31-2007
Your color coding is excellent.
# 6  
Old 05-31-2007
The colon must follows the option
Code:
while getopts ri:o: opt

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

2. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

3. Shell Programming and Scripting

How to pass variables between scripts?

Hello, I have two bash scripts like the following: script 1: #!/bin/bash var=WORLD bash path/to/second/script/script2.bash script 2: #!/bin/bash echo "HELLO $var" I expected the output to be "HELLO WORLD" but instead, I get "HELLO". I understand that when I envoke another bash... (2 Replies)
Discussion started by: jl487
2 Replies

4. Shell Programming and Scripting

Paramerter pass for function(sub routine) need help

Hi, Please help me here while passing the paramert to fuction i am facing problem. i tryied passing 7 PARAMeter in side single quote,double quate even tried tild sign not working. how can assign it properly . usage () { typeset -i NumPARAMs=$1 typeset -i PARAM1=$2 typeset PARAM2=$3... (3 Replies)
Discussion started by: nitindreamz
3 Replies

5. Shell Programming and Scripting

pass function as argument to a function

I have the following code : function1 () { print "January" } function2() { case $1 in January) print "Dzisiaj mamy styczen" ;; *) ;; } main() { (1 Reply)
Discussion started by: presul
1 Replies

6. Shell Programming and Scripting

Pass parameters to function

Hi, for example I have this function: function get_param () { test=echo "some string" test2=echo "someother string" } I want to call this function and get test or test2 result, how do I do that ? Thank you (2 Replies)
Discussion started by: ktm
2 Replies

7. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies

8. Shell Programming and Scripting

pass variables from one script to another

HI all I am calling a script "b" from script "a". In script "a", i connect to database and get month and year. I have to pass these same values to script b. How can i do that. How can i pass parameters from one script to another (3 Replies)
Discussion started by: vasuarjula
3 Replies

9. Shell Programming and Scripting

Can we pass array with call by value in function

I want to pass an array in my function, And my function will be changing the elements of the array in the fuction, but it should not affect the values in my array variable of main function (1 Reply)
Discussion started by: ranjithpr
1 Replies

10. UNIX for Dummies Questions & Answers

Pass argument to function

Hi, Can someone please explain to me how I can get a function to recognize a file given as an argument to a script. Suppose the script has the argument as follows: sh script file and the function is as follows: function display_file () { cat $1 } and it s then called #main program... (1 Reply)
Discussion started by: Knotty
1 Replies
Login or Register to Ask a Question