{"id":1340,"date":"2016-03-23T15:57:59","date_gmt":"2016-03-23T13:57:59","guid":{"rendered":"http:\/\/www.ludovicocaldara.net\/dba\/?p=1340"},"modified":"2016-03-24T11:17:04","modified_gmt":"2016-03-24T09:17:04","slug":"bash-tips-6-check-the-exit-code","status":"publish","type":"post","link":"https:\/\/www.ludovicocaldara.net\/dba\/bash-tips-6-check-the-exit-code\/","title":{"rendered":"Bash tips &#038; tricks [ep. 6]: Check the exit code"},"content":{"rendered":"<p>This is the sixth epidose of a <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/bash-tips-1-personal-accounts-permissions\/\">small series<\/a>.<\/p>\n<p><strong>Description:<\/strong><\/p>\n<p>Every command in a script may fail due to external reasons. Bash programming is not functional programming! \ud83d\ude42<\/p>\n<p>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.<\/p>\n<p><strong>BAD:<\/strong><\/p>\n<p>The worst example is not to check the exit code at all:<\/p>\n<pre class=\"lang:sh decode:true \">#!\/bin\/bash -l\r\n\r\nrecover -a -f -c ${NWCLIENT} -d ${DEST_FILE_PATH} $BASEBCK_FILENAME\r\n# what if recover fails?\r\n\r\ndo_something_with_recovered_files<\/pre>\n<p>Next one is better, but you may have a lot of additional code to type:<\/p>\n<pre class=\"lang:sh decode:true \">#!\/bin\/bash -l\r\n\r\nrecover -a -f -c ${NWCLIENT} -d ${DEST_FILE_PATH} $BASEBCK_FILENAME\r\n\r\n#---------\r\n# the following piece of code is frequently copied&amp;pasted \r\nERR=$?\r\nif [ $ERR -ne 0 ] ; then\r\n    # I've got an error with the recovery\r\n    eerror \"The recovery failed with exit code $ERR\"\r\n    Log_Close\r\n    exit $ERR\r\nelse\r\n    eok \"The recovery succeeded.\"\r\nfi\r\n#---------\r\n\r\ndo_something_with_recovered_files<\/pre>\n<p>Again, Log_Close, eok, eerror, etc are functions defined using the previous Bash Tips &amp; Tricks in this series.<\/p>\n<p><strong>GOOD:<\/strong><\/p>\n<p>Define once the check functions that you will use after every command:<\/p>\n<pre class=\"lang:sh decode:true  \"># F_check_warn will eventually raise a warning but let the script continue\r\nfunction F_check_warn() {\r\n        EXITCODE=$1\r\n        shift\r\n        if [ $EXITCODE -eq 0 ] ; then\r\n                eok $@ succeded with exit code $EXITCODE\r\n        else\r\n                ewarn $@ failed with exit code $EXITCODE. The script will continue.\r\n        fi\r\n        # return the same code so other checks can follow this one inside the script\r\n        return $EXITCODE\r\n}\r\n\r\n# F_check_warn will eventually raise an error and exit\r\nfunction F_check_exit() {\r\n        EXITCODE=$1\r\n        shift\r\n        if [ $EXITCODE -eq 0 ] ; then\r\n                eok $@ succeded with exit code $EXITCODE\r\n        else\r\n                eerror $@ failed with exit code $EXITCODE. The script will exit.\r\n                Log_Close\r\n                exit $EXITCODE\r\n        fi\r\n}\r\n\r\nCMD=\"recover -a -f -c ${NWCLIENT} -d ${DEST_FILE_PATH} $BASEBCK_FILENAME\"\r\nenotify \"Recover command: $CMD\"\r\neval $CMD\r\nF_check_exit $? \"Recovery from networker\"\r\n\r\ndo_something_with_the_recovered_files\r\nF_check_warn $? \"Non-blocking operation with recovered files\"<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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! \ud83d\ude42 After running a command, make sure that you check the exit code &hellip; <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/bash-tips-6-check-the-exit-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,132],"tags":[],"class_list":["post-1340","post","type-post","status-publish","format-standard","hentry","category-linux","category-triblog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/comments?post=1340"}],"version-history":[{"count":4,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1340\/revisions"}],"predecessor-version":[{"id":1354,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1340\/revisions\/1354"}],"wp:attachment":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/media?parent=1340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/categories?post=1340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/tags?post=1340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}