Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

mysql - How to suppress output and check whether or not a command is successful?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update:

  • The if ($LASTEXITCODE -eq 0) ... approach will continue to work robustly with external programs.
  • However, if and when the pre-v7.2 experimental feature named PSNotApplyErrorActionToStderr becomes an official feature, if ($?) ... will work robustly too - see this answer for more information.

Use $LASTEXITCODE -eq 0 rather than $? to reliably detect a nonzero exit code (typically signaling failure) reported by an external program.

You can then use *> $null to categorically suppress all output without having to worry about the impact of that redirection on $?:

mysql -u root --password=mypass -e "show databases" *>$null
if ($LASTEXITCODE -eq 0) {
  "Hooray!"
} else {
  "Boo!"
}

Using a redirection that involves PowerShell's error stream - either explicitly via 2> or implicitly via *> - means that if any data is received via that stream - which in the case of calling an external program means any output from stderr - PowerShell sets $? to $false.

However, in the realm of external console / terminal programs, stderr isn't just used to output error information, but any information that isn't data, such as status information. Therefore, you cannot infer failure from the presence of stderr output.

External console / terminal programs communicate their success status solely via their exit code, which PowerShell reflects in the automatic $LASTEXITCODE variable.

It follows from the above that $? can be $false even if the exit code is 0, so it isn't a reliable success indicator - unlike $LASTEXITCODE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...