Cha*_*ffy 16 bash introspection
我正在尝试在bash中编写一些代码,它使用内省来选择要调用的相应函数.
确定候选者需要知道定义了哪些功能.仅使用参数扩展在bash中列出已定义的变量很容易:
$ prefix_foo="one"
$ prefix_bar="two"
$ echo "${!prefix_*}"
prefix_bar prefix_foo
Run Code Online (Sandbox Code Playgroud)
但是,为函数执行此操作似乎需要过滤set的输出 - 这是一种更随意的方法.
有正确的方法吗?
小智 29
compgen -A function # compgen is a shell builtin
Run Code Online (Sandbox Code Playgroud)
$ declare -F
declare -f ::
declare -f _get_longopts
declare -f _longopts_func
declare -f _onexit
...
Run Code Online (Sandbox Code Playgroud)
所以,杰德丹尼尔的别名,
declare -F | cut -d" " -f3
Run Code Online (Sandbox Code Playgroud)
削减空间并回声第3场:
$ declare -F | cut -d" " -f3
::
_get_longopts
_longopts_func
_onexit
Run Code Online (Sandbox Code Playgroud)
我有一个条目.bashrc
说:
alias list='declare -F |cut -d" " -f3'
Run Code Online (Sandbox Code Playgroud)
这允许我输入list
并获取功能列表.当我添加它时,我可能理解发生了什么,但我不记得现在挽救我的生命.
祝好运,
--jed
归档时间: |
|
查看次数: |
6003 次 |
最近记录: |