Bash 代码>foo='*';echo$foo到foo='*';echo“$foo”。您不需要将其设置为只读,只需检查它是否已设置[[$original_function_list]]| | original_function_list=$(…)很好的调用--我没
Bash 代码>foo='*';echo$foo到foo='*';echo“$foo”。您不需要将其设置为只读,只需检查它是否已设置[[$original_function_list]]| | original_function_list=$(…)很好的调用--我没,bash,introspection,Bash,Introspection,代码>foo='*';echo$foo到foo='*';echo“$foo”。您不需要将其设置为只读,只需检查它是否已设置[[$original_function_list]]| | original_function_list=$(…)很好的调用--我没有想到将declare-fs读入数组,并在扩展时将它们剥离出来。在2.0.4的情况下,可能会使它declare-F&&printf'\0'在进程替换内部——这样read的退出状态将为零(并且,作为额外的条件,任何以某种方式停止declare-F
代码>foo='*';echo$foo到
foo='*';echo“$foo”
。您不需要将其设置为只读,只需检查它是否已设置[[$original_function_list]]| | original_function_list=$(…)
很好的调用--我没有想到将declare-f
s读入数组,并在扩展时将它们剥离出来。在2.0.4的情况下,可能会使它declare-F&&printf'\0'
在进程替换内部——这样read
的退出状态将为零(并且,作为额外的条件,任何以某种方式停止declare-F
工作的错误都将被有效地传递出去)。@charlesduff谢谢,补充。这回答了你的问题吗@安非他命机器,嗯。这是个好问题,是把它当作另一个的复制品来关闭,还是反过来关闭——这是两个问题中的第一个,有更多的答案(10对8)。在两种可能的方法中,你有理由选择这个方向吗?
$ prefix_foo="one"
$ prefix_bar="two"
$ echo "${!prefix_*}"
prefix_bar prefix_foo
set \
| egrep '^[^[:space:]]+ [(][)][[:space:]]*$' \
| sed -r -e 's/ [(][)][[:space:]]*$//'
declare -F
alias list='declare -F |cut -d" " -f3'
$ declare -F
declare -f ::
declare -f _get_longopts
declare -f _longopts_func
declare -f _onexit
...
declare -F | cut -d" " -f3
$ declare -F | cut -d" " -f3
::
_get_longopts
_longopts_func
_onexit
saveIFS="$IFS"
IFS=$'\n'
funcs=($(declare -F)) # create an array
IFS="$saveIFS"
funcs=(${funcs[@]##* }) # keep only what's after the last space
$ for i in ${funcs[@]}; do echo "$i"; done
__ack_filedir
__gvfs_multiple_uris
_a2dismod
. . .
$ echo ${funcs[42]}
_command
compgen -A function # compgen is a shell builtin
functions=$(for c in $patterns; do compgen -A function | grep "^$c\$")
which() {
for c in "$@"; do
compgen -A function |grep "^$c\$" | while read line; do
echo "shell function $line" 1>&2
done
/usr/bin/which "$c"
done
}
(xkcd)Sandy$ which deactivate
shell function deactivate
(xkcd)Sandy$ which ls
/bin/ls
(xkcd)Sandy$ which .\*run_hook
shell function virtualenvwrapper_run_hook
#!/bin/bash
# list-defined-functions.sh
# Lists functions defined in this script.
#
# Using `compgen -A function`,
# We can save the list of functions defined before running out script,
# the compare that to a new list at the end,
# resulting in the list of newly added functions.
#
# Usage:
# bash list-defined-functions.sh # Run in new shell with no predefined functions
# list-defined-functions.sh # Run in current shell with plenty of predefined functions
#
# Example predefined function
foo() { echo 'y'; }
# Retain original function list
# If this script is run a second time, keep the list from last time
[[ $original_function_list ]] || original_function_list=$(compgen -A function)
# Create some new functions...
myfunc() { echo "myfunc is the best func"; }
function another_func() { echo "another_func is better"; }
function superfunction { echo "hey another way to define functions"; }
# ...
# function goo() { echo ok; }
[[ $new_function_list ]] || new_function_list=$(comm -13 \
<(echo $original_function_list) \
<(compgen -A function))
echo "Original functions were:"
echo "$original_function_list"
echo
echo "New Functions defined in this script:"
echo "$new_function_list"
readarray -t funcs < <(declare -F)
printf '%s\n' "${funcs[@]##* }"
IFS=$'\n' read -d '' -a funcs < <(declare -F)
IFS=$'\n' read -d '' -a funcs < <( declare -F && printf '\0' )
typeset -f +
-f The names refer to functions rather than parameters.
+ If `+' appears by itself in a separate word as the last
option, then the names of all parameters (functions with -f)
are printed, but the values (function bodies) are not.
martin@martin ~ % cat test.zsh
#!/bin/zsh
foobar()
{
echo foobar
}
barfoo()
{
echo barfoo
}
typeset -f +
martin@martin ~ % ./test.zsh
barfoo
foobar