开发者

What is the proper way to test a Bash function's return value?

I would like to test a Bash function's return value in an if statement like this:

if [[ func arg ]] ; then …

But I get error 开发者_StackOverflowmessages like: conditional binary operator expected.

What is the right way to do this?

Is it the following?

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


If it was the exit code and not the result, you could just use

if func arg; then ...

If you cannot make the function return a proper exit code (with return N), and you have to use string results, use Alex Gitelman's answer.

$ help if:

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

Execute commands based on conditional.

The if COMMANDS list is executed. If its exit status is zero, then the then COMMANDS list is executed. Otherwise, each elif COMMANDS list is executed in turn, and if its exit status is zero, the corresponding then COMMANDS list is executed and the if command completes. Otherwise, the else COMMANDS list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true.

Exit Status:
Returns the status of the last command executed.


If you need to test two conditions, one being the exit status of function/command and the other e.g. value of variable, use this:

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


This error seems to be produced if the function returns more than one word.

For example, 1 2.

Just quote it:

"$(func arg)"

Sample:

$ 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

And if you compare 0 vs non 0, just use

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


On a related note, if the function returns a variety of exit codes instead of true/false, then:

func args; ec=$?      # call function and grab the exit code
                      # it is better to have them on the same line so that a future addition of a command
                      # before the case statement wouldn't break the logic
case $ec in
  value1) # commands
          ;;
  value2) # commands
          ;;
  *)      # commands
          ;;
esac


select provides a lot of help here.

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

For help with select, consult man bash and search for 'select'. Providing no input will repeat the menu.

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.

Sample Output:

[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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜