Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I used to write korn shell script.
How can I store common shell script functions in one or more shell script source files and then store the shell script source files in one or more directory(ies); so that I can call the functions from within any shell scripts ?

Reginald:
Using the Korn shell, the preferred method is place all your functions in a director, pointed to by shell variable FPATH. i.e: export FPATH=/home/nails/functions.
Say, you have a function called downshift:
# downshift: this function return the argument as downshifted string
# usage: cmmd=$(down_shift $cmmd)
function downshift {
echo $1 | tr '[A-Z]' '[a-z]'
} # end down_shiftplace it in /home/nails/functions/downshift and now Korn shell scripts automatically load downshift if it's needed.
Another method that works with both Korn and Bourne is to place all functions in 1 file and "source" it:
. /home/nails/functions/filefunction
but this loads all the functions whether they're required of not.
Regards,
Nails

Nails,
Thanks for your reply.
I know FPATH setting. But I've found the source shell script file name must be the same as the function name. In that case, it seems that I am forced to store only one function in each shell script source file. Is it true ?
Do I also need to use typeset ? If yes, how and why ?

Reginald:
If you are going to use the FPATH functionality, I'm afraid you must store a single function in a file of the same name. The only exception is if the function calls another function it may alos reside in that file, but you can't call it from the main script. (I never use this).
If you autoload a function:
autoload downshift
The FPATH directories is searched before PATH. autoload is an alias for 'typeset -fu'.
Of course, if you choose to source a file, all your functions can be in one file. We've already talked about the drawback of doing this.Regards,
Nails

Reginald:
If you load all the functions in a file, and "source" the file (as described in my original post), all the functions are loaded into your environment whether they're required or not.
If the file is large, the loading time maybe prohibitive. Program execution maybe affected also.
Regards,
Nails

Nails,
What is the purpose of -u option in typeset upon declaring function using typeset.
That is, what if I type as follows:typeset -f function_name
instead of
typeset -fu function_name

Reginald:
The -u option of typeset leaves the function undefined until it's needed. Thus, in the event you declare the function, but never use it in the script, this saves shell processing time. Another way of saying why load the function if you don't use it.
Regards,
Nails

But why I've found from a number of HP and non-HP Unix reference manuals that the '-u' option is used to convert all lowercase characters to uppercase characters ? I have never found any clue similar to your explanation. Is it an undocumented secret ?

Reginald:
I'm not an HP person, so I can comment on that. You are correct about -u except where it's used with the -f option.
A good reference that covers this is "Korn Shell Programming by Example" by Dennis O'Brein and David Pitts.
Also, here is an excerpt from my Solaris "man" page for typeset:
-f The names refer to function names rather than variable names. No assignments can be made and the only other valid flags are -t, -u and -x. The flag -t turns on execution tracing for this function.
The flag -u causes this function to be marked undefined. The FPATH variable will be searched to find the function definition when the function is referenced.Regards,
Nails

![]() |
Installing unix with winx...
|
Why this command to unzip...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |