title: How to catch mysql Error in bash
body: <p>I have a bash script which exports a table from one db and imports it into another. Works like a charm.</p>
  
  <p>However since I want to let it run as a cronjob I would like it to send an email in case for whatever reason I get an error. But how do I find out if I have an error like e.g.:</p>
  
  <pre><code>ERROR 1045 (28000): Access denied for user 'importuser'@'192.168.xxx.xxx' (using password: YES)
  </code></pre>
  
  <p>Any Idea how to do that? Here is the critical passage:</p>
  
  <pre><code>mysql --user=$rep_user --password=$rep_password --host=$rep_host --database=$rep_name &lt; /tmp/${i}.sql
  </code></pre>
  
  <p>I already tried</p>
  
  <pre><code>    result=`mysql --user=$rep_user --password=$rep_password --host=$rep_host --database=$rep_name &lt; /tmp/${i}.sql`
      echo $result &gt;&gt; $EMAILMESSAGE
  </code></pre>
  
  <p>But it doesn't show up in my $EMAILMESSAGE</p>
  
  <p>Has anybody an idea how to achieve this?</p>