I’m writing a script which runs the following command
mysql -u root -e "show databases"
and this will display a list of databases.
If this table doesn’t contain a database by name “userdb“, it should do the following-
if [ ... ]; then
echo "error"
exit
fi
What do i write in the if [ ... ] condition?
You can check with
grepif the table name is listed.grep -qwill not print anything to the console but will set the exit status according to the result (the exit status will then be checked byif).About the regular expression: ‘
^‘ matches the beginning of the line and ‘$‘ matches the end of the line (to avoid a false positive for database names containinguserdb, e.g.userdb2)