Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I build my application using Maven. In my script after I build the app I startup application server. The problem is that if build is not succesfull the application server is started any way.

If I execute (with bad version that does not compile)

mvn compile
echo "$?"
  if [ $? != 0 ]; then
    echo "is not 0"
  else
    echo "is 0"
  fi

the output is

1
is 0

How to make the if return is not 0 ?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

First of all you must save the content of $? as soon as possible, because each command run overwrite its content. In your case the $? in the if statement is the exit status of the previous echo, not of mvn.

Next, for arithmetic comparison in bash (and sh) you have to use -ne for not equal.

So I suggest:

mvn compile
status="$?"
echo "$status"
  if [ $status -ne 0 ]; then
    echo "is not 0"
  else
    echo "is 0"
  fi

But it could be done more elegantly with

if ! mvn compile; then
    echo "is not 0"
else
    echo "is 0"
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.