确定bash中是否存在函数

问题描述 投票:160回答:13

目前我正在做一些从bash执行的单元测试。单元测试在bash脚本中初始化,执行和清理。该脚本通常包含init(),execute()和cleanup()函数。但它们不是强制性的。我想测试它们是否定义。

我之前通过绘图和发送源来做到这一点,但它似乎是错误的。有没有更优雅的方式来做到这一点?

编辑:以下片段的工作方式类似于魅力:

fn_exists()
{
    LC_ALL=C type $1 | grep -q 'shell function'
}
bash function testing scripting
13个回答
175
投票

我想你正在寻找'type'命令。它会告诉你某些东西是函数,内置函数,外部命令还是未定义。例:

$ LC_ALL=C type foo
bash: type: foo: not found

$ LC_ALL=C type ls
ls is aliased to `ls --color=auto'

$ which type

$ LC_ALL=C type type
type is a shell builtin

$ LC_ALL=C type -t rvm
function

$ if [ -n "$(LC_ALL=C type -t rvm)" ] && [ "$(LC_ALL=C type -t rvm)" = function ]; then echo rvm is a function; else echo rvm is NOT a function; fi
rvm is a function

2
投票

我特别喜欢Grégory Joseph的解决方案

但我已经修改了一点,以克服“双引丑陋技巧”:

function is_executable()
{
    typeset TYPE_RESULT="`type -t $1`"

    if [ "$TYPE_RESULT" == 'function' ]; then
        return 0
    else
        return 1
    fi
}

2
投票

从我对另一个答案的评论(当我回到这个页面时我一直不知道)

$ fn_exists() { test x$(type -t $1) = xfunction; }
$ fn_exists func1 && echo yes || echo no
no
$ func1() { echo hi from func1; }
$ func1
hi from func1
$ fn_exists func1 && echo yes || echo no
yes

1
投票

我会改进它:

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

0
投票

可以在没有任何外部命令的情况下使用'type',但是你必须调用它两次,所以它的结果仍然是'declare'版本的两倍:

test_function () {
        ! type -f $1 >/dev/null 2>&1 && type -t $1 >/dev/null 2>&1
}

另外这在POSIX sh中不起作用,所以除了琐事之外它完全没用!


69
投票
$ g() { return; }
$ declare -f g > /dev/null; echo $?
0
$ declare -f j > /dev/null; echo $?
1

36
投票

如果声明比测试快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

18
投票

借鉴其他解决方案和评论,我想出了这个:

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。


9
投票

挖掘一个旧帖子......但我最近使用了这个并测试了两种替代方案:

test_declare () {
    a () { echo 'a' ;}

    declare -f a > /dev/null
}

test_type () {
    a () { echo 'a' ;}
    type a | grep -q 'is a function'
}

echo 'declare'
time for i in $(seq 1 1000); do test_declare; done
echo 'type'
time for i in $(seq 1 100); do test_type; done

这产生了:

real    0m0.064s
user    0m0.040s
sys     0m0.020s
type

real    0m2.769s
user    0m1.620s
sys     0m1.130s

声明是一个helluvalot更快!


6
投票

归结为使用'declare'来检查输出或退出代码。

输出方式:

isFunction() { [[ "$(declare -Ff "$1")" ]]; }

用法:

isFunction some_name && echo yes || echo no

但是,如果内存服务,重定向到null比输出替换更快(说起来,应该放弃可怕的和过时的`cmd`方法并使用$(cmd)。)并且因为声明如果找到则返回true / false找不到,函数返回函数中最后一个命令的退出代码,因此通常不需要显式返回,因为检查错误代码比检查字符串值(甚至是空字符串)要快:

退出状态样式:

isFunction() { declare -Ff "$1" >/dev/null; }

这可能就像你能得到的那样简洁和温和。


5
投票

测试不同解决方案的速度

#!/bin/bash

f () {
echo 'This is a test function.'
echo 'This has more than one command.'
return 0
}

test_declare () {
    declare -f f > /dev/null
}

test_declare2 () {
    declare -F f > /dev/null
}

test_type () {
    type -t f | grep -q 'function'
}

test_type2 () {
    local var=$(type -t f)
    [[ "${var-}" = function ]]
}

post=
for j in 1 2; do
echo
echo 'declare -f' $post
time for i in $(seq 1 1000); do test_declare; done
echo
echo 'declare -F' $post
time for i in $(seq 1 1000); do test_declare2; done
echo
echo 'type with grep' $post
time for i in $(seq 1 1000); do test_type; done
echo
echo 'type with var' $post
time for i in $(seq 1 1000); do test_type2; done
unset -f f
post='(f unset)'
done

输出例如:

声明-f

实际0m0.037s用户0m0.024s sys 0m0.012s

声明-F

实际0m0.030s用户0m0.020s sys 0m0.008s

用grep键入

实际0m1.772s用户0m0.084s sys 0m0.340s

用var类型

实际0m0.770s用户0m0.096s sys 0m0.160s

declare -f(f unset)

实际0m0.031s用户0m0.028s sys 0m0.000s

声明-F(f未设置)

实际0m0.031s用户0m0.020s sys 0m0.008s

用grep键入(f unset)

实际0m1.859s用户0m0.100s sys 0m0.348s

var的类型(f unset)

实际0m0.683s用户0m0.092s sys 0m0.160s

所以declare -F f && echo function f exists. || echo function f does not exist.似乎是最好的解决方案。


3
投票
fn_exists()
{
   [[ $(type -t $1) == function ]] && return 0
}

更新

isFunc () 
{ 
    [[ $(type -t $1) == function ]]
}

$ isFunc isFunc
$ echo $?
0
$ isFunc dfgjhgljhk
$ echo $?
1
$ isFunc psgrep && echo yay
yay
$

2
投票

这告诉你它是否存在,但不是它是一个函数

fn_exists()
{
  type $1 >/dev/null 2>&1;
}
© www.soinside.com 2019 - 2024. All rights reserved.