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

How to echo a function name?

12 views
Skip to first unread message

Matthew Landt

unread,
Jan 17, 2000, 3:00:00 AM1/17/00
to
I am trying to enable some debugging and I can't get functions to return
their own name in KSH. $0 returns the shell or other script that called them.

$ testfunc()
{
echo "function: testfunc"
echo "\$0: $0"
}

$ testfunc
function: testfunc
$0: -ksh

I can use the history to get the first function call, but if a function is
called within a function... this doesn't work.

$ firstfunc()
{
echo "firstfunc: $(fc -nl -0)"
secondfunc
}

(0) landt @ bucky: 4.3.2.0 /home/landt
$ secondfunc()
{
echo "secondfunc: $(fc -nl -0)"
}

(0) landt @ bucky: 4.3.2.0 /home/landt
$ firstfunc
firstfunc: firstfunc
secondfunc: firstfunc

Any ideas?

- Matt

--
Matthew Landt <la...@austin.ibm.com> _ _ . _ __.____. \/\|/\/
AIX and HACMP Certified Specialist | | / \ |\| | \. ,_| ` o O '
/ Comments, views, and opinions \ | |_/ ^ \| | ) | | x
\ are mine alone, not IBM's. / |___|/~\_\_|\|__/|_| \___/

CSC News

unread,
Jan 18, 2000, 3:00:00 AM1/18/00
to

Matthew Landt <la...@austin.ibm.com> wrote in message
news:38837438...@austin.ibm.com...


I write my shell scripts to include a debug option.
Dirty example

#!/bin/ksh
DEBUG=0
while [ $# -gt 0 ]
do
case $1 in
-d)
echo DEBUG:Entering Debug Mode.
DEBUG=1
shift;;
*) echo "Option not understood"
shift;;
esac
done
testfunc()
{
if [ DEBUG -eq 1 ]
then
echo DEBUG:Entered the testfunc
fi
echo Hello World
}
functest()
{
if [ DEBUG -eq 1 ]
then
echo DEBUG:Entered the functest
fi
echo Goodbye World
}
testfunc
functest

Hope this gives you some ideas.
Marc

Sweth Chandramouli

unread,
Jan 18, 2000, 3:00:00 AM1/18/00
to
In article <38837438...@austin.ibm.com>,

Matthew Landt <la...@austin.ibm.com> wrote:
>I am trying to enable some debugging and I can't get functions to return
>their own name in KSH. $0 returns the shell or other script that called them.
if you are just debugging, why not turn on xtrace? it will
show you exactly where you are in the script at any point. also, the oreilly
ksh book includes a pretty robust ksh debugger that lets you set breakpoints,
step through a program, etc.
assuming that your goal is to have portable definitions that
will adapt to the changing of the function name, the other alternative would
be to define your functions dynamically by eval-ing the definition:

$ bar=foo
$ eval "function $bar {
> echo "starting function $bar..."
> do_whatever_you_want
> }
> "
$

(to answer the actual question you asked, i don't think
that the name of the function is every available to the function itself.)

-- sweth.

--
Sweth Chandramouli ; <sw...@gwu.edu>
<a href="http://astaroth.nit.gwu.edu/resume/">Will Work For Food.</a>
<a href="http://astaroth.nit.gwu.edu/~sweth/disc.html">*</a>

Matthew Landt

unread,
Jan 18, 2000, 3:00:00 AM1/18/00
to

Yes, I thought of this. However, I am trying to get debugging into
scripts that already exist. In this env there are well over 1000
seperate functions that get called. I was hoping to use one simple
line that could be easily placed in EVERY function. That way I don't
have to waste time adding function names, etc. Also... functions can get
cut/paste/edited/renamed by other people. That is why I want the
function name to be intelligent.

Here is the debug I use:

The user has the variable DEBUG_LEVEL. This can be tuned to get
various levels of debugging.

I have a 3 tier structure. 1 - debug level1, 2 - debug level2,
4 - debug level3. This is the same idea of chmod. I can set
the debug level to 6 to get levels2 and 3 debugged, 7 for all,
0 for none, etc.

Here is my debug line at the top and then bottom of the functions:

[[ $((($DEBUG_LEVEL & $LEVEL1))) -ne 0 ]] &&
{ set -x; echo "# START PROGNAME: $0"; } || set +x

...

[[ $((($DEBUG_LEVEL & $LEVEL1))) -ne 0 ]] &&
echo "# END PROGNAME: $0"

I would like the above lines to be the exact same for all functions.
But at the same time, I want to report when I enter and exit
a function. I want to be able to figure out what gets called when

START PROGNAME: function1
START PROGNAME: function2
START PROGNAME: function3
END PROGNAME: function3
START PROGNAME: function4
END PROGNAME: function4
END PROGNAME: function2
END PROGNAME: function1

I just need to figure out how to get the $0 like functionality to
work in this environment.

Matthew Landt

unread,
Jan 18, 2000, 3:00:00 AM1/18/00
to
Sweth Chandramouli wrote:
>
> In article <38837438...@austin.ibm.com>,
> Matthew Landt <la...@austin.ibm.com> wrote:
> >I am trying to enable some debugging and I can't get functions to return
> >their own name in KSH. $0 returns the shell or other script that called them.
> if you are just debugging, why not turn on xtrace? it will
> show you exactly where you are in the script at any point. also, the oreilly
> ksh book includes a pretty robust ksh debugger that lets you set breakpoints,
> step through a program, etc.
> assuming that your goal is to have portable definitions that
> will adapt to the changing of the function name, the other alternative would
> be to define your functions dynamically by eval-ing the definition:
>
> $ bar=foo
> $ eval "function $bar {
> > echo "starting function $bar..."
> > do_whatever_you_want
> > }
> > "
> $
>
> (to answer the actual question you asked, i don't think
> that the name of the function is every available to the function itself.)
>
> -- sweth.

Unfortunately debugging is not that simple in this environment. This
is an automated test driver and the scripts are being read by the
driver, not me. I don't have control over any other part except what
gets called within the script. I don't have control on when and how
the script gets called.

There could be 1000's of functions. All these functions get sourced
into the driver. So it can be difficult and tedious to try and
use/control the eval method... Although it is one of the better ideas.
It does avoid hard coding of the function name into the debug
statement.

- matt

Dan Mercer

unread,
Jan 18, 2000, 3:00:00 AM1/18/00
to
Your problem stms from using POSIX style function definitions -
that is,those that are defined as:

funcname() { list; }

Pdksh and ksh93 and later versions (post POSIX) of ksh88
apply strict POSIX functionality to POSIX style functions - which is why
you should never use POSIX style functions in ksh scripts because
their behavior may change even from one ksh88 to another. For instance,
HP-UX's ksh88 is still pre-POSIX as far as handling functions.

In POSIX, $0 is the name of the script and not the function. However,
if you define the functions as Korn functions using the Korn syntax:

function funcname { list; }

then $0 will be set to the function name, not the script name.
Korn functions also allow you to specify local variables using
the typeset command and in general are superior to POSIX
functions (you can set local traps, for instance). You do not
have to modify individual functions to be able to trace them -
simply modify PS4:

PS4='${0##*/}:$LINENO: '

You can turn on trace for all defined functions with:

typeset -ft $(typeset +f)

Here's part of the standard header I use. The -x option turns on global
tracing. The "-X funcname" turns on tracing for funcname. The
"+X funcname" turns on global tracing but turns off function tracing
for the functions named in $OPTARG.

while getopts :hvxX: opt
do
case $opt in
h) help;;
v) ident $0;exit;;
x) PS4='${0##*/}:$LINENO: ';typeset -ft $(typeset +f);set -x;;
X) PS4='${0##*/}:$LINENO: ';typeset -ft ${OPTARG};;
+X) PS4='${0##*/}:$LINENO: ';typeset +ft ${OPTARG};set -x;;
:) help "Option ${OPTARG} is missing it's argument";;
?) help "Invalid option ${OPTARG}";;
esac
done

shift OPTIND-1

For a script xxx with functions abc,def,ghi:

xxx -X "abc def" # trace abc def
xxx +X "abc def" # trace all but abc def

--
Dan Mercer
dame...@uswest.net

"CSC News" <mrobert...@csc.com> writes:
>
> Matthew Landt <la...@austin.ibm.com> wrote in message
> news:38837438...@austin.ibm.com...

>> I am trying to enable some debugging and I can't get functions to return
>> their own name in KSH. $0 returns the shell or other script that called
> them.
>>

>> $ testfunc()
>> {
>> echo "function: testfunc"
>> echo "\$0: $0"
>> }
>>
>> $ testfunc
>> function: testfunc
>> $0: -ksh
>>
>> I can use the history to get the first function call, but if a function is
>> called within a function... this doesn't work.
>>
>> $ firstfunc()
>> {
>> echo "firstfunc: $(fc -nl -0)"
>> secondfunc
>> }
>>
>> (0) landt @ bucky: 4.3.2.0 /home/landt
>> $ secondfunc()
>> {
>> echo "secondfunc: $(fc -nl -0)"
>> }
>>
>> (0) landt @ bucky: 4.3.2.0 /home/landt
>> $ firstfunc
>> firstfunc: firstfunc
>> secondfunc: firstfunc
>>
>> Any ideas?
>>
>> - Matt
>>

>> --
>> Matthew Landt <la...@austin.ibm.com> _ _ . _ __.____. \/\|/\/
>> AIX and HACMP Certified Specialist | | / \ |\| | \. ,_| ` o O '
>> / Comments, views, and opinions \ | |_/ ^ \| | ) | | x
>> \ are mine alone, not IBM's. / |___|/~\_\_|\|__/|_| \___/
>
>

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


Matthew Landt

unread,
Jan 18, 2000, 3:00:00 AM1/18/00
to Dan Mercer
Dan Mercer wrote:
>
> Your problem stms from using POSIX style function definitions -
> that is,those that are defined as:
>
> funcname() { list; }
>

Dan, you are the man! I always wondered the diff between function funcname
and funcname() was. I used the latter for brevity. Now I have to try
and change my scripting style.

0 new messages