Bash tips & tricks [ep. 6]: Check the exit code

This is the sixth epidose of a small series.

Description:

Every command in a script may fail due to external reasons. Bash programming is not functional programming! 🙂

After running a command, make sure that you check the exit code and either raise a warning or exit with an error, depending on how a failure can impact the execution of the script.

BAD:

The worst example is not to check the exit code at all:

Next one is better, but you may have a lot of additional code to type:

Again, Log_Close, eok, eerror, etc are functions defined using the previous Bash Tips & Tricks in this series.

GOOD:

Define once the check functions that you will use after every command:

 

Be Sociable, Share!
The following two tabs change content below.

Ludovico

Oracle ACE and Senior Consultant at Trivadis SA
Ludovico is an Oracle ACE Director, frequent speaker and community contributor, working as Senior Database Specialist for Trivadis, Switzerland.

2 thoughts on “Bash tips & tricks [ep. 6]: Check the exit code

  1. Pingback: Bash tips & tricks [ep. 6]: Check the exit code - Ludovico Caldara - Blogs - triBLOG

  2. Thanks for the tips, could You explain why there is a shift in function ?
    Regards
    GG

Leave a Reply

Your email address will not be published. Required fields are marked *