0

I've had 0 exposure to BASH scripting and this is something I would love to learn. I can't figure out how to run a conditional statement based on the output of ant debug on an Android build.

I would like to essentially say something like

if(`ant debug` == SUCCESS) {
  // EXECUTE THESE COMMANDS
} else {
  // EXECUTE THESE COMMANDS
}

How can I determine if the ant debug has passed or failed in shell script?

SOLUTION

Okay here is what I have:

ant clean

if ant debug; then
    echo "success"
else
    echo "failure"
fi

3 Answers 3

1

I'll give a quick summary for you.

In Bash, conditionals are based around the exit codes of programs. An exit code of 0 is accepted as true, while everything else is accepted as false.

For example, the true program always exits with an exit code of 0, which means that something like this is possible:

if true;
  echo "It is true"
fi

Most commands honor this system, but not every program does. The first thing to check is what exit code ant returns on success and failure. You can check the exit code of the previous command with $?.

Here is an example:

$ true
$ echo $?
0
$ false
$ echo $?
1

If ant does honor the exit code system properly, then something like the following should be possible:

if ant debug; then
  echo success
else
  echo failure
fi
Sign up to request clarification or add additional context in comments.

1 Comment

thanks - I was aware of the exit codes of 0 and 1 but not of the $?. Let me play around and see where I can get. I'll let you know.
0

I know nothing about Ant debugging, but there are two approaches to doing what you want to do in Bash. The first is to test output like you've shown:

if test $(ant debug) == 'SUCCESS'; then
    # do stuff
else
    # do other stuff
fi

You can make your shell script portable to other variants on the Bourne shell by using backticks instead of $(.....) like you wrote in your question, but that starts to become a hassle if your commands later involve nested quotes.

The second way, which is a little more robust, is to test the exit value of the commands instead of their output. This depends on Ant (or whatever) having exit codes that are documented and stable, but it means that if the output of the commands changes, your scripts will continue to work. For example, the POSIX standard says that if a programs succeeds in doing whatever it's supposed to do, it should exit() with a value of zero:

ant debug > /dev/null
ant_exit_code=$?
# other commands can safely go here now that $? is captured

if test $ant_exit_code -eq 0; then
    # do stuff
else
    # do other stuff
fi

And yes, Bourne shell really does end an if block with "fi". :-)

Comments

0

A quick man ant shows that ant debug invokes Ant with the debug task. Ant's tasks are kinda of user-defined XML scripts, and by default Ant searches a build.xml file in the current directory. You can generate the file with the android tools, however a template is kept in android-sdk/tools/ant and you can view it online (line 1005 defines the debug target).

So ant debug isn't really a command, and should not be put in a script toolchain. However, if you find your output to be stable and feel brave, you can always compare string. This is the definitve guide.

if [ `ant debug` = $SOMETHING ]; then
  echo Success
else
  echo Failure
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.