示例脚本(感谢Bill Karwin的–user和–password注释!):
- #!/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
这些是在提示符下运行时命令的样子:
- [root@host ~]# MysqLshow myDatabase
- Wildcard: myDatabase
- +------------------+
- | Databases |
- +------------------+
- | myDatabase |
- +------------------+
如果不存在DB,则输出如下所示:
- [root@host ~]# MysqLshow myDatabase
- Wildcard: myDatabase
- +-----------+
- | Databases |
- +-----------+
- +-----------+
然后,解析输出并根据它是否存在执行您需要的操作!