如何抑制输出并检查命令是否成功? [英] How to suppress output and check whether or not a command is successful?
问题描述
我正在尝试编写一个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>
但是,可以说 This works correctly but doesn't suppress any output. Works correctly still but does not suppress the errors if the password is wrong. This does not work correctly. In this example, it always prints "Boo!" This suppresses all output correctly but only ever prints "Boo!" like before. Use You can then use
Using a redirection that involves PowerShell's error stream - either explicitly via 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 It follows from the above that However, arguably 这篇关于如何抑制输出并检查命令是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!$?
应该是可靠的,并且当前的(自v7.0起)有问题的行为在mysql -u root --password=mypass -e "show databases" > $null
mysql -u root --password=mypass -e "show databases" 2> $null
mysql -u root --password=mypass -e "show databases" > $null 2>&1
$LASTEXITCODE -eq 0
rather than $?
to reliably detect a nonzero exit code (typically signaling failure) reported by an external program.*> $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!"
}
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
.$LASTEXITCODE
variable.$?
can be $false
even if the exit code is 0
, so it isn't a reliable success indicator - unlike $LASTEXTICODE
.$?
should be reliable, and the current (as of v7.0), problematic behavior is discussed in this GitHub issue.