目前我正在做一些从bash执行的单元测试。单元测试在bash脚本中初始化、执行和清理。该脚本通常包含init()、execute()和cleanup()函数。但它们不是强制性的。我想测试它们是否有定义。
我以前通过greping和seding源代码做到了这一点,但这似乎是错误的。还有更优雅的方式吗?
编辑:下面这句话很有魔力:
fn_exists()
{
LC_ALL=C type $1 | grep -q 'shell function'
}
目前我正在做一些从bash执行的单元测试。单元测试在bash脚本中初始化、执行和清理。该脚本通常包含init()、execute()和cleanup()函数。但它们不是强制性的。我想测试它们是否有定义。
我以前通过greping和seding源代码做到了这一点,但这似乎是错误的。还有更优雅的方式吗?
编辑:下面这句话很有魔力:
fn_exists()
{
LC_ALL=C type $1 | grep -q 'shell function'
}
当前回答
借鉴其他解决方案和评论,我得出了以下结论:
fn_exists() {
# appended double quote is an ugly trick to make sure we do get a string -- if $1 is not a known command, type does not output anything
[ `type -t $1`"" == 'function' ]
}
用作……
if ! fn_exists $FN; then
echo "Hey, $FN does not exist ! Duh."
exit 2
fi
它检查给定的参数是否是一个函数,并避免重定向和其他grepping。
其他回答
如果declare比test快10倍,这似乎是显而易见的答案。
编辑:下面,-f选项在BASH中是多余的,可以省略它。就我个人而言,我很难记住哪个选项能做哪个,所以我两种都用。-f显示函数,-f显示函数名。
#!/bin/sh
function_exists() {
declare -f -F $1 > /dev/null
return $?
}
function_exists function_name && echo Exists || echo No such function
要声明的“-F”选项导致它只返回找到的函数的名称,而不是整个内容。
使用/dev/null不应该有任何可衡量的性能损失,如果您如此担心:
fname=`declare -f -F $1`
[ -n "$fname" ] && echo Declare -f says $fname exists || echo Declare -f says $1 does not exist
或者把两者结合起来,只为你自己毫无意义的享受。它们都有效。
fname=`declare -f -F $1`
errorlevel=$?
(( ! errorlevel )) && echo Errorlevel says $1 exists || echo Errorlevel says $1 does not exist
[ -n "$fname" ] && echo Declare -f says $fname exists || echo Declare -f says $1 does not exist
它可以归结为使用“declare”来检查输出或退出代码。
输出方式:
isFunction() { [[ "$(declare -Ff "$1")" ]]; }
用法:
isFunction some_name && echo yes || echo no
然而,如果内存有用,重定向到null比输出替换更快(说到这里,可怕的过时的' cmd '方法应该被摒弃,取而代之的是$(cmd)。)由于declare在找到/未找到时返回true/false,并且函数返回函数中最后一个命令的退出码,因此通常不需要显式返回,并且由于检查错误代码比检查字符串值(甚至是空字符串)更快:
退出状态样式:
isFunction() { declare -Ff "$1" >/dev/null; }
这可能是你能得到的最简洁和最温和的说法了。
函数的调用(如果已定义)。
已知函数名。假设名称是my_function,然后使用
[[ "$(type -t my_function)" == 'function' ]] && my_function;
# or
[[ "$(declare -fF my_function)" ]] && my_function;
函数名存储在一个变量中。如果我们声明func=my_function,那么我们可以使用
[[ "$(type -t $func)" == 'function' ]] && $func;
# or
[[ "$(declare -fF $func)" ]] && $func;
||代替&&也有同样的结果 (这样的逻辑反转在编码过程中很有用)
[[ "$(type -t my_function)" != 'function' ]] || my_function;
[[ ! "$(declare -fF my_function)" ]] || my_function;
func=my_function
[[ "$(type -t $func)" != 'function' ]] || $func;
[[ ! "$(declare -fF $func)" ]] || $func;
严格的模式和前提条件检查 我们将-e设置为严格模式。 在先决条件下,我们在函数中使用|| return。 这将迫使我们的shell进程终止。
# Set a strict mode for script execution. The essence here is "-e"
set -euf +x -o pipefail
function run_if_exists(){
my_function=$1
[[ "$(type -t $my_function)" == 'function' ]] || return;
$my_function
}
run_if_exists non_existing_function
echo "you will never reach this code"
以上是等价的
set -e
function run_if_exists(){
return 1;
}
run_if_exists
这会终止您的流程。 使用|| {true;返回;}而不是||返回;在前提条件下解决这个问题。
[[ "$(type -t my_function)" == 'function' ]] || { true; return; }
这告诉你它是否存在,但不是说它是一个函数
fn_exists()
{
type $1 >/dev/null 2>&1;
}
我特别喜欢Grégory Joseph的解决方案
但我对它做了一点修改,以克服“双引号丑陋的把戏”:
function is_executable()
{
typeset TYPE_RESULT="`type -t $1`"
if [ "$TYPE_RESULT" == 'function' ]; then
return 0
else
return 1
fi
}