0

I'm not using set -e in my shell script, i'm handling each specific error with a function (event). In this particular case i'm trying to (insert a database and) catch any errors from mysql if it fails. Then send it straight into my event function so i can store it in a logfile.

mysql $schema < 'structure_dump.sql' || event "8" "$schema structure insert failed:"

I thought that output would be caught by doing this, but my error variable appears empty when this fails even though i'm still getting the mysql error message on my screen.

err=$(mysql $schema < 'structure_dump.sql') || event "8" "$schema structure insert failed: $err"

Is it possible to do this or am i thinking about it in the wrong way?

AlexH
  • 49
  • 1
  • 9

1 Answers1

2

MySQL should be reporting errors on stderr but you are only capturing stdout. Redirect stderr (descriptor 2) to stdout (descriptor 1)

I believe this should work for you:

# Push stderr into stdout in your output capture
err=$(mysql $schema < 'structure_dump.sql' 2>&1) || event "8" "$schema structure insert failed: $err"
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390