Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of function names in a shell script [duplicate]

Tags:

I have a Bourne Shell script that has several functions in it, and allows to be called in the following way:

my.sh <func_name> <param1> <param2>

Inside, func_name() will be called with param1 and param2.

I want to create a help function that would just list all available functions, even without parameters.

The question: how do I get a list of all function names in a script from inside the script?

I'd like to avoid having to parse it and look for function patterns. Too easy to get wrong.

Update: the code. Wanted my help() function be like main() - a function added to the code is added to the help automatically.

#!/bin/sh

# must work with "set -e"

foo ()
{
    echo foo: -$1-$2-$3-
    return 0
}

# only runs if there are parameters
# exits
main ()
{
    local cmd="$1"
    shift
    local rc=0
    $cmd "$@" || rc=$?
    exit $rc
}

if [[ "$*" ]]
then
    main "$@"
    die "how did we get here?"
fi