如何抑制输出并检查命令是否成功? [英] How to suppress output and check whether or not a command is successful?

查看:102
本文介绍了如何抑制输出并检查命令是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Powershell脚本,该脚本通过使用$?检查是否发生错误来测试MySQL登录是否成功.

我还想禁止命令显示所有输出-成功还是不成功.

这些是我尝试过的事情:

mysql -u root --password=mypass -e "show databases"
If ( $? ) {
  echo "Hooray!"
} Else {
  echo "Boo!"
}

这可以正常工作,但不会抑制任何输出.

mysql -u root --password=mypass -e "show databases" > $null

仍然可以正常工作,但是如果密码错误,则不能抑制错误.

mysql -u root --password=mypass -e "show databases" 2> $null

这不能正常工作.在此示例中,它始终打印"Boo!"

mysql -u root --password=mypass -e "show databases" > $null 2>&1

这将正确地抑制所有输出,但仅打印嘘!".像以前一样.

解决方案

使用$LASTEXITCODE -eq 0而不是$?来可靠地检测外部程序报告的非零退出代码(通常表示失败)

然后,您可以使用*> $null绝对抑制所有输出,而不必担心该重定向对$?的影响:

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


使用涉及PowerShell错误流的重定向-通过2>显式地或通过*>隐式地-意味着如果通过该流接收到任何数据-在调用外部程序的情况下意味着从 stderr -PowerShell将$?设置为$false.

但是,在外部控制台/终端程序领域,stderr不仅用于输出 error 信息,而且还用于输出不是 data 的信息.作为状态信息.因此,您不能从stderr输出的存在来推断故障.

外部控制台/终端程序仅通过其退出代码传达其成功状态,PowerShell会在自动$LASTEXITCODE变量中反映出该成功状态.

从上面可以看出,即使退出代码为0 $?也可以为$false,因此它不是可靠的成功指示符-与$LASTEXTICODE 不同./p>

但是,可以说$? 应该是可靠的,并且当前的(自v7.0起)有问题的行为在

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.

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 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 $LASTEXTICODE.

However, arguably $? should be reliable, and the current (as of v7.0), problematic behavior is discussed in this GitHub issue.

这篇关于如何抑制输出并检查命令是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆