title: Stopping the shell script if any of the query gets failed
body: <p>Below is my shell script from which I am trying to invoke few hive SQL queries which is working fine.</p>
  
  <pre><code>#!/bin/bash
  
  DATE_YEST_FORMAT1=`perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time()- 3600*504);'`
  echo $DATE_YEST_FORMAT1
  
  hive -e "
          SELECT t1 [0] AS buyer_id
              ,t1 [1] AS item_id
              ,created_time
          FROM (
              SELECT split(ckey, '\\\\|') AS t1
                  ,created_time
              FROM (
                  SELECT CONCAT (
                          buyer_id
                          ,'|'
                          ,item_id
                          ) AS ckey
                      ,created_time
                  FROM dw_checkout_trans
                  WHERE to_date(from_unixtime(cast(UNIX_TIMESTAMP(created_time) AS BIGINT))) = '$DATE_YEST_FORMAT1' distribute BY ckey sort BY ckey
                      ,created_time DESC
                  ) a
              WHERE rank(ckey) &lt; 1
              ) X
          ORDER BY buyer_id
              ,created_time DESC;"
  
  sleep 120
  
  QUERY1=`hive -e "
  set mapred.job.queue.name=hdmi-technology;
  SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';"`
  </code></pre>
  
  <p><strong>Problem Statement:-</strong></p>
  
  <p>If you see my first <code>hive -e</code> block after the <code>echo $DATE_YEST_FORMAT1</code>. Sometimes that query gets failed due to certain reasons. So currently what happens is that, if the <code>first Hive SQL query</code> gets failed, then it goes to <code>second Hive SQL query</code> after sleeping for <code>120 seconds</code>. And that is the thing I don't want. So Is there any way if the <code>first query</code> gets failed dues to any reasons, it should get <code>stopped automatically</code> at that point. And it should start running automatically from the starting again after few minutes(<strong>should be configurable</strong>)</p>
  
  <p><strong>Update:-</strong></p>
  
  <p>As suggested by <code>Stephen</code>.</p>
  
  <p>I tried something like this-</p>
  
  <pre><code>#!/bin/bash
  
  hive -e " blaah blaah;"
  
  RET_VAL=$?
  echo $RET_VAL
  if [ $RET_VAL -ne 0]; then
  echo "HiveQL failed due to certain reason" | mailx -s "LIP Query Failed" -r rj@host.com rj@host.com
  exit(1)
  </code></pre>
  
  <p>I got something like this below as an error and I didn't got any email too. Anything wrong with my syntax and approach?</p>
  
  <pre><code>syntax error at line 152: `exit' unexpected
  </code></pre>
  
  <p><strong>Note:-</strong></p>
  
  <p>Zero is success here if the Hive Query is executed successfully.</p>
  
  <p><strong>Another Update after putting the space:-</strong>
  After making changes like below</p>
  
  <pre><code>#!/bin/bash
  
  hive -e " blaah blaah;"
  
  RET_VAL=$?
  echo $RET_VAL
  if [ $RET_VAL -ne 0 ]; then
  echo "HiveQL failed due to certain reason for LIP" | mailx -s "LIP Query Failed" -r rj@host.com rj@host.com
  fi
  exit
  
  hive -e 'Another SQL Query;'
  </code></pre>
  
  <p>I got something like below-</p>
  
  <pre><code>RET_VAL=0
  + echo 0
  0
  + [ 0 -ne 0 ]
  + exit
  </code></pre>
  
  <p>Status code was <code>zero</code> as my first query was successful but my program exited after that and it didn't went to execute my second query? Why? I am missing something here for sure again.</p>