Getting Function Name At Runtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting Function Name At Runtime
# 1  
Old 05-13-2003
Question Getting Function Name At Runtime

Hi,

Suppose I have a User define function get_abc in which I am using $0 to get the name of function. But when I call that function in any script, $0 will give the script name, not the function name.

For example:
Function: get_abc
-------------------
get_abc( ){
echo $0
}

Script: call_get_abc.ksh
---------------------- ---
echo "Calling get_abc"
get_abc
exit 0

Output:
Calling get_abc
call_get_abc.ksh <---- Here I want function name (get_abc)


Regards,
Yeheya
# 2  
Old 05-13-2003
All the documentation I could find says that $0 will always give the name of the script - not the function - no matter how deep inside a function it lies.. and yet when I copied your script and ran it in ksh, $0 inside a function returned the name of the function! However, when I ran it in bash, it returned the name of the script like all the documentation says it should..
Code:
get_abc( ){
 echo $0
}

echo "Calling get_abc"
get_abc
exit 0

# 3  
Old 05-14-2003
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.

But my problem is not still solved Smilie . This is not the generic and reliable way of doing things.

Thanks & Regards,
Yeheya
# 4  
Old 05-14-2003
I havent tried this so it still may print out the name of the script, but what about using typeset -fu to define the function in a separate function library. In the script, set the FPATH variable to the path to search for the function. Call the function. See what happens

function library script

my_function () {
echo "\$0 is $0"
}


your-script.ksh

#defines my_function as an undefined function. FPATH is the
#search path to auto-load the function.

FPATH=/path/to/function-library-script
typeset -fu my_function

my_function #call the function

end your-script.ksh
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Runtime input

Hi all.. I have a command for example /abc/def/ghi.jkl Filename filename1. If I run this command, it will ask for y/N which I have to type manually. Now Im trying to automate it using shell script and input the y option. Please help me on doing this. Thanks in advance. ... (7 Replies)
Discussion started by: Sathya83aa
7 Replies

2. Programming

IP address of a server at runtime

Hello, I developed a client task in C and a server task in C (an application strongly coupled). The client needs the server IP address to connect to the server. The problem is: I don't know in advance which machine will run the client and which machine will run the server so I can know the ip... (3 Replies)
Discussion started by: chercheur857
3 Replies

3. Solaris

Runtime Linker Problems ld.so.1

Hello all, I am releatively new to Solaris and I am the System administrator for my branch at the FAA. This is the first time I can say I have really messed something up thankfully. My issue came up after installing and uninstalling Oracle Secure Backup which i felt I needed to do a clean... (7 Replies)
Discussion started by: jbirkes
7 Replies

4. Solaris

Re:How to get the path during runtime?

Thank u vgesh99 It works well.. (1 Reply)
Discussion started by: Nandagopal
1 Replies

5. Solaris

How to get the path during runtime?

Hi guys, I have commands like /solaris/opt/VRTS/bin/vxdisk /opt/VRTS/bin/vxdisk From these two commands, i need to get the directory path value before /VRTS/bin/vxdisk at runtime. e.g /solaris/opt/VRTS/bin/vxdisk as /solaris/opt /opt/VRTS/bin/vxdisk as /opt I have tried with cut... (1 Reply)
Discussion started by: Nandagopal
1 Replies

6. Programming

Runtime.getSystem.exec() function waits for child

Runtime.getSystem.exec() waits for child process to complete .. I do not want to wait for the child process to complete ..what is the option which has to be used ? (2 Replies)
Discussion started by: shafi2all
2 Replies

7. Shell Programming and Scripting

finding string at runtime

Hi I am creating a script in shell (sh), in which i will enter a string at runtime. let i will enter the string asdfergtdev_DEV2(or DEV,DEV3,DEV1 etc). i want it will find DEV2(it will be only find DEV2 at last 4 digit 3 in case only DEV,no metter if before it, there is dev) and append _qqq01... (1 Reply)
Discussion started by: inderpunj
1 Replies

8. UNIX for Dummies Questions & Answers

Runtime Error...

My system did stay appears the error Run Time Library Error. What itīs? When the error appear, iīve to reboot my system and lost all I did. Is there the UNIX System problem? Please. I need help!!! (4 Replies)
Discussion started by: marpin
4 Replies

9. Solaris

Runtime error...

My interprise use a UNIX mainform for a instrumentation process control. The control use the FOXBORO INVENSYS system and they donīt gonna solve the problem the run time error. The run time error happen without logic explication. When everything itīs run perfectely and happenly appears the run time... (1 Reply)
Discussion started by: marpin
1 Replies
Login or Register to Ask a Question