title: Catch Mysql Error in bash
body: <p>I have a bash script that queries mysql and I would like to catch the error and echo it .  Here is my code</p>
  
  <pre><code> mysql --silent -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${DATABASE} -BNe "$sql_str" | while read -r line
  do
      query="$(echo "$line" | cut -f1)"
      database_out="$(echo "$line" | cut -f2)"
      outfile="$(echo "$line" | cut -f3)"
      directory_out="$(echo "$line" | cut -f4)"
      type="$(echo "$line" | cut -f5)"
      host="$(echo "$line" | cut -f6)"
      username="$(echo "$line" | cut -f7)"
      directory_scp="$(echo "$line" | cut -f8)"
  
      #switch over format types
      case "$type" in 
          csv)
              file_out=$directory_out$outfile"."`date +%Y%m%d`.csv
              mysql -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${database_out} -e "$query" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' &gt; $file_out
              ;;
          xml)
              file_out=$directory_out$outfile`date +%Y%m%d`.xml
              mysql -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${database_out} -e "$query" -X &gt; $file_out
              ;;
          *)
          echo "["$(timestamp)"] : {ERROR} Invalid output format.  Currently only csv and xml output supported."
          ;;
      esac
      str=$file_out" "$username@$host:$directory_scp  
      echo "["$(timestamp)"] : {NOTIFICATION} Attempting to scp "$outfile" to "$username@$host:$directory_scp" - [Started]"
      scp $str
      echo "["$(timestamp)"] : {NOTIFICATION} scp "$outfile" to "$username@$host:$directory_scp" - [Complete]"
  
  done 
  </code></pre>
  
  <p>Any ideas.  I would like to catch error and format the error output.</p>