【发布时间】:2019-11-18 15:24:53
【问题描述】:
在 bash 中,我可以这样做:
if this_command >/dev/null 2>&1; then
ANSWER="this_command"
elif that_command >/dev/null 2>&1; then
ANSWER="that_command"
else
ANSWER="neither command"
fi
但在 Powershell 中,我必须这样做:
this_command >/dev/null 2>&1
if ($?) {
ANSWER="this_command"
} else {
that_command >/dev/null 2>&1
if ($?) {
ANSWER="that_command"
} else {
ANSWER="neither command"
}
}
或与($LASTEXITCODE -eq 0) 类似的东西。如何使 Powershell 看起来像 bash?我不是 Powershell 专家,但我不敢相信它没有提供某种方法来运行命令并以可以在 if-elseif-else 语句中使用的方式在单个语句中检查其返回代码。对于必须以这种方式测试的每个外部命令,该语句将越来越难以阅读。
【问题讨论】: