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

UNIX shell scripting makes it easy for you to accomplish repetitive tasks on your machine. Shell scripts are also versatile, as they allow you to improve and tailor your computer to your use. While you may hastily throw shell scripts together to accomplish a task, you should also include error-checking statements that allow your program to exit upon finding the first error.

Instructions

    • 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:

      else

      echo "Error" 1>&2

      exit 1

      fi

      This would echo the phrase "Error" and exit with "1," the exit code for exiting an application because of a failure in the application.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured