如何确定BASH中的mysql中是否有用户?

时间:2020-12-28 15:44:57

How to determine if there is a user in mysql in BASH ?

如何确定BASH中的mysql中是否有用户?

if [ "$NAME_USER" == ???? ]; then
  echo "User exists in MySQL"
else
  echo "User no exists in MySQL"
fi

2 个解决方案

#1


3  

while read User; do
    if [[ "$NAME_USER" == "$User" ]]; then
        echo "$User exists in MySQL"
        break
    fi
done < <(mysql -B -N -e 'use mysql; SELECT `user` FROM `user`;')

if [[ "$NAME_USER" != "$User" ]]; then
    echo "$NAME_USER does not exists in MySQL"
fi

#2


0  

You can enclose a script to make a mysql query and then parse the output.

您可以包含一个脚本来进行mysql查询,然后解析输出。

#1


3  

while read User; do
    if [[ "$NAME_USER" == "$User" ]]; then
        echo "$User exists in MySQL"
        break
    fi
done < <(mysql -B -N -e 'use mysql; SELECT `user` FROM `user`;')

if [[ "$NAME_USER" != "$User" ]]; then
    echo "$NAME_USER does not exists in MySQL"
fi

#2


0  

You can enclose a script to make a mysql query and then parse the output.

您可以包含一个脚本来进行mysql查询,然后解析输出。