测试bash函数返回值的正确方法是什么?

时间:2011-06-05 05:53:36

标签: bash function

我想在if语句中测试一个bash函数返回值,如下所示:

if [[ func arg ]] ; then …

但我得到的错误信息如下:条件二进制运算符。

这样做的正确方法是什么?

是吗:

 if [[ $(func arg) ]] ; then ...

5 个答案:

答案 0 :(得分:61)

如果是退出代码而不是结果,您可以使用

if func arg; then ...

如果你不能使函数返回正确的退出代码(使用return N),并且你必须使用字符串结果,请使用@Alex Gitelman答案。

$ help if

  

if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi

     

根据条件执行命令。

     

执行if COMMANDS列表。如果它的退出状态为零,那么       then COMMANDS列表已执行。否则,每个elif COMMANDS列表都是       依次执行,如果其退出状态为零,则对应       执行then COMMANDS列表,if命令完成。除此以外,       如果存在,则执行`else COMMANDS'列表。退出状态       整个构造是最后执行的命令的退出状态,或者为零       如果没有条件测试成真。

     

退出状态:       返回最后执行的命令的状态。

答案 1 :(得分:15)

如果函数返回多个单词,则似乎会产生此错误。

例如,1 2

引用它:

"$(func arg)"

样品:

$ if [[ 1 2 ]] ; then echo 1 ; fi
-bash: conditional binary operator expected
-bash: syntax error near `2'
$ if [[ "1 2" ]] ; then echo 1 ; fi
1

如果你比较0和非0只是使用

if [[ "$(func arg)" != "0" ]]

答案 2 :(得分:14)

这对我很有用,所以我会添加以下细节。

如果你需要测试两个条件,一个是函数/命令的退出状态,另一个是例如变量值使用此:

if func arg && [[ $foo -eq 1 ]]; then echo TRUE; else echo FALSE; fi

答案 3 :(得分:1)

在相关说明中,如果函数返回各种退出代码而不是true / false,则:

select

答案 4 :(得分:1)

意识到这是一个古老的帖子...这是我对此事的建议:

select在这里提供了很多帮助。

PS3="What's your choice? (^D to stop choosing): "
select mainmenuinput in updatesystem installsamba installvsftpd installwebmin configuresambaforactivedirectory quitprogram; do
    case "$mainmenuinput" in

    "updatesystem")
        echo "Update System..."
    ;;

    "installsamba")
        echo "Installing Samba..."
    ;;

    #echo And so forth...
    esac
done

echo Done

有关select的帮助,请咨询man bash并搜索“选择”。不提供任何输入将重复菜单。

select name [ in word ] ; do list ; done
       The  list  of words following in is expanded, generating a list of items.  The set of expanded words is printed on the standard error, each preceded by a number.  If the in word is omitted, the
       positional parameters are printed (see PARAMETERS below).  The PS3 prompt is then displayed and a line read from the standard input.  If the line consists of a number corresponding  to  one  of
       the  displayed  words, then the value of name is set to that word.  If the line is empty, the words and prompt are displayed again.  If EOF is read, the command completes.  Any other value read
       causes name to be set to null.  The line read is saved in the variable REPLY.  The list is executed after each selection until a break command is executed.  The exit status  of  select  is  the
       exit status of the last command executed in list, or zero if no commands were executed.

样本输出:

[rinzler ~] $ ./test.sh 
1) updatesystem                      4) installwebmin
2) installsamba                      5) configuresambaforactivedirectory
3) installvsftpd                     6) quitprogram
What's your choice? (^D to stop choosing): 1
Update System...
What's your choice? (^D to stop choosing): 2
Installing Samba...
What's your choice? (^D to stop choosing): 
1) updatesystem                      4) installwebmin
2) installsamba                      5) configuresambaforactivedirectory
3) installvsftpd                     6) quitprogram
What's your choice? (^D to stop choosing): 
Done