{"id":3236,"date":"2014-06-23T21:16:33","date_gmt":"2014-06-23T13:16:33","guid":{"rendered":"http:\/\/rmohan.com\/?p=3236"},"modified":"2014-06-23T21:51:23","modified_gmt":"2014-06-23T13:51:23","slug":"exit-status-code","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3236","title":{"rendered":"exit status code"},"content":{"rendered":"<h3 class=\"post-title entry-title\" style=\"font: 18px\/1.4em 'Trebuchet MS', Trebuchet, Verdana, sans-serif; margin: 0.25em 0px 0px; padding: 0px 0px 4px; text-align: left; color: #aadd99; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #000000; -webkit-text-stroke-width: 0px;\"><a style=\"color: #aadd99; font-weight: bold; text-decoration: none; display: block;\" href=\"http:\/\/linuxcommando.blogspot.sg\/2008\/03\/how-to-check-exit-status-code.html\">How to check the exit status code<\/a><\/h3>\n<div class=\"post-header-line-1\" style=\"font: 13px\/normal 'Trebuchet MS', Trebuchet, Verdana, sans-serif; text-align: left; color: #cccccc; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #000000; -webkit-text-stroke-width: 0px;\"><\/div>\n<div class=\"post-body entry-content\" style=\"font: 13px\/1.6em 'Trebuchet MS', Trebuchet, Verdana, sans-serif; margin: 0px 0px 0.75em; text-align: left; color: #cccccc; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: #000000; -webkit-text-stroke-width: 0px;\">\n<p>When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the exit code, you need to examine a special variable, &#8220;<span style=\"font-weight: bold;\">$?<\/span>&#8221;<\/p>\n<p>Say, you are searching for a string in a text file.<\/p>\n<pre>$ grep x1y2z3 somefile.txt\r\n$<\/pre>\n<p>The standard output of the command returns null, which is a pretty good indication that the string cannot be found in the file.<\/p>\n<p>But what if you embed the grep command in a script? How can you tell if the string is found or not?<\/p>\n<p>Checking the exit code will tell you. Let&#8217;s first try it out interactively.<\/p>\n<pre>$ grep x1y2z3 somefile.txt\r\n$ echo $?\r\n1<\/pre>\n<p>Note that in bash, the exit status is 0 if the command succeeded, and 1 if failed. For<span class=\"Apple-converted-space\">\u00a0<\/span><span style=\"font-style: italic;\">grep,<\/span><span class=\"Apple-converted-space\">\u00a0<\/span>0 means that the string was found, and 1 (or higher), otherwise.<\/p>\n<p>To check the exit status in a script, you may use the following pattern:<\/p>\n<pre>somecommand  argument1 argument2\r\nRETVAL=$?\r\n[ $RETVAL -eq 0 ] &amp;&amp; echo Success\r\n[ $RETVAL -ne 0 ] &amp;&amp; echo Failure<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>How to check the exit status code <\/p>\n<p>When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the exit code, you need to examine a special variable, &#8220;$?&#8221;<\/p>\n<p>Say, you are searching for a string in a text file.<\/p>\n<p> $ grep x1y2z3 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3236"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3236"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3236\/revisions"}],"predecessor-version":[{"id":3238,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3236\/revisions\/3238"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}