Quelle est votre méthode préférée pour gérer les erreurs en BASH ? Le meilleur exemple de gestion des erreurs en BASH que j'ai trouvé sur le web a été écrit par William Shotts, Jr à l'adresse suivante http://www.linuxcommand.org .
William Shotts, Jr suggère d'utiliser la fonction suivante pour la gestion des erreurs dans BASH :
#!/bin/bash
# A slicker error handling routine
# I put a variable in my scripts named PROGNAME which
# holds the name of the program being run. You can get this
# value from the first item on the command line ($0).
# Reference: This was copied from <http://www.linuxcommand.org/wss0150.php>
PROGNAME=$(basename $0)
function error_exit
{
# ----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
# ----------------------------------------------------------------
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
# Example call of the error_exit function. Note the inclusion
# of the LINENO environment variable. It contains the current
# line number.
echo "Example of error with line number and message"
error_exit "$LINENO: An error has occurred."
Avez-vous une meilleure routine de gestion des erreurs que vous utilisez dans les scripts BASH ?