1753868 Members
7305 Online
108809 Solutions
New Discussion юеВ

Re: Creating Functions

 
SOLVED
Go to solution
Gilbert Standen_1
Frequent Advisor

Creating Functions

Hi, it was suggested I post this in this forum.

When I create a function by pasting the code to create the function into a script and run the script, the script runs but when I try to execute the function, I get the message
ksh: get_time2: not found
However, if I just paste the code to the command line, it works.
For example:

oracle [/m02/oracle/scripts]
#get_time.ksh

oracle [/m02/oracle/scripts]
#get_time2
ksh: get_time2: not found.

oracle [/m02/oracle/scripts]
#more get_time.ksh
function get_time2 {
TIME=$(date '+%m/%d/%y-%H:%M:%S')
printf "$TIME\n"
}

oracle [/m02/oracle/scripts]
#function get_time2 {
> TIME=$(date '+%m/%d/%y-%H:%M:%S')
> printf "$TIME\n"
> }

oracle [/m02/oracle/scripts]
#get_time2
05/05/05-18:20:10

Question: how can I create the functions using code that executes within a shell script?
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
7 REPLIES 7
Uwe Zessin
Honored Contributor
Solution

Re: Creating Functions

Well, OpenVMS does not have a Korn shell ;-)

I think you have to do what is called 'source' the script:
# ./get_time.sh
.
Ian Miller.
Honored Contributor

Re: Creating Functions

(I think DCL is a lot more readable :-)

Gilbert,
you may get more help in the HPUX scripting forum
http://forums1.itrc.hp.com/service/forums/categoryhome.do?categoryId=150
____________________
Purely Personal Opinion
Gilbert Standen_1
Frequent Advisor

Re: Creating Functions

Hi, replies are appreciated, but am not on OpenVMS ... not sure how that got into the mix ?
will move it to the scripting forum !
Gil
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Darrel Louis
Honored Contributor

Re: Creating Functions

Hi,

You cannot call a function from the prompt, only a existing script.

Try the following:
Content get_time.ksh
#####
function get_time2 {
TIME=$(date '+%m/%d/%y-%H:%M:%S')
printf "$TIME\n"
}

get_time2

#####

Darrel
curt larson_1
Honored Contributor

Re: Creating Functions

function names and definitions are not inherited by scripts or across seperate invocations of ksh. you should put function definitons in a file whose name is the name of the function, in a directory defined in the FPATH variable.

for a simple example:

in get_time.ksh

FPATH=/m02/oracle/functions
get_time2

create a file named get_time2 in the /m02/oracle/functions directory with your funtion definition within
Steven E. Protter
Exalted Contributor

Re: Creating Functions

A script looks like this:

check_network() {

$SCMD network status > /dev/null
rcode=$?
# echo "Network status return code: ${rcode}"
return $rcode

} # End check_network()



It is sourced into another script that with code like this:

. /usr/contrib/bin/functions


Any scrip; with this line can now call the function check_network()

Thats from a bash script but I think it should work with the korn shell.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
renarios
Trusted Contributor

Re: Creating Functions

Hi gilbert,

In general, if you create a function in one shell, only that shell recognises the function.
When executing a script using ./script.sh a new shell is opened, script is executed, feedback is given and shell gets closed.
That's why you can't create a script with a function and after running the script, trying to execute the function.
If you run the script by . script.sh, the script is executed in the current shell, but that's not recommended. The FPATH variable is preferred.

Cheerio,

Renarios
Nothing is more successfull as failure