Skip to content

Instantly share code, notes, and snippets.

@rschroll
Created June 23, 2017 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rschroll/70dc9ed3f7ef935982a9bf2f799940b0 to your computer and use it in GitHub Desktop.
Save rschroll/70dc9ed3f7ef935982a9bf2f799940b0 to your computer and use it in GitHub Desktop.
Makefiles and failing commands

How does a Makefile react to failing commands?

There are three scripts:

  • good.sh exits successfully
  • maybe.sh contains a failing command, but its last command succeeds, so the whole script exits successfully
  • bad.sh adds set -e to maybe.sh, causing the script to fail after the first failing command

The Makefile targets of good, bad, and ugly depend on those scripts. Note that good and ugly succeed while bad fails.

The targets of good-dep, bad-dep, and ugly-dep depend on the above targets. Again, good-dep and ugly-dep succeed, and the additional rules run. bad-dep stops after running bad, since it failed.

The good-bad and bad-good targets run the good.sh and bad.sh scripts in opposite orders. Both stop after running the bad script, and both fail.

The good-bad-dep and bad-good-dep depend on both good and bad, in opposite orders. The dependent commands are run in the order listed, and execution ends after running bad. Both fail, and neither runs its own code.

#!/bin/bash
set -e
echo "Bad"
false
true
#!/bin/bash
set -e
echo "Good"
true
.PHONY: good bad ugly good-bad bad-good good-dep bad-dep ugly-dep good-bad-dep bad-good-dep
good:
./good.sh
bad:
./bad.sh
ugly:
./ugly.sh
good-bad:
./good.sh
./bad.sh
bad-good:
./bad.sh
./good.sh
good-dep: good
echo "Good parent"
bad-dep: bad
echo "Bad parent"
ugly-dep: ugly
echo "Ugly parent"
good-bad-dep: good bad
echo "Good bad"
bad-good-dep: bad good
echo "Bad good"
#!/bin/bash
echo "Ugly"
false
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment