Reputation: 1
I am trying to extract the current running function name to generate logs for assertion. here is what i have tried
function_name()
{
s=${FUNCNAME[0]}
touch ${s}
}
I think the ${FUNCNAME[0]} only works in bash not in sh.
Is there any way to get the current running function name in sh
Upvotes: 0
Views: 122
Reputation: 103
One way to do it is to insert the logging expressions manually into every function, that's tedious but it can be automated with awk.
Assume you obey the coding conventions you used in the question:
#!/usr/bin/awk
/^[ \t]*function[ \t]*[a-zA-Z_][^(]*\(\)/ {
name=$0
sub(/^[ \t]*function[ \t]*/, "", name)
sub(/[ \t]*\(\).*/, "", name)
if ($0 !~ /\{/) {
print
getline
}
print
print " s='" name "'"
print " touch ${s}"
next
}
1
The code is self-explanatory, for each function in the sh script, it parses the function name, and insert the logging code below. Note the last line in the above code, which means print out all the other lines.
Upvotes: 1