本文介绍了bash脚本 - 检查mysql数据库中存在[执行行动基于结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从一个bash脚本中,以检查是否MySQL数据库存在。根据结果​​再进行其他操作,或终止脚本?

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?

推荐答案

例脚本(感谢比尔Karwin为 - 用户 - -password 注释):

Example script (Thanks to Bill Karwin for the --user and --password comment!):

#!/bin/bash
## --user=XXXXXX --password=XXXXXX *may* not be necessary if run as root or you have unsecured DBs but
##   using them makes this script a lot more portable.  Thanks @billkarwin
RESULT=`mysqlshow --user=XXXXXX --password=XXXXXX myDatabase| grep -v Wildcard | grep -o myDatabase`
if [ "$RESULT" == "myDatabase" ]; then
    echo YES
fi

这些命令是什么样子时,在提示符下运行:

These are what the commands look like when run at a prompt:

[root@host ~]# mysqlshow myDatabase
Wildcard: myDatabase
+------------------+
|    Databases     |
+------------------+
| myDatabase       |
+------------------+

如果不存在DB,输出将是这样的:

If no DB exists, the output will look like this:

[root@host ~]# mysqlshow myDatabase
Wildcard: myDatabase
+-----------+
| Databases |
+-----------+
+-----------+

然后,解析输出和你需要的基础上,如果它存在与否的!

Then, parse the output and do what you need to based on if it exists or not!

这篇关于bash脚本 - 检查mysql数据库中存在[执行行动基于结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:09