代码之家  ›  专栏  ›  技术社区  ›  terminus

确定bash中是否存在函数

  •  149
  • terminus  · 技术社区  · 16 年前

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

    我以前是通过对消息源进行greping和seding来实现这一点的,但这似乎是错误的。有更优雅的方法吗?

    编辑:下面的剪贴画就像一个魅力:

    fn_exists()
    {
        type $1 | grep -q 'shell function'
    }
    
    13 回复  |  直到 16 年前
        1
  •  167
  •   David Winiecki    9 年前

    $ type foo
    bash: type: foo: not found
    
    $ type ls
    ls is aliased to `ls --color=auto'
    
    $ which type
    
    $ type type
    type is a shell builtin
    
    $ type -t rvm
    function
    
    $ if [ -n "$(type -t rvm)" ] && [ "$(type -t rvm)" = function ]; then echo rvm is a function; else echo rvm is NOT a function; fi
    rvm is a function
    
        2
  •  62
  •   Eliran Malka    8 年前
    $ g() { return; }
    $ declare -f g > /dev/null; echo $?
    0
    $ declare -f j > /dev/null; echo $?
    1
    
        3
  •  33
  •   jpaugh JotaBe    8 年前

    -f

    #!/bin/sh
    
    function_exists() {
        declare -f -F $1 > /dev/null
        return $?
    }
    
    function_exists function_name && echo Exists || echo No such function
    

    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
    
        4
  •  16
  •   Grégory Joseph    13 年前

    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
    

        5
  •  9
  •   jonathanserafini    14 年前

    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
    

        6
  •  6
  •   Scott    12 年前

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

    isFunction some_name && echo yes || echo no
    

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

        7
  •  3
  •   Jonas    8 年前
    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
    $
    
        8
  •  3
  •   jarno    7 年前

    #!/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
    

    declare -F f && echo function f exists. || echo function f does not exist.

        9
  •  2
  •   Jason Plank IEnumerator    13 年前

    fn_exists()
    {
      type $1 >/dev/null 2>&1;
    }
    
        10
  •  2
  •   Community Lee Campbell    7 年前

    Grégory Joseph

    function is_executable()
    {
        typeset TYPE_RESULT="`type -t $1`"
    
        if [ "$TYPE_RESULT" == 'function' ]; then
            return 0
        else
            return 1
        fi
    }
    
        11
  •  1
  •   user186791    15 年前

    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
    
        12
  •  1
  •   qneill Adam Burry    7 年前

    $ 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
    
        13
  •  0
  •   Noah Spurrier    14 年前

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