How to pass arguments to a function in a shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass arguments to a function in a shell script?
# 1  
Old 02-10-2004
How to pass arguments to a function in a shell script?

Hi,
I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments.
The function written is
f1()
{......
........
}

What should be the syntax to pass arguments $t1 and $t2 to this function.
Help would be appreciated!
# 2  
Old 02-10-2004
.....

Last edited by druuna; 05-21-2009 at 10:16 AM..
druuna
# 3  
Old 02-10-2004
in bash the variables are global, so a variable established somewhere else can be used an a separate function. i am not sure about variables assigned in another function however. check out tldp.org and search for the "advanced bash shell scripting guide", if youre using bash.
# 4  
Old 03-01-2004
Bug

Hi Preeti

You can pass variables to functions and use them within for modification. Once you exit the function, the variables still continue to hold the same values. So you need not worry abt the changes as long as your vaiable names within the function and outside the function are same.

Else you can do as follows :-

example_func()
{
echo "Var1 is $1"
echo "Var2 is $2"
Var1=`echo $Var1 + 10`
Var2=`echo $Var2 + 15`
echo "Var1 is $Var1"
echo "Var2 is $Var2"
}

-
-
example_func $Var1 $Var2
-
-
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 call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

2. Shell Programming and Scripting

Function in one-linef and pass arguments in a pipe

I need to declare a function, this function will contain a script, this script cannot be in a file but must be piped. and then, for the script to run, i need to pass arguments to it. everything has to be on one line. so i'm basically looking for a one-liner here's what i'm doing: myfunc ()... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Need to pass arguments while running a Shell Script

Shell Script Gurus, I am writing a shell script which needs User ID's to pass as an Arguments in command line while executing. Can this be doable? I've never done this, If you give a sample script that would be helpful, Thanks. (1 Reply)
Discussion started by: shekar777
1 Replies

4. Shell Programming and Scripting

Pass Arguments to Command from Shell Script

Hi all, I am working on a project, in which I have to connect to Bluetooth low energy device. I am able to connect and do data transfer from command line. But I want to do from script Here is my script #!/bin/bash #sudo hcitool -i hci0 lescan sleep 1 sudo hcitool -i hci0 lecc --random... (8 Replies)
Discussion started by: nithin@embdes
8 Replies

5. Shell Programming and Scripting

How to pass arguments from a textbox in CGI to a shell script?

Hello All, Could you please help me to explain that how do I pass textboxes values while running a shell script which need some arguments while running it and should be invoked after the button click. Here is an example. The first CGI is: #!/bin/ksh echo "Content-Type: text/html"... (4 Replies)
Discussion started by: RavinderSingh13
4 Replies

6. Shell Programming and Scripting

How to pass an array to a function in shell script.?

hi, I have a array say SAP_ARRAY="s1.txt" SAP_ARRAY="s2.txt" how can i pass this full array to a function. here is the sample code i am using.. CHECK_NO_FILES() { FARRAY=$1 echo "FARRAY = $FARRAY" echo "FARRAY = $FARRAY" ............... (5 Replies)
Discussion started by: Little
5 Replies

7. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

8. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

9. Shell Programming and Scripting

no of arguments to function in shell script

Hi, I have a function in shell script fun1{ echo "No.of arguments are..."} this function will be called in same script by passing arguments fun 1 2 3 I want to check the no. of arguments passed to fun1 function in the same functionbefore validation. can any one suggest me. (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

10. Shell Programming and Scripting

Need help to pass arguments to shell script

Hi, I have a shell script called ftp.sh which is running continously in background. I tried passing arguments to this script but it did not worked out. Below is ftp.sh script. Please help me case $param in start) sleep_func "300" echo "!ksh $scr_ddir/ftp.sh... (1 Reply)
Discussion started by: bhargav20
1 Replies
Login or Register to Ask a Question