Need help on awk for printing the function name inside each function

Hi,

I am having script which contains many functions. Need to print each function name at the starting of the function. Like below,

functionname()
{
echo "functionname"
commands....
}

I've tried like below,

func=`grep "()" scriptname | cut -d "(" -f1`
for i in $func
do
nawk -v var="$i" '1;/var/{c=2}c&&!--c{print "var"}' scriptname
done

But here am not able to get the expected output with "-v" option,Otherwise its working fine.

TIA

awk 's construct /var/ looks for exactly that: the three char sequence "v", "a", and "r". That is not recognised as a variable and thus not replaced by its value.
Try $0 ~ var or the match function.

EDIT: Not exactly sure what you're after, but would this go into the right direction:

awk '
1
/\(\)/  {sub (/\(\)/, _)
         var = $0
         L = NR + 1
        }
NR == L {print "echo \"" var "\""
        }
' scriptname

HI,

As my script is having too many functions.I need to know the flow while executing..Because of that i need to print each function names inside the function(echo 'functionname')

Note:I can check the flow using bash -x. But it will be better if i know the flow with normal execution.

Now I have used

$0~var

and its working fine. But I need an option like

sed -i

,Save changes in place.

Did you consider bash 's other debug functionalities, like shopt -s extdebug , or trap ... DEBUG ?

awk doesn't have a feature similar to sed -i . Print to a temp file, and then overwrite the original.

1 Like

Thanks a lot for the reply..

I have done using "sed" like below and its working fine.

func=`grep "()" Scriptname | cut -d "(" -f1`
for i in $func
do
sed  -i "/$i()/ {n;a\
echo $i
}" Scriptname
done