相关文章推荐
旅途中的茄子  ·  更新SQL子查询 - ·  2 月前    · 
爱运动的核桃  ·  JSONObject ...·  1 年前    · 
焦虑的皮带  ·  .NET ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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" ]
    mysql -uUSER -pPASS -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')";
    do this
    do this

this is the output I am getting

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

Can any one help please

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"
  echo "FALSE"
                rather obvious, but just to be affirmative... this worked for me with shell_exec  --> $is_mysql_user = shell_exec('mysql --defaults-extra-file=MYPATHTOSTOREDCREDENTIALS -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = \'' . $user_name . '\')"');
– aequalsb
                Nov 12, 2018 at 18:43

Assigning the result to a variable can be done like this:

RESULT_VARIABLE="$(mysql -uUSER -pPASS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')")"

And you can also alias a column in MySQL, btw.

SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER') AS does_it_exist
                Thanks very much. I am then trying to pick up the result. This gives me an error  if [ $RESULT_VARIABLE = "1" ]; then echo "true" else   echo "try again" fi
– Gary
                Jan 23, 2015 at 14:48
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.