Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function file

Status
Not open for further replies.

medir01

Programmer
Aug 8, 2002
7
0
0
US
Is it possible to create a file of function(s) and then refer to that file from within another script file to run those function(s)? I am trying to re-use function code within multiple scripts.
 
Sure:
.myfuncfile
or
source myfuncfile.
 
Thank you. The .myfuncfile works as long as I don't declare the code as a function. The 'source myfile' sends an error of 'source: not found'.
I basically want to have a file with multiple functions decalared where I can any function from a seperate script file. Such as this:

myfunctionfile:
func_1()
{
echo "Hello world!"
}

func_2()
{
echo "Hello world again!"
}

myscriptfile:
#! /bin/ksh

./rm_func
func_1

echo "Exiting"

The script file errors out as 'func_1: not found'. Is there a way to refer the the functions within my function file?
 
Try :
#! /bin/ksh
#
param1="First"
. rm_func param1 #call (using . space)
#function_script with a parameter

echo "Exiting"

and that function script :-
#!/bin/ksh
#All your functions in here
func_1()
{
echo "Hello world!"
}

func_2()
{
echo "Hello world again!"
}

#
if [ $1 == "First" ]
then
funct_1
fi
if [ $1 == "Next" ]
then
funct_2
fi



HTH ;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
dickiebird you are the one!
Thanks so much. So simple, yet not!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top