call function in one script from another script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting call function in one script from another script
# 1  
Old 02-19-2008
call function in one script from another script

Hello experts,
Is it possible to call function in one script from another script?

Example.

Script 1:
#!/bin/bash
function1(){
}

Script 2:
#!/bin/bash
#code to call function1 in Script 1

Smilie
thanks you
# 2  
Old 02-19-2008
Yes. Assuming that script1 just defines the function and does not do stuff you do not want done. You source script1 inside script2 then call the function that lives in script1.
example script2:
Code:
#!/bin/bash
source script1  # alternate syntax: . script1 < note the free floating dot
function1 argument1 argument2

# 3  
Old 02-19-2008
SmilieSmilieSmilieSmilieSmilieSmilie

Thank you great help~
# 4  
Old 10-23-2008
Hi All,

I have written something like this to call a function from another script.

Script-1 : as.sh

#!/bin/sh
file_archive()
{
mv /home/babu/amit/employee /home/babu/amit/error_employee
return 0
}

script 2: a.sh

#!/bin/sh

a=2
b=3

if [ $a -ne $b ]
then
echo " Value $a and value $b are not same" >> LOG
. as.sh
file_archive()
fi

The echo message will be appended in LOG file... but it is not renaming file from employee to error_employee.



But
# 5  
Old 10-23-2008
You don't need the ():

Code:
if [ $a -ne $b ]
then
echo " Value $a and value $b are not same" >> LOG
. as.sh
file_archive
fi

# 6  
Old 10-23-2008
Thank you vimes, Now it is working.

One more thing i like to ask you...

If the way we are calling function from other script can we call variable in same way?

i mean if say my script is like...

#!/bin/sh

a=2
b=3

if [ $a -ne $b ]
then
echo " Value $a and value $b are not same" >> LOG
. as.sh
file_archive
fi

instead of defining a & b variable i want to declare it in some other script & need to call.

a=2
b=3

Why i am asking is that i have make an script. i am using different kinds of variable. While opening that script it looks wierd. So i want to create a seperate file which contains all functions & variables. & then in my main script i need to call it.
# 7  
Old 10-23-2008
Yes, that's possible. It works the same way.

You can just create vars.sh, for example.
Code:
#!/bin/sh
a=2
b=3

And then in your main code do:

Code:
#!/bin/sh

# Load vars
. vars.sh

if [ $a -ne $b ]
then
echo " Value $a and value $b are not same" >> LOG
. as.sh
file_archive
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call function as different user within the script

Hello For HP-UX, ksh shell, is it possible to define functions and call them by another user ? For example <function_name> ( ) { command1 command2 } su - <user> -c <function_name> Or the only option is defining the user in the function itself as follows - <function_name> ( )... (2 Replies)
Discussion started by: atanubanerji
2 Replies

2. Shell Programming and Scripting

Call function from another script

Hey, i got this 2 file. When i try to pick option 1, which is test1, it says ./test: test1: not found. Any idea on how i can fix it? #!/bin/sh QUIT=0 `dirname $0`/testfile while ; do testmenu read option case $option in 1) test1 ;; 2) test2 ;; 3) echo... (2 Replies)
Discussion started by: Nick1097
2 Replies

3. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

4. Shell Programming and Scripting

Script – Function Call

Hello, I have Individual function in my shell script , Function1 { Master activities } Function2 { Sub activities 1 } Function3 { Sub activities 2 } … (2 Replies)
Discussion started by: Shanks
2 Replies

5. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

6. Shell Programming and Scripting

Shell Script to call another function

Here is the following code : 1. # gcc -c test firstprog.c the above command will generate a executable file called "test " in which ever directory it is run. Assuming It will also return a value. 2. In the below SCRIPT . test is a file generated by compiling a c program... (3 Replies)
Discussion started by: Vabiosis
3 Replies

7. Shell Programming and Scripting

error when call function in bash script

Dear all, Could you please advice as I when call function i found the following error " refills: command not found" note that refills is function name. following also the function and how i call it function refills { echo "formatting refills and telepin" >> $log awk -F,... (20 Replies)
Discussion started by: ahmed.gad
20 Replies

8. Shell Programming and Scripting

how can i call a function in shell script

i have a function written in one shell script and i want to call that function in another shell script and use the value returned by that script. can any one suggest me how can i do that? regards, Rajesh.P (4 Replies)
Discussion started by: rajesh.P
4 Replies

9. Shell Programming and Scripting

i want to call a oracle function in my shell script

i want to call a oracle function in my shell script (4 Replies)
Discussion started by: dineshr85
4 Replies
Login or Register to Ask a Question