Operating System - HP-UX
1830227 Members
2556 Online
109999 Solutions
New Discussion

Posix shell scripting using functions

 
SOLVED
Go to solution
Kurt Renner
Frequent Advisor

Posix shell scripting using functions

I am writing a script using many functions. Within the script, I use an environment variable (VERBOSE) set to /bin/true if I want more verbose output which spits out additional information for me to use while trouble shooting.

An example of how I use it follows:

SomeFunction()
{
${VERBOSE} && echo "SomeFunction function entered."
rc=0

:
# other logic
:
return ${rc}
}

I want to standardize on the format of all functions I create, and would like to have the VERBOSE line be dynamic without having to change the text of the message so it reflects the real name of the function in the example code above.

The script name can be referenced via $0, and arguments to the script via $1, $2,... etc. Is there some other way to reference a function name within a script? I've been unable to find any reference in the man page sh-posix to any environment variable names, etc that would reflect the function name within the script.

Thanks in advance for any help!
Do it right the first time and you will be ahead in the long run.
4 REPLIES 4
Rodney Hills
Honored Contributor
Solution

Re: Posix shell scripting using functions

I'm not sure if this will work, but couldn't you write a wrapper function and have it execute the function you want.

example, I have function do
do()
{
dofct=$1
shift
${VERBOSE} && echo "$dofct function entered."
eval "$dofct $@"
return
}

Then in my scripts I would run

do SomeFunction arg1 arg2 ...

Then "do" would optionally display trace statement and then execute the required function.

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: Posix shell scripting using functions

Hi Kurt:

I don't know of any way to directly accomplish your objective as defined. Here's what I've sometimes done in shell scripts:

Declare a local variable within each function to which you assign the name of the function:

When you need to produce output you can include the defined function name and optionally the linenumber of the function.

...
myfunction()
{
typeset -r WHO=myfunction
...
echo "Error in $WHO at $LINENO"
]

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Posix shell scripting using functions

Hi Kurt:

There is unfortunately no way to do this; there have been many times when I looked for a method without success. Not that it helps you but Perl does have a built-in function 'caller()' that does exactly this; e.g.:

$this_func_name = (caller(0))[3];

If it ain't broke, I can fix that.
Kurt Renner
Frequent Advisor

Re: Posix shell scripting using functions

Thanks for all the responses. I had a feeling that what A. Clay said was the case, but wanted to run it by.

I like Rodney's idea, but it is a little messy with too many levels of function calls. I prefer to keep it simpler for those on my team that are not regular shell scripters.

James has a good idea also. It is a little better than what I am doing, but it still is not as generic as I was hoping to be able to make my "template function".

If I find any other way, I'll be sure to let you know within this posting.

Thanks again to each one of you.
Do it right the first time and you will be ahead in the long run.