I am trying to write a powershell script that tests if a MySQL login is successful by using $?
to check if an error occurs.
I also want to suppress all output - successful or not successful - from the command.
These are the things I've tried:
mysql -u root --password=mypass -e "show databases"
If ( $? ) {
echo "Hooray!"
} Else {
echo "Boo!"
}
This works correctly but doesn't suppress any output.
mysql -u root --password=mypass -e "show databases" > $null
Works correctly still but does not suppress the errors if the password is wrong.
mysql -u root --password=mypass -e "show databases" 2> $null
This does not work correctly. In this example, it always prints "Boo!"
mysql -u root --password=mypass -e "show databases" > $null 2>&1
This suppresses all output correctly but only ever prints "Boo!" like before.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…