How to Trap Errors in Bash Scripts on Linux
fatmawati achmad zaenuri/Shutterstock.com
By default, a Bash script on Linux will report an error but keep running. We show you how to handle errors yourself so that you can decide what needs to happen next.
Error Handling in Scripts
Handling errors is part of programming. Even if you write flawless code, you can still run into error conditions. The environment on your computer changes over time, as you install and uninstall software, create directories, and perform upgrades and updates.
For example, a script that used to run without issue can run into difficulties if directory paths change, or permissions are changed on a file. The default action of the Bash shell is to print an error message and continue to execute the script. This is a dangerous default.
If the action that failed is critical to some other processing or action that happens later in your script, that critical action will not be successful. How disastrous that turns out to be, depends on what your script is trying to do.
A more robust scheme would detect errors and let the script work out if…
Read Full Article Source