Q:

Bash check if mysql command failed

I have the following bash script:
MYSQL="which mysql"
SQL="CREATE DATABASE IF NOT EXISTS TEST;"

read -p -s "Enter mySQL password:" DBPASS

$MYSQL -root -p$DBPASS -e "$SQL"
if [ "$?" -eq 0 ]; 
then
   echo "Failed."
else
   echo "Done."
fi

Even if I type wrong password the script says "Done.", as it can´t detect the mysql command error... 
I need to be able to correctly show the "Error." message when someone types a wrong password.
Help appreciated.

A:

$? == 0 means the command run successfully. 
try this:
MYSQL="which mysql"
SQL="CREATE DATABASE IF NOT EXISTS TEST;"

read -p -s "Enter mySQL password:" DBPASS

$MYSQL -root -p$DBPASS -e "$SQL"
if [ "$?" -eq 0 ]; 
then
   echo "Done."
else
   echo "Failed."
fi

