Operating System - Linux
1753442 Members
4712 Online
108794 Solutions
New Discussion юеВ

Re: Making functions in Korn Shell .

 
SOLVED
Go to solution
Manuales
Super Advisor

Making functions in Korn Shell .

Hi ..
i need make many scripts, for that reasen i need make many functions ... i'm going to do them in korn shell.
I remember in profile it must have a variable called FPATH i do not remember how declare it, could you tell me sintaxis please?

and, in this variable i know i have put the path where are going to be all functions, i remember only is necesary named the file like name function, is correct?
do i have make another thing?
do you know any manual o tutorial where it indicates how to create functions in korn shell?

thanks!! Manuales.
6 REPLIES 6
Geoff Wild
Honored Contributor
Solution

Re: Making functions in Korn Shell .


Functions are quite easy:

# my function
function myfunction
{
add code here
exit
}

Example:

function print_hp
{
Outpath=/var/spool/lp/receive
PSMServer=prdprt
# Strip path if any and take only filename
Filename=`echo $F | sed 's/.*\///'`
# Append filename to output file path
Fileout=$Outpath/${Filename}

rcp $F ${PSMServer}:$Outpath
rtc=$?
if test "$rtc" != 0
then
echo "Cannot move $F to $Fileout"
echo "Cannot move $F to $Fileout" 1>&2
exit $rtc
fi
# -d -n -p -o
remsh $PSMServer lp -d$P -n$C -p3 -onb $Fileout 1>&2
rtc=$?
if test "$rtc" != 0
then
echo "petroprt returned error code $rtc"
echo "petroprt returned error code $rtc" 1>&2
exit $rtc
fi
rm -f $4
#
return ${rtc}
Status=$?
if test "$Status" != 0
then
echo "Cannot print $F to $P"
echo "Cannot print $F to $P" 1>&2
exit $Status
fi
exit
}


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: Making functions in Korn Shell .

BTW - if the functions are to be used by many scripts, create a file called, say common.functions

Then source it from your scripts:

FUNC_FILE=/usr/local/common.functions

# Source the files and functions
. $FUNC_FILE

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: Making functions in Korn Shell .

Hi Manuales:

The manpages provide your first source of reference and answers:

http://www.docs.hp.com/en/B2355-60103/ksh.1.html

The Shell Users Guide is also a useful reference:

http://www.docs.hp.com/en/B2355-90046/B2355-90046.pdf

Regards!

...JRF...
Manuales
Super Advisor

Re: Making functions in Korn Shell .

thank all .. i was refered to globar functions ..

i don't know where use typeset -fu, autoload, FPATH, ...

Thanks, Manuales.
Geoff Wild
Honored Contributor

Re: Making functions in Korn Shell .

FPATH should be set in the script - not exported.

THere is an interesting thread about ksh scripting here:

http://www.exforsys.com/forum/showthread.php?t=81773

Particulary, the rather long (but informative) post by Dan Mercer...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Manuales
Super Advisor

Re: Making functions in Korn Shell .

Hi all!!
i have the solution:

1st step:
in .profile file put:
FPATH=/.../../FUNCTIONS/
typeset -fu function_one
typeset -fu function_two
typeset -fu function_three
....
....
....
typeset -fu function_"n"

then, in path /.../../FUNCTION create new file, the name of this file must be same name in the function, for exammple:

======================
cat > function_one
function function_one
{
echo "Hi, this is a test"
}
ctrl + c

more function_one
function function_one
{
echo "Hi, this is a test"
}

======================
then since other file you must invoke it, for example, in a file named "verificando"

cat > verificando
#(1st step done)
. $HOME/.../../FUNCTIONS/.profile
functione_one

ctrl+c

cat verificando:
#(1st step done)
. $HOME/.../../FUNCTIONS/.profile
functione_one

Running script:
unix> ./verificando
Hi, this is a test

Thanks friends !!!
Manuales