目前我正在进行一些从bash执行的单元测试。单元测试在bash脚本中初始化、执行和清理。这个脚本通常包含一个init()、execute()和cleanup()函数,但它们不是必需的。我想测试它们是否被定义。
我之前通过grep和sed源代码来实现这个功能,但感觉不太对。有没有更优雅的方法?
编辑:下面的代码片段非常好用:
fn_exists()
{
LC_ALL=C type $1 | grep -q 'shell function'
}
这会告诉你它是否存在,但不会告诉你它是一个函数
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
}
type
调用放在引号内。 - akostadinov你可以用4种方式来检查它们
fn_exists() { type -t $1 >/dev/null && echo 'exists'; }
fn_exists() { declare -F $1 >/dev/null && echo 'exists'; }
fn_exists() { typeset -F $1 >/dev/null && echo 'exists'; }
fn_exists() { compgen -A function $1 >/dev/null && echo 'exists'; }
我会进行改进:
fn_exists()
{
type $1 2>/dev/null | grep -q 'is a function'
}
使用方法如下:
fn_exists test_function
if [ $? -eq 0 ]; then
echo 'Function exists!'
else
echo 'Function does not exist...'
fi
可以使用'type'而不需要任何外部命令,但是你必须调用它两次,所以它仍然比 'declare' 版本慢大约两倍:
test_function () {
! type -f $1 >/dev/null 2>&1 && type -t $1 >/dev/null 2>&1
}
此外,这在 POSIX sh 中不起作用,因此除了作为琐事外毫无价值!
fn_exists foo || foo() { :; }
- Harveytype -t
和==
,您可以简化grep的命令。 - Roland WeberLC_ALL=C
来解决问题。 - gaRex