Is it possible from within a bash script to check if a mysql database exists. Depending on the result then perform another action or terminate the script?
I couldn't get the accepted answer work for me (the grep in the quotes didn't work), so here is my version:
RESULT=`mysql -u $USER -p$PASSWORD --skip-column-names -e "SHOW DATABASES LIKE 'myDatabase'"`
if [ "$RESULT" == "myDatabase" ]; then
echo "Database exist"
else
echo "Database does not exist"
fi
I used the option --skip-column-names to remove the column names from the result.