Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Determining caller for a shell function

3 views
Skip to first unread message

Jeenu

unread,
Nov 21, 2009, 1:20:40 AM11/21/09
to
Hi,

If I've an exported function in BASH, how can I distinguish among the
following scenarios when the function is:

* Invoked interactively by user from command line
* Invoked from a function
* Invoked from a sub-shell or a shell script (I could test this by
inspecting $- for "i")

Thanks
Jeenu

bsh

unread,
Nov 23, 2009, 4:39:37 PM11/23/09
to
"Jeenu" <jee...@gmail.com> wrote:

First of all, let me answer from the context of ksh(1), not
bash(1), which I do not know as well, and can speak of definively.
Others will no doubt correct me if a detail here does not pertain
to bash(1), which is near enough to ksh(1), but does indeed have
subtle differences.

> If I've an exported function in BASH, how can I distinguish among the
> following scenarios when the function is:

> * Invoked from a function;

Are you talking about determining _what_ the function name of the
caller is, or merely determining whether it is called from a script
or another function, and/or a user or another script? You are not
clear on the matter.

If the former:

Let me start off by saying that it is impossible to determine the
caller of a shell function, insofar as any builtin feature is
implemented. That said, you are already aware of testing for whether
the shell is interactive or not, and presumably whether it is a login
shell or not ($SHELL =~ /-*/), and the usual tricks. But an
interactive shell does not necessarily denote whether the script
is connected to the terminal (See below).

I have accomplished this in the past by having each calling function
prepend its name to a "stack" (environment-)variable; an alternate
means would be to put this information in argv of the called function,
perhaps as a option argument which every function would be expected to
parse via getopts. This of course is only practical when all the code
is under your scripting control -- and you do talk about users or
scripts calling the script or function-via-script directly....

One could, I presume, experiment using truss(1) or dbx(1) or the like,
examining the system stack to glean the script function's name, and
therefore its caller information. I have not attempted this, and you
do not provide the information of which host and OS you are using.
Does this have to be a portable solution?

> * Invoked interactively by user from command line;

> * Invoked from a sub-shell or a shell script (I could test this by

> inspecting $- for "i").

These are the same thing! Or may be, insofar (as I said) that even a
non-interactive script can be denoted as interactive merely with the
command "set -o interactive".

Again talking about ksh(1), there are myriad caveats regarding
shell builtin versions of Unix software tools, possible functional
interpositioning of builtins and executables, and autoloading and
its FPATH envvar for function path lookup, etcetera.

Perhaps your query is as simple as parsing the output of the
"type" aka "whence -v" command, or /usr/bin/which as necessary.

=Brian

Chris F.A. Johnson

unread,
Nov 23, 2009, 6:23:27 PM11/23/09
to

Look at the FUNCNAME array:

printf "%s\n" "${FUNCNAME[@]}"

--
Chris F.A. Johnson, author <http://shell.cfajohnson,com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

mss

unread,
Nov 23, 2009, 6:46:04 PM11/23/09
to
On 2009-11-23, Chris F.A. Johnson <cfajo...@gmail.com> wrote:

> <http://shell.cfajohnson,com/>

You've a malformed URL in your sig...

--
later on,
Mike

Chris F.A. Johnson

unread,
Nov 23, 2009, 6:47:30 PM11/23/09
to
On 2009-11-23, mss wrote:
> On 2009-11-23, Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>
>> <http://shell.cfajohnson,com/>
>
> You've a malformed URL in your sig...

Thanks for pointing it out. My fingers are getting clumsy in my old
age!

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>

0 new messages