Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to check if a Mysql user exists in a bash script

Tags:

bash

mysql

I am trying to check if a MYSQL user exists. This is as far as I have got. I fall down on capturing the answer from the output.

 #!/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

this is the output I am getting

+-----------------------------------------------------+
| EXISTS(SELECT 1 FROM mysql.user WHERE user = 'bob') |
+-----------------------------------------------------+
|                                                   1 |
+-----------------------------------------------------+

Can any one help please

like image 385
Gary Avatar asked Jan 23 '15 14:01

Gary


1 Answers

Thanks very much for your help. Here is the final result working.

It needs the -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
like image 60
Gary Avatar answered Oct 13 '22 11:10

Gary