我正在尝试检查MYSQL用户是否存在。这是我所拥有的。我沮丧地从输出中捕获答案。
#!/bin/bash
echo -e "What is the MYSQL username called"
read DBUSER
if [ -z "$DBUSER" ]
then
exit
mysql -uUSER -pPASS -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')";
if
yes
do this
else
do this
这是我得到的输出
+-----------------------------------------------------+
| EXISTS(SELECT 1 FROM mysql.user WHERE user = 'bob') |
+-----------------------------------------------------+
| 1 |
+-----------------------------------------------------+
任何人都可以帮忙吗
答案 0 :(得分:6)
非常感谢你的帮助。这是最终的结果。
需要-sse
RESULT_VARIABLE="$(mysql -uUSER -pPASS -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')")"
if [ "$RESULT_VARIABLE" = 1 ]; then
echo "TRUE"
else
echo "FALSE"
fi
答案 1 :(得分:3)
将结果分配给变量可以这样做:
RESULT_VARIABLE="$(mysql -uUSER -pPASS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')")"
你也可以在MySQL中使用别名,btw。
SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER') AS does_it_exist