Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
if
if mysql -u root -p < test.sql ; then
mysql
You don't need the test command, i.e. [ here.
test
[
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
Add a comment
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.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
if mysql -u root -p < test.sql ; thenwould do it. You don't want the back ticks unless you want theifto test the output ofmysql. To check for success, assumingmysqlfollows convention, you want the return value ofmysql.