1

I'm writing a small restore script and before I run the actual script I want to check if all commands work..

I want to check if alias is added correctly on mysql..

So if the command # mysql returns an error I want to write an error message and then exit the script

#!/bin/sh

2 Answers 2

3
mysql -e 'exit' >/dev/null 2>&1 || { echo "mysql failed"; exit 1; }
# mysql works fine
# Your commands here

You could execute mysql and exit if it fails (either command is not found or alias not set correctly). In case, mysql works as expected, just exit out of it.

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

Comments

0

You can check the return status of a command with the `$?' variable, but there is a simpler way:

if !mysql; then
   echo "An error has occured"
fi

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.