title: Linux Shell Script Catching exit code after executing mysql script shell
body: <p>I need to automate the mysql script execution in linux. If one sql script fails, it should not proceed further. It should come out of the script. I am using ${?} to test the exit status. But it's always returning the zero status though script has failed. I think it's returning zero because mysql command executed successfully even though script has failed with valid error. Please help me with the same. Also I want to know how to rollback if any script failed with valid error. Please check my code as below</p>
  
  <pre><code>#!/bin/bash
  # Load the script and env configuration
  . /home/base/scripts/conf/sql.conf
  if ls ${HOME_PATH}/sql_actual/*.sql &amp;&gt; /dev/null; then
     for file in ${HOME_PATH}/sql_actual/*.sql
     do
         echo ${file}
         s=${file##*/}
         var_log=${s%.sql}
         echo ${var_log}
       mysql -u ${DB_USER} -p${DB_PASSWORD} -h ${DB_HOST} --default-character-set=utf8 ${DB_SCHEMA} &lt; $file --silent --force -b 2&gt;${HOME_PATH}/sql-logs/${var_log}.log
           if [ ${?} -ne 0 ]; then
          echo "${var_log}.sql SQL Script Failed to execute"
          exit ${?}
          else
          echo -e "${var_log}.sql" SQL Script executed successfully
          fi
  done
  else
      echo "files do not exist"
  fi
  </code></pre>