Issue
Hello awk ninjas I need some help, so I found a solution to print defined function names from my shell script from here but my issue I would love my functions to show the comments... here is an example of how my functions are defined.
function1() { # this is a function 1
command1
}
function2() { # this is a function 2
command2
}
function3() { # this is a function 3
command3
}
So the command provided that works is this one: typeset -f | awk '/ \(\) $/ && !/^main / {print $1}'
and the output will be this:
function1
function2
function3
But I would love the output to be this way (a 4 space between the function name and comment would be really nice also):
function1 # this is a function 1
function2 # this is a function 2
function3 # this is a function 3
Thank you in advance.
Solution
Replace each #
with :
:
function1() { : this is a function 1
command1
}
function2() { : this is a function 2
command2
}
function3() { : this is a function 3
command3
}
$ typeset -f | awk '/ \() $/{fn=$1} /^ *:/{ print fn $0 }'
function1 : this is a function 1;
function2 : this is a function 2;
function3 : this is a function 3;
Change the awk script as you like to match whatever other function layouts you have and/or tweak the output however you prefer.
See What is the purpose of the : (colon) GNU Bash builtin? for more info.
Answered By - Ed Morton