Tuesday, January 10, 2012

How to Get the Shell Script to Stop on the First Error


1. Open your shell script in a text editor or your scripting application of choice.
2. Enter an 'If' statement in the shell script for commands that you want checked for errors. For instance, 'if cd $directory; then' would follow through with the next command lines if the 'cd' command completes successfully.
3. Enter a following 'Else' statement in your shell script that allows you to exit on an error. For example, you can enter:elseecho 'Error' 1>&2exit 1fiThis would echo the phrase 'Error' and exit with '1,' the exit code for exiting an application because of a failure in the application.