It is currently Tue, 28 Sep 2021 11:16:27 GMT



 
Author Message
 exporting functions in ksh/sh-posix

I am writing some POSIX shell scripts on HP-UX 10.10, and am having a small
problem with functions.

Basically, I have a number of scripts which are called from a main menuing
scripts (let's call it script A). Some of the called scripts (let's call them
scripts B and C) have some common functionality in them. I would like to
encapsulate this common functionality in a function (let's call it foo() ).
I would like to define foo() one time in script A, and have foo() available in
script B and C (and any other scripts I subsequently write to be called from
script A that might need it). Function foo() does NOT have to return its values
to script A, only to B and C where it is invoked.

I would prefer not to invoke scripts B and C with the '.' command (which works)
because not having them in their own subshell would really complicate some
program flow control in all the scripts (i.e, "return" and "exit" statements
would have to be replaced with "break" statements...gets kinda messy and hard
to maintain).

How can this be done?
--
Mark Landin
T. D. Williamson, Inc.
UNIX Sys. Admin



 Sun, 22 Aug 1999 03:00:00 GMT   
 exporting functions in ksh/sh-posix

: I am writing some POSIX shell scripts on HP-UX 10.10, and am having a small
: problem with functions.

: Basically, I have a number of scripts which are called from a main menuing
: scripts (let's call it script A). Some of the called scripts (let's call them
: scripts B and C) have some common functionality in them. I would like to
: encapsulate this common functionality in a function (let's call it foo() ).
: I would like to define foo() one time in script A, and have foo() available in
: script B and C (and any other scripts I subsequently write to be called from
: script A that might need it). Function foo() does NOT have to return its values
: to script A, only to B and C where it is invoked.

: I would prefer not to invoke scripts B and C with the '.' command (which works)
: because not having them in their own subshell would really complicate some
: program flow control in all the scripts (i.e, "return" and "exit" statements
: would have to be replaced with "break" statements...gets kinda messy and hard
: to maintain).

: How can this be done?
: --
: Mark Landin
: T. D. Williamson, Inc.
: UNIX Sys. Admin

in A,  typeset your functions for export:

typeset -fx fun1 fun2 ...

Then,  in scripts B and C,  do not begin the script with
"#!/bin/ksh".  Using "#!/bin/ksh" causes the shell to run in a
forked and exec'ed shell - thus,  it inherits none of the
parent shell's environment except for exported (non-array)
variables.  Begin the file with a ':' instead.  Now the shell
processes the script in a forked process that does inherit the
parent environment - exported aliases and functions and array
variables.

--
Dan Mercer
Reply To:  damer...@mmm.com

Opinions expressed herein are my own and may not represent those of my employer.



 Sun, 22 Aug 1999 03:00:00 GMT   
 exporting functions in ksh/sh-posix

Besides the : solution described by Dan Mercer, you could also define a
script, lets callit funcs.ksh, where you would include your foo()
funtion. You then read this file into B and C with ". funcs.ksh".

Hope this helps

als



 Tue, 24 Aug 1999 03:00:00 GMT   
 exporting functions in ksh/sh-posix

">
"> I am writing some POSIX shell scripts on HP-UX 10.10, and am having a small
"> problem with functions.
">
"> Basically, I have a number of scripts which are called from a main menuing
"> scripts (let's call it script A). Some of the called scripts (let's call them
"> scripts B and C) have some common functionality in them. I would like to
"> encapsulate this common functionality in a function (let's call it foo() ).
"> I would like to define foo() one time in script A, and have foo() available in
"> script B and C (and any other scripts I subsequently write to be called from
"> script A that might need it). Function foo() does NOT have to return its values
"> to script A, only to B and C where it is invoked.
">
"> I would prefer not to invoke scripts B and C with the '.' command (which works)
"> because not having them in their own subshell would really complicate some
"> program flow control in all the scripts (i.e, "return" and "exit" statements
"> would have to be replaced with "break" statements...gets kinda messy and hard
"> to maintain).
">
"> How can this be done?
"
"Besides the : solution described by Dan Mercer, you could also define a
"script, lets callit funcs.ksh, where you would include your foo()
"funtion. You then read this file into B and C with ". funcs.ksh".

if you're going to do that, you might as well define FPATH to
someplace like ~/fbin and put your functions in there, in files named
foo for foo(), bar for bar()... or one big file linked to foo, bar...
--
Andrew Hay      |    when i was young, i used to be confused by simple things.
Consultant      |    as i've grown older and wiser, my vision has widened,
HP Med. Prods.  |    my perceptions have sharpened, and my knowledge has
a...@an.hp.com |    increased, and now i'm confused on a much higher level.



 Sun, 05 Sep 1999 03:00:00 GMT   
 
   [ 4 post ] 

Similar Threads

1. Posix function exports.

2. LOCAL: LA-CA-US free presentation: ksh shell-scripting (POSIX sh)

3. lex / yacc grammar for ksh and/or POSIX sh

4. Recursion level limit in ksh and posix-sh

5. Export "depth" in sh and ksh

6. sh exporting functions?

7. ksh/posix shell math functions

8. exporting function in KSH

9. exporting function in ksh / bash


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software