Operating System - HP-UX
1748259 Members
3793 Online
108760 Solutions
New Discussion юеВ

create usable persistent function

 
SOLVED
Go to solution
Gilbert Standen_1
Frequent Advisor

create usable persistent function

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?

BTW the function doesn't seem to "be" anywhere in a physical location (does not appear in any searches of disk). And it is lost when you log out of your session.
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
6 REPLIES 6
Amit Agarwal_1
Trusted Contributor

Re: create usable persistent function

Source in the shell script. Use '.' before the script name on shell prompt.

oracle [/m02/oracle/scripts]
# . get_time.ksh
Gilbert Standen_1
Frequent Advisor

Re: create usable persistent function

Hi,
Well, Amit, your reply solved part of the problem ... the function now creates from a script !

That is very much appreciated!!

The command I had to use differed slightly from yours:

. ./get_time.ksh

Next question is, how do you get a function to persist so that whenever you login, it is there ? The above got the function created for that session only. Logging in to box with another session, the function is not there.
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Gilbert Standen_1
Frequent Advisor

Re: create usable persistent function

Hi,
I found what I needed at the following webpage:
http://www.samag.com/documents/s=1153/sam0103f/0103f.htm
I see now that it has to be autoloaded from .kshrc at each logon.
Gil
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Amit Agarwal_1
Trusted Contributor
Solution

Re: create usable persistent function

If you wish you can put it in .profile. That will also work.
Daavid Turnbull
Frequent Advisor

Re: create usable persistent function

.profile is "dotted" by default when you log in.

When you "dot" a file it is similar to executing a Windows style command file. It is as if you were entering the commands therin in sequence from the command line.

"man ksh" is a reasonable reference for this behaviour.
Behold the turtle for he makes not progress unless he pokes his head out.
Daavid Turnbull
Frequent Advisor

Re: create usable persistent function

Another command that you may wish to consider using in your .profile which will achieve much the same result is "alias". it has a number of clevers that allow you to build and and tailor commands.
Behold the turtle for he makes not progress unless he pokes his head out.