Posted on Wednesday 26th February 2020
As part of a deployment script, needed a way to detect if a database already exisit, and if not, create and seed the database.
This consists of just running the SHOW DATABASEs command and piping through to grep to search for the database name.
mysql -uUSER -pPASSWORD-e "SHOW DATABASES" | grep DATABASENAME
This uses the mysql show command to check for the database and echo a string if found
mysqlshow -uroot -psecret "DATABASENAME" > /dev/null 2>&1 && echo "Database exists."