0

I'm writing a bash script and I want to redirect MySQL errors to a log file.

I had success with the below (ERROR 1045 (28000): Access denied for user... is being appended to the log file)

mysql -u user -pWrongpass -sN -e "query to update db;" 2>&1 | tee -a log

however, I'm not having success with this one. The error is displayed when I run the script but I don't see it in the log file.

result=$(mysql -u user -pWrongpass -sN "query to select from db;") 2>&1 | tee -a log

What's the correct syntax to put the result of a query into a variable while printing any potential error to the log file?

Thanks in advance and let me know if I'm not clear :)

1 Answer 1

2

You have to put the entire pipeline inside the command substitution.

result=$(mysql -u user -pWrongpass -sN "query to select from db;" 2>&1 |
         tee -a log)

Since the output of mysql is piped to tee, it is the output of tee that you need to capture in result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.