Wednesday 20 February 2013

#!/bin/sh and its options -x and -e

Let’s look at an example.
#!/bin/sh
make
echo “Build exit code was $?”
What is the exit code of the script above?
It will be zero, always. Why?
Because the exit code of a script is the exit code of the last command and the echo command will succeed with very high probability.


The shell has a convenient option -e, which causes shell to stop running a script immediately when any command exits with non-zero exit code. This makes it easy to have Jenkins know when your script fails.

By the way, there’s also one more useful shell option you might want to know about, -x, which makes shell print every command it executes.

#!/bin/sh -xe   will exit if any command exits with non-zero exit code AND will print every command that is executed

No comments:

Post a Comment