0
votes

I'm working on a simple bash script and one of the things it does is check whether a database already exists before moving on. It's simple enough code, but I'm getting a warning message whenever I try to run the script and I want to suppress that.

Here is the code:

if ! mysql -uroot -proot -e "use $NAME"; then
    echo YES
else
    echo NO
fi

So, as output, I get the following message when the if statement returns true:

ERROR 1049 (42000) at line 1: Unknown database 'database'
YES

How can I suppress that message? It doesn't stop the script from running, but I would prefer not to see it.

1

1 Answers

0
votes

It simply tells you that the DB with the name database (which is apparently passed as a value of the $NAME variable) doesn't exist. Use the correct DB name and there'll be no warning.

To simply mute the warning redirect all the output to /dev/null as usual:

if ! mysql -uroot -proot -e "use $NAME" 2>&1 >/dev/null; then
    echo YES
else
    echo NO
fi