2

I have the following script:

if [ `mysql -u root -p < test.sql` ]; then
    echo "success"
else
    echo "some error"
fi

How can I check in the if if the command went well or there was some sql error?

1
  • I think just if mysql -u root -p < test.sql ; then would do it. You don't want the back ticks unless you want the if to test the output of mysql. To check for success, assuming mysql follows convention, you want the return value of mysql. Commented May 20, 2014 at 17:28

2 Answers 2

4

You don't need the test command, i.e. [ here.

Assuming that mysql produces a non-zero exit code in case of failure and 0 on success, then you can say:

if mysql -u root -p < test.sql; then
    echo "success"
else
    echo "some error"
fi
Sign up to request clarification or add additional context in comments.

Comments

0

Another way this is typically done is with the "&&" and "||" separators. For example:

mysql -u root -p < test.sql && echo "success" || echo "some error"

If the mysql command exits with status 0, then "success" will be printed. If it doesn't, "some error" will be printed.

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.