mysqladmin -uroot create foo
returns an exit status of 1 if foo
exists, and 0 otherwise, but of course it will also create the database if it doesn't already exist. Is there some easy way to simply check whether a database exists?
Mysql – From a shell script, how can I check whether a MySQL database exists
bashdatabaseMySQLshell
Related Topic
- Mysql – From a shell script, how can I check whether a table in MySQL database exists or not
- Bash – Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediately
- Linux – bash : “set -e” and check if a user exists make script exit
- Linux – How to return a specific exit code on the Linux command line
- Why does this Ansible task report a failure
Best Answer
I realize this was answered a long time ago, but it seems much cleaner to me to do this:
If the database exists, this will produce no output and exit with returncode == 0.
If the database does not exist, this will produce an error message on stderr and exit with returncode == 1. So you'd do something like this:
This operates nicely with shell scripts, doesn't require any processing of the output, and doesn't rely on having local filesystem access.