Call functions from other scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call functions from other scripts
# 1  
Old 11-01-2016
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:
Code:
myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}

otherscript.sh:
Code:
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}')

# 2  
Old 11-01-2016
Source the functions file.

In linux use the source command or more generally the lonely dot operator.
Code:
. /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.

Last edited by jim mcnamara; 11-01-2016 at 11:14 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 11-01-2016
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 dotting 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:
Code:
results=$(/var/home/functions.sh ; myTest ; myCar)

to:
Code:
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:
Code:
. /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}')

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-02-2016
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:

Code:
. 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:
Code:
#!/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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Modify call to functions depending on file name

Hi, I am creating a script that does gunzip/unzip/untar depending on the file extension. I have created 3 functions in my script and call them in following order. 1) gunzip function 2) unzip function 3) untar function I am using FILENAME=${FILENAME%.*} to modify filename between... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

How to call different scripts?

I have 3 different scripts with 3 different functions, I would like to merge these 3 and make a master script. The following are my ideas: - ask user to pick which script he needs - after choosing, point it to the right script or... depending on the inputs provided by the user, all three... (1 Reply)
Discussion started by: priyanka.premra
1 Replies

3. Programming

can a linux kernel module call libc functions?

can a linux kernel module call libc functions, such as printf(), strcpy(), etc...? (9 Replies)
Discussion started by: vistastar
9 Replies

4. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

5. Shell Programming and Scripting

Scripts in ~/bin vs. functions in ~/.bashrc

Hi there, Anyone knows what would be the cons and pros of adding a script in ~/bin vs. a function in ~/.bashrc? I'm not sure how the system keeps tracks of some of the settings loaded in ~/.bashrc (like functions and aliases). Would I be right in thinking that this would all be loaded into... (2 Replies)
Discussion started by: victorbrca
2 Replies

6. Shell Programming and Scripting

Bash: Nested functions and including other scripts

Hello. I have the following problem with bash code: function fl1_load_modules_and_get_list() ........... for module in $FL_MODULES_TO_PROCESS do source "${FL_MODULE_DIR}/${module}/module.sh" done ........... } function fl1_handle_install { local... (12 Replies)
Discussion started by: FractalizeR
12 Replies

7. Shell Programming and Scripting

How to call C functions in shell scripts?

How can i call dynamic C functions in shell scripts? (5 Replies)
Discussion started by: agarwal
5 Replies

8. Shell Programming and Scripting

Calling functions in scripts directly

Hi, I have a menu driven script that does various tasks, I want to be able to call functions directly from within other unix scripts from my menu script. Is this possible? (12 Replies)
Discussion started by: LiquidChild
12 Replies

9. Programming

call functions from diferents programs

hi i have ten program in C, and there are functions what are in all the programs. so, i want to make a directory to store all the functions what are in all the programs, and call them from the C programs. (sending variables and values) is that possible?¿? how ca i do that?¿? any idea,... (1 Reply)
Discussion started by: DebianJ
1 Replies
Login or Register to Ask a Question