- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Posix shell scripting using functions
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2002 10:54 AM
05-17-2002 10:54 AM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2002 11:18 AM
05-17-2002 11:18 AM
Solutionexample, 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2002 11:25 AM
05-17-2002 11:25 AM
Re: Posix shell scripting using functions
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2002 11:40 AM
05-17-2002 11:40 AM
Re: Posix shell scripting using functions
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];
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2002 12:24 PM
05-17-2002 12:24 PM
Re: Posix shell scripting using functions
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.