Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MySQL output in bash [duplicate]

In my bash file I have the following

result=$(mysql --login-path=remote_user --database=mydatabase < "import.sql");
echo $result;

I am trying to capture the output so that if it errors I can email myself

When I run the command I get

ERROR 1062 (23000) at line 1:....

But $result is always empty so how can I assign / capture the error so that I can then use this in the email

Note: I have enforced the error so that I can test

like image 470
goose84 Avatar asked Apr 29 '26 06:04

goose84


1 Answers

Got it to work for anyone else who has this issue

        resultfile="result.txt";
        result=$(mysql --login-path=remote_user --database=mydatabase < "import.sql"  2>&1 ) || echo $result > $resultfile
        if [ -f $resultfile ]; then
            echo "DO SOMETHINNG"
        fi
like image 87
goose84 Avatar answered Apr 30 '26 20:04

goose84