Mohammad Sheykholeslam
Mohammad Sheykholeslam

Reputation: 1437

Error detection in shell script commands such as "make"

I want to write a script to extract, configure, make and make install a source(xyz.tar.bz2). But I want to stop this script if there is an error in one of these commands. Is there any signal or method specially for make command to detect errors during execution to stop script?

Upvotes: 1

Views: 1612

Answers (2)

Mohammad Sheykholeslam
Mohammad Sheykholeslam

Reputation: 1437

That's true; the more simple way is below...

tar xvf foo.tar.bz2 && make && make install

Upvotes: 0

derobert
derobert

Reputation: 51197

Make normally aborts if any command it runs returns a non-zero exit code (including being killed by a signal). So you just need to make sure your script exists (with an error) if a command fails. Also, make itself then exists with a non-zero status (so you can detect make or make install failing). Note that make can be told to ignore errors on a per-command basis (by prefixing the command with a -), but this isn't the default.

You can do this in two main ways:

  1. set -e (though beware that set -e has a lot of problems)
  2. Test each command for failure (e.g., tar xzf foo.tar.gz || exit 1)

Alternatively, you can use make the way it is intended, and write make rules to do all these steps.

Upvotes: 2

Related Questions