Pass parameters to function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass parameters to function
# 1  
Old 03-27-2010
Pass parameters to function

Hi, for example I have this function:

Code:
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  
Old 03-27-2010
you call the function and pass arguments to it by
Code:
function get_param ()
{
arg1="$1"
arg2="$2"
test="`date`"
test2="someother string"
#export test test2
}

echo "before function get_param test=$test"
echo "before function get_param test2=$test2"
echo "before function get_param arg1=$arg1"
echo "before function get_param arg2=$arg2"
get_param arg1 arg2
echo "after function get_param test=$test"
echo "after function get_param test2=$test2"
echo "after function get_param arg1=$arg1"
echo "after function get_param arg2=$arg2"

output

before function get_param test=
before function get_param test2=
before function get_param arg1=
before function get_param arg2=
after function get_param test=Sat Mar 27 15:23:00 EDT 2010
after function get_param test2=someother string
after function get_param arg1=arg1
after function get_param arg2=arg2
# 3  
Old 03-27-2010
Code:
get_param()
{
   case $1 in
     test) echo "some string" ;;
     test2) echo "some other string" ;;
   esac
}

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 subject,mailbody and filename as parameters to function?

Hi Experts, how to pass subject,mailbody and filename as parameters to function. mode=$1 file=/db/files/uniq.txt mail_send() { export MAILPART=$(uuidgen) export MAILPART_BODY=$(uuidgen) { echo "TO:nalu.d@hes.com" echo "Subject:$subject" echo "MIME-Version: 1.0" ... (0 Replies)
Discussion started by: nalu
0 Replies

2. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

3. Shell Programming and Scripting

Pass Parameters to awk command

I need to pass values at runtime for the below awk command where l is the length and partial.txt is the file name. awk -v l=285 '{s="%-"l"s\n";printf(s,$0);}' partial.txt > temp1.txt; (5 Replies)
Discussion started by: Amrutha24
5 Replies

4. Emergency UNIX and Linux Support

Pass two parameters

Hi I have a batch file aaa.exe which needs two input parameters: Usually the command's format likes aaa 555 10000 But I want to use parameters to do it. aaa $1 $2 These two parameters come from a text file list.txt 41800497 41801375 41814783 41816135 41814930 41816135 41819987 41820843... (4 Replies)
Discussion started by: zhshqzyc
4 Replies

5. Shell Programming and Scripting

pass shell parameters to awk does not work

Why does this work for myfile in `find . -name "R*VER" -mtime +1` do SHELLVAR=`grep ^err $myfile || echo "No error"` ECHO $SHELLVAR done and outputs No error err ->BIST Login Fail 3922 err No error err ->IR Remote Key 1 3310 err But... (2 Replies)
Discussion started by: alan
2 Replies

6. Shell Programming and Scripting

Can't get shell parameters to pass properly to sqlplus

Gurus, The issue I'm having is that my Shell won't accept SQL parameters properly...... Here's they way I'm running it.... applmgr@ga006hds => sh CW_MigrationDeployScript.sh apps <appspwd> <SID> '01-JAN' '31-MAR' The process just hangs not submitting the SQL job... ... (3 Replies)
Discussion started by: WhoDatWhoDer
3 Replies

7. Shell Programming and Scripting

pass parameters from perl to csh scripts

I use csh a lot but I don't really write csh scripts. Now I have a need to implement a security check (written in perl; verify an user input security code) into a csh script. Here is the senario: #csh 1. call the perl script 2. if the perl script returns 'true', pass on; if the perl... (1 Reply)
Discussion started by: Julian16
1 Replies

8. Shell Programming and Scripting

How to pass parameters transparently into a sub script

Hi, I am trying to write a script like this: #!/bin/ksh #script name: msgflow #The awk commands for Solaris and Linux are incompatible if ] then msgflow-solaris $* elif ] then msgflow-linux $* fi This script is shared by a file system which is visible to both... (3 Replies)
Discussion started by: danielnpu
3 Replies

9. Shell Programming and Scripting

How to pass parameters to an awk file?

I have an awk file where I need to pass a filename and a value as a parameter from a sh script. I need to know how to pass those values in the sh script and how to use the same in the awk file. Thanks in advance!!! Geetha (3 Replies)
Discussion started by: iamgeethuj
3 Replies

10. UNIX for Dummies Questions & Answers

How to pass two or more parameters to the main in shell script

Hey Guys from the below script what I understood is we are sending the the first parameter as input to the main (){} file main > $LOGFILE 2>&1 but can we send two or three parameter as input to this main file as main > $LOGFILE 2>&1 2>&2 like this Can any one plz help I need to writ a... (0 Replies)
Discussion started by: pinky
0 Replies
Login or Register to Ask a Question