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:

 

The following two tabs change content below.

Ludovico

Principal Product Manager at Oracle
Ludovico is a member of the Oracle Database High Availability (HA), Scalability & Maximum Availability Architecture (MAA) Product Management team in Oracle. He focuses on Fleet Patching and Provisioning (FPP), MAA-optimized database lifecycle management, Cloud MAA and Online Reorganization.

3 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

  3. Wow, I realize now that I have never rplied to your comment… Sorry Grzegorz.
    I use shift get the rid of the first parameter, so that after that I can refer to $@ without caring about hte first parameter that was the exit code.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.