检查 bash 脚本中的函数是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1007538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a function exists from a bash script
提问by csexton
How can I determine if a function is already defined in a bash script?
如何确定函数是否已在 bash 脚本中定义?
I am trying to make my .bash_login script portable between systems, so I want to add logic to only call a function if it exists.
我正在尝试使我的 .bash_login 脚本在系统之间可移植,因此我想添加逻辑以仅调用存在的函数。
I want to add __git_ps1()to PS1only if that function exists on that system. This function normally defined in git-completion.bashwhich comes with git source, or by one of the bash completion scripts that ports/apt installs.
我想添加__git_ps1()到PS1仅在该系统上存在的功能。此函数通常在git-completion.bashgit 源代码中定义,或由ports/apt 安装的 bash 完成脚本之一定义。
采纳答案by chaos
if type __git_ps1 | grep -q '^function$' 2>/dev/null; then
PS1=whatever
fi
回答by Trevor Robinson
I realize this is an old question, but none of the other answers do this quite as simply as I'd like. This uses type -t(as suggested in a comment by Chen Levy) as an efficient type-of test, but then uses shell string comparison rather than invoking grep.
我意识到这是一个老问题,但没有其他答案能像我希望的那样简单地做到这一点。这使用type -t(如 Chen Levy 的评论中所建议的)作为一种有效的测试类型,但随后使用 shell 字符串比较而不是调用 grep。
if [ "$(type -t somefunc)" = 'function' ]; then
somefunc arg1 arg2
fi
And to take it a step further, it also works indirectly:
更进一步,它也间接起作用:
funcname=do_it_special_$somehow
if [ "$(type -t $funcname)" != 'function' ]; then
funcname=do_it_normal
fi
$funcname arg1 arg2
回答by Artem Barger
You can do it using:
您可以使用:
type function_name
in will return your a function definition in case it exists. So you can check whenever output is empty or not.
in 将返回您的函数定义,以防它存在。所以你可以检查输出是否为空。
PS. Even better I've just checked it will output that function is not exist otherwise output function body.
附注。更好的是,我刚刚检查过它会输出该函数不存在,否则输出函数体。
回答by marcel
If you need a /bin/shcompliant version, you can not use typesetor declareto test for a function definition as it is not a shell builtin. Also the -foption to typemight not be available on some systems.
如果您需要/bin/sh兼容版本,则不能使用typeset或declare测试函数定义,因为它不是内置的 shell。此外,某些系统上可能不提供-f选项type。
The solution I present is partly covered in other answers already:
我提出的解决方案部分已包含在其他答案中:
isFunction() {
type | head -1 | egrep "^.*function$" >/dev/null 2>&1;
return;
}
isFunction __git_ps1 && PS1=__git_ps1
回答by Fritz G. Mehner
declare -F 'function_name' > /dev/null
声明 -F 'function_name' > /dev/null
echo $?
回声 $?
$? result has value 0 if the function exists, 1 otherwise
$? 如果函数存在,则结果值为 0,否则为 1
回答by Jacobo de Vera
I think it is better to use declare, even if it is slightly slower than type. Type suceeds also for aliases or scripts that are in the PATH.
我认为最好使用声明,即使它比类型稍慢。也为 PATH 中的别名或脚本键入 suceeds。
I am using this:
我正在使用这个:
function function_exists
{
FUNCTION_NAME=
[ -z "$FUNCTION_NAME" ] && return 1
declare -F "$FUNCTION_NAME" > /dev/null 2>&1
return $?
}
So later in my scripts I can easily see what's going on:
所以稍后在我的脚本中,我可以很容易地看到发生了什么:
if function_exists __git_ps1
then
PS1=__git_ps1
fi
Or even the still readable one-liner:
甚至仍然可读的单行:
function_exists __git_ps1 && PS1=__git_ps1
回答by tim
You may list all available functions or check individual functions with compgen:
您可以列出所有可用的函数或使用 compgen 检查单个函数:
help compgen
compgen -A function
compgen -A function myfunc 1>/dev/null && echo 'myfunc already exists' || exit 1
回答by Reed Hedges
Note, busybox (minimalist all in one shell implementation, useful on Windows for example) has "type" but "type -t" prints the wrong thing, so I just check the return value of type to see if something is callable. busybox also does not have declare.
请注意,busybox(一个 shell 实现中的极简主义,例如在 Windows 上很有用)具有“type”但“type -t”打印错误的东西,所以我只检查 type 的返回值以查看是否可以调用某些内容。busybox 也没有声明。
回答by Jaime Soriano
if declare -F | grep __git_ps1$
then
PS1=whatever
fi

