Call functions from other scripts

i have a file that contains functions and i want the functions to be available in another script.

of course, the ideal situation here would be to put the functions in the same script, but i dont own these scripts. so I have to call the functions file from a different script.

how do i execute the functions file so its information becomes available to the other script?

functions.sh:

myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}

otherscript.sh:

results=$(/var/home/functions.sh ; myTest ; myCar)
MYPWD=$(echo "${results}" | egrep MYPWD | awk -F"=" '{print $2}')
MYLOGNAME=$(echo "${results}" | egrep MYLOGNAME | awk -F"=" '{print $2}')
MYDATE=$(echo "${results}" | egrep MYDATE | awk -F"=" '{print $2}')
MYUPTIME=$(echo "${results}" | egrep MYUPTIME | awk -F"=" '{print $2}')

Source the functions file.

In linux use the source command or more generally the lonely dot operator.

. /path/to/my/functions.shl
# linux
source /path/to/my/functions.shl

Oh:

PS: make sure the functions script does not call exit except on a fatal error. Since it runs in the process context of the calling script, exit or some signals initiated in the functions script will stop everything.

We have a db startup/error exit/exit functions script that does all manner of stuff. We source it and every one of hundreds of scripts run the exact same thing. Great for minimizing the source of problems. problems.

1 Like

Expanding a little bit on what Jim already said...

It is ALWAYS a good idea to tell us what shell and operating system you're using when you ask a questions like this! The way to do this varies from shell to shell. Assuming that you are using a shell that is based on Bourne shell or POSIX shell syntax, that the functions file only defines functions and/or variables that your script will want, that the functions file does not invoke any commands or functions that you don't want your script to execute, and that the function definitions are suitable for the shell that will be running your script; you bring those functions into your script by dot ting the function file before you reference any of the functions you want to invoke from that function file.

If you only want to reference those functions in the subshell environment that is set up to run the 1st command substitution in otherscript.sh , you could change the 1st line you showed us in that file from:

results=$(/var/home/functions.sh ; myTest ; myCar)

to:

results=$(. /var/home/functions.sh ; myTest ; myCar)

If you want to reference those functions in other places in otherscript.sh including inside and/or outside any other subshells it starts, dot your function file before you invoke any subshells:

. /var/home/functions.sh
results=$( myTest ; myCar)
MYPWD=$(echo "${results}" | egrep MYPWD | awk -F"=" '{print $2}')
MYLOGNAME=$(echo "${results}" | egrep MYLOGNAME | awk -F"=" '{print $2}')
MYDATE=$(echo "${results}" | egrep MYDATE | awk -F"=" '{print $2}')
MYUPTIME=$(echo "${results}" | egrep MYUPTIME | awk -F"=" '{print $2}')
1 Like

im hoping someone can help me understand this better.

so the problem here is, the client has complete control over the functions.sh script. meaning, i cant see the content of it.

so, while sourcing it with a:

. myfunctions.sh
or
source myfunctions.sh 

works in most ordinary situations, im curious if there are other options.

for instance, suppose the customer has the following in their functions.sh file:

#!/bin/sh
myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}
Selection=${1}
${Selection}

then, in my script otherscript.sh, I do something similar to this:

results=$(/home/james/functions.sh myTest ; /home/james/functions.sh myCar )

will this work?

of course, the problem i see right off the bat is that, yes, the above will run the function within the functions.sh file. but what if i needed it to run inside the otherscript.sh script and make the resources generated from it available to otherscript.sh?

I presume at some point, the export command will be used somewhere.