Error[8]: Undefined offset: 4, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述参见英文答案 > How do I list the functions defined in my shell?                                    6个 我有一个Bourne Shell脚本,其中包含多个函数,并允许以下列方式调用: my.sh< func_name> <参数1> < param2的> 将使用param1和param2调用func_name() 参见英文答案 > How do I list the functions defined in my shell?6个
我有一个Bourne Shell脚本,其中包含多个函数,并允许以下列方式调用:

my.sh< func_name> <参数1> < param2的>

将使用param1和param2调用func_name()内部.

我想创建一个“帮助”功能,只列出所有可用的功能,即使没有参数.

问题:如何从脚本内部获取脚本中所有函数名称的列表?

我想避免解析它并寻找功能模式.太容易出错了.

谢谢,
亚历克斯

更新:代码.想要我的help()函数就像main() – 添加到代码中的函数会自动添加到帮助中.

#!/bin/sh# must work with "set -e"foo (){    echo foo: ----    return 0}# only runs if there are parameters# exitsmain (){    local cmd=""    shift    local rc=0    $cmd "$@" || rc=$?    exit $rc}if [[ "$*" ]]then    main "$@"    dIE "how dID we get here?"fi
解决方法 您可以在自己的脚本上使用grep命令获取脚本中的函数列表.为了使这种方法起作用,您需要以某种方式构造函数,以便grep可以找到它们.这是一个示例:
$cat my.sh#!/bin/shfunction func1() # Short description{    echo func1 parameters:  }function func2() # Short description{    echo func2 parameters:  }function help() # Show a List of functions{    grep "^function" 
$my.sh function func1() # Short descriptionfunction func2() # Short descriptionfunction help() # Show a List of functions$my.sh helpfunction func1() # Short descriptionfunction func2() # Short descriptionfunction help() # Show a List of functions$my.sh func1 a bfunc1 parameters: a b$my.sh func2 x yfunc2 parameters: x y
}if [ "_" = "_" ]; then helpelse "$@"fi

这是一个互动演示:

my_private_function(){    # Do something}

如果你有“私人”功能,你不想在帮助中显示,那么省略“功能”部分:

[+++] 总结

以上是内存溢出为你收集整理的linux – 获取shell脚本中的函数名列表全部内容,希望文章能够帮你解决linux – 获取shell脚本中的函数名列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
linux – 获取shell脚本中的函数名列表_系统运维_内存溢出

linux – 获取shell脚本中的函数名列表

linux – 获取shell脚本中的函数名列表,第1张

概述参见英文答案 > How do I list the functions defined in my shell?                                    6个 我有一个Bourne Shell脚本,其中包含多个函数,并允许以下列方式调用: my.sh< func_name> <参数1> < param2的> 将使用param1和param2调用func_name() 参见英文答案 > How do I list the functions defined in my shell?6个
我有一个Bourne Shell脚本,其中包含多个函数,并允许以下列方式调用:

my.sh< func_name> <参数1> < param2的>

将使用param1和param2调用func_name()内部.

我想创建一个“帮助”功能,只列出所有可用的功能,即使没有参数.

问题:如何从脚本内部获取脚本中所有函数名称的列表?

我想避免解析它并寻找功能模式.太容易出错了.

谢谢,
亚历克斯

更新:代码.想要我的help()函数就像main() – 添加到代码中的函数会自动添加到帮助中.

#!/bin/sh# must work with "set -e"foo (){    echo foo: ----    return 0}# only runs if there are parameters# exitsmain (){    local cmd=""    shift    local rc=0    $cmd "$@" || rc=$?    exit $rc}if [[ "$*" ]]then    main "$@"    dIE "how dID we get here?"fi
解决方法 您可以在自己的脚本上使用grep命令获取脚本中的函数列表.为了使这种方法起作用,您需要以某种方式构造函数,以便grep可以找到它们.这是一个示例:
$cat my.sh#!/bin/shfunction func1() # Short description{    echo func1 parameters:  }function func2() # Short description{    echo func2 parameters:  }function help() # Show a List of functions{    grep "^function" 
$my.sh function func1() # Short descriptionfunction func2() # Short descriptionfunction help() # Show a List of functions$my.sh helpfunction func1() # Short descriptionfunction func2() # Short descriptionfunction help() # Show a List of functions$my.sh func1 a bfunc1 parameters: a b$my.sh func2 x yfunc2 parameters: x y
}if [ "_" = "_" ]; then helpelse "$@"fi

这是一个互动演示:

my_private_function(){    # Do something}

如果你有“私人”功能,你不想在帮助中显示,那么省略“功能”部分:

总结

以上是内存溢出为你收集整理的linux – 获取shell脚本中的函数名列表全部内容,希望文章能够帮你解决linux – 获取shell脚本中的函数名列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/1033857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存