How can I check if a user exists?
Im doing an installer for a mysql database, and I need to check if a user exits, if not create user, if yes delete user and create it a
When in need to check if a user exists without deleting it (for instance when just skipping some instructions instead of executing them in any case), one can use this (where $USER
is the user to check):
if [ $(echo "SELECT COUNT(*) FROM mysql.user WHERE user = '$USER'" | mysql | tail -n1) -gt 0 ]
then
echo "User exists"
else
echo "User doesn't exist"
fi
NB:
mysql
command requires extra args and/or configuration for authentication)tail -n1
is used for removing the query result header