参见英文答案 > How do I list the functions defined in my shell?????????????????????????????????????6个
我有一个Bourne Shell脚本,其中包含多个函数,并允许以下列方式调用:
我有一个Bourne Shell脚本,其中包含多个函数,并允许以下列方式调用:
my.sh
在里面,将使用param1和param2调用func_name().
我想创建一个帮助函数,只列出所有可用的函数,即使没有参数.
问题:如何从脚本内部获取脚本中所有函数名称的列表?
我想避免解析它并寻找功能模式.太容易出错了.
更新:代码.想要我的help()函数就像main() – 添加到代码中的函数会自动添加到帮助中.
#!/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