title: Catching exit code after executing mysql query
body: <p>I'm working on a bash script that will query mysql. I would like to add some error checking. Let say if the following query for some reason fails I want to catch the exit error and exit the script. For example this is part of my script.</p>
  
  <pre><code>QUERY="SELECT DISTINCT `TABLE_SCHEMA`, `TABLE_NAME`
  FROM `information_schema`.`TABLES`
  WHERE  table_schema NOT IN ( 'mysql', 'information_schema', 'performance_schema' )"    
  
  mysql -u user -pPASSWD --batch -N -e "$QUERY" | while read DATABASE TABLE;
  do
    ...
    ...
    ...
  done
  </code></pre>
  
  <p>How could i catch the exit code after the scripts run the "$QUERY". I was thinking something like this. But it doesn't seem to work.</p>
  
  <pre><code>mysql -u user -pPASSWD --batch -N -e "$QUERY" echo $? | while read DATABASE TABLE;
  </code></pre>
  
  <p>Any ideas</p>