Operating System - HP-UX
1833346 Members
2911 Online
110051 Solutions
New Discussion

How to call a function (in a separate directory) from script ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

How to call a function (in a separate directory) from script ?

I have one file for each function in a seperate directory . How do I call functions from a script without putting the full functions in each of my script itself ?
Ex:
#pwd
/home/sam
#cat call_fun.sh
=============
export PATH=$PATH:/home/functions
echo HELLO
test
test2
=================
But this script only echoes HELLO but does
call the command in the function. Want an output of commands in file test and test2

# pwd
/home/functions
================
#ls
test test2
# cat test
###File name: test
----------------
test()
{
pwd
whoami
}
--------------------
# cat test2
###File name: test2
----------------
test2()
{
whodu
ls >out
}


Thanks a bunch
good judgement comes from experience and experience comes from bad judgement.
9 REPLIES 9
Hai Nguyen_1
Honored Contributor

Re: How to call a function (in a separate directory) from script ?

You can do it by "sourcing" your function files in your script.

For example,

. /fullpath/function1.sh

Hope this helps.

Hai
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to call a function (in a separate directory) from script ?

Hi Sam:

You're on the right track. Set the environmental variable FPATH in your hosting shell. Here's an example:

/tmp/fct.sh
#!/usr/bin/sh
FPATH=/tmp/fcts
testme
exit 0

the 'testme' function looks like this:

#!/usr/bin/sh
function testme
{
date
}

...and is named '/tmp/fcts/testme' with execute permissions.

Regards!

...JRF...
curt larson_1
Honored Contributor

Re: How to call a function (in a separate directory) from script ?

a function must be defined, or found in a directory named by the FPATH variable before it is referenced.

Additionally, you can put function difinitions in your environment file (.kshrc, etc) if you want them to be defined whenever the shell is invoked interatively.

if you want to improve your shell's performance, put functions that you use infrequently in a directory named by FPATH, instead of putting the definitions in your environment file and use set -o nolog to keep function definitons from being stored in the history file.

a function that becomes defined the first time it is referenced is called an auto-loaded function. Its primary advantage is better performance, since the shell does not have to read the function definiton if you never referenced the function.

you can specifiy that a function auto-load with the "autoload" preset alias. when the shell first encounters an auto-loaded function, it uses the FPATH varialbe to search for a filename whose name matches that of the function. this file must contain the definition of this function.

you can load a library of related functions with the first reference to any of its members, by putting them all in one file. For each function defined, you can us "ln" to create a filename with the name of the function that refers to this file.
Sammy_2
Super Advisor

Re: How to call a function (in a separate directory) from script ?

JRF,
And you are on the right path to 10 pts !!!.
Out of curiosity, what does FPATH does. I was trying
PATH=$PATH:/tmp/functs in the script
But it would not work.
huge Thanks.
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: How to call a function (in a separate directory) from script ?

Curt,
Thanks for the explanation for a question that I asked JRF. FPATH was little confusing to me. Until you explained it. Much thanks to you.
Sam
good judgement comes from experience and experience comes from bad judgement.
James R. Ferguson
Acclaimed Contributor

Re: How to call a function (in a separate directory) from script ?

Hi (again) Sam:

'FPATH' is the search path for function definitions (colon-delimited, as you would expect). Have a look at the 'sh-posix' man pages.

Regards!

...JRF...
Sammy_2
Super Advisor

Re: How to call a function (in a separate directory) from script ?

Another quick question.How come after the execution of testme and then echo "NEXT FUNCTION" the second function testme2 does not get executed. I have the testme2 function in /tmp/fcts (just like testme)


tmp/fct.sh
#!/usr/bin/sh
FPATH=/tmp/fcts
testme
echo "NEXT FUNTION"
testme2
exit 0
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: How to call a function (in a separate directory) from script ?

Its working. actually, I was using function name "test" (and not testme2)and test has special meaning and so it was given me probs.
Thanks all
good judgement comes from experience and experience comes from bad judgement.
curt larson_1
Honored Contributor

Re: How to call a function (in a separate directory) from script ?

is the filename that contains the testme2 function called testme2?