Bash 的 && 等价于 PowerShell 的是什么?和||运营商?
在 Bash 中,我可以轻松地执行类似操作
command1 && command2 || command3
,即运行 command1,如果 command1 成功运行 command2,如果 command1 无法运行 command3。
PowerShell 中的等效项是什么?
In Bash I can easily do something like
command1 && command2 || command3
which means to run command1 and if command1 succeeds to run command2 and if command1 fails to run command3.
What's the equivalent in PowerShell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新:
&&
和||
终于来到 PowerShell (Core),即 v7+ 中,称为 管道链运算符;有关详细信息,请参阅此答案。在这个问题首次提出多年后,让我总结一下Windows PowerShell 的行为,其最新且最终版本是 v5.1:
cmd
的&&
和||
控制运算符没有 PowerShell 等效项,并且由于您无法在 PowerShell 中定义自定义运算符,因此没有好的解决方法:使用单独的命令(在单独的行上或用
;
分隔),并通过 自动$?
变量,例如:外部程序的更好替代方案,使用自动
$LASTEXITCODE
变量;这是更好的选择,因为如果2>
在 Windows PowerShell 中可能会产生漏报(在 PowerShell (Core) 7.2+ 中不再出现) > 涉及重定向 - 请参阅此答案:请参阅下文,了解为什么 PowerShell 的
-and
和-or
通常不是解决方案。[自从在 PowerShell (Core) 7+ 中实现]有讨论添加它们 不久前,但它似乎从未成为列表的顶部。
&&
和||
目前保留供将来在 PowerShell 中使用,因此希望可以实现与 Bash 中相同的语法。< br>(从 PSv5.1 开始,尝试类似
1 && 1
的操作会产生错误消息标记“&&”在此版本中不是有效的语句分隔符。
)为什么 PowerShell 的
-and
和-or
不能替代&&
和||
:< strong>Bash 的控制运算符
&&
(短路逻辑 AND)和||
(短路逻辑 OR)隐式通过退出代码检查命令的成功状态,不干扰其输出流;例如:无论
ls
输出什么 - stdout 输出(/
中的文件)和 stderr 输出(尝试访问不存在的文件nosuchfile< 时的错误消息/code>) --通过,但
&&
检查ls< 的(不可见的)退出代码 /code> 命令来决定是否应执行
echo
命令(&&
控制操作符的 RHS)。在这种情况下,
ls
报告退出代码1
,表示失败 - 因为文件nosuchfile
不存在 -因此&&
判定ls
失败,并通过应用短路,判定echo
命令不需要执行。请注意,退出代码
0
表示在cmd.exe
和bash
领域成功,而任何非零 退出代码表示失败。换句话说:Bash 的
&&
和||
完全独立于命令的输出进行操作,并且仅作用于 < em>命令的成功状态。PowerShell 的
-and
和-or
相比之下,仅按照命令的标准起作用(成功)输出,使用它,然后仅输出运算的布尔结果;例如:上面:
使用并消耗成功(标准)输出 -
\
的列表 - 并将其解释为一个布尔值;非空输入集合在布尔上下文中被视为$true
,因此如果至少有一个条目,则表达式的计算结果为$true
。nosuchfile
产生的错误信息会被传递,因为错误会发送到单独的流。鉴于
Get-ChildItem \, nosuchfile
返回非空成功输出,LHS 计算结果为$true
,因此-and
也计算 RHS,'ok'
,但是,再次使用其输出并将其解释为布尔值,作为非空字符串,它也计算为$正确
。因此,
-and
表达式的总体结果是$true
,这是(唯一成功的)输出。最终效果是:
-and
表达式两侧的成功输出在期间消耗评估,因此有效隐藏。表达式的唯一(成功)输出是其布尔结果,在本例中为
$true
(在终端)。Update:
&&
and||
have finally come to PowerShell (Core), namely in v7+, termed pipeline-chain operators; see this answer for details.Many years after the question was first asked, let me summarize the behavior of Windows PowerShell, whose latest and final version is v5.1:
Bash's /
cmd
's&&
and||
control operators have NO PowerShell equivalents, and since you cannot define custom operators in PowerShell, there are no good workarounds:Use separate commands (on separate lines or separated with
;
), and explicitly test the success status of each command via automatic$?
variable, e.g.:Better alternative for external programs, using the automatic
$LASTEXITCODE
variable; this is preferable, because$?
in Windows PowerShell can yield false negatives (no longer in PowerShell (Core) 7.2+), if a2>
redirection is involved - see this answer:See below for why PowerShell's
-and
and-or
are generally not a solution.[Since implemented in PowerShell (Core) 7+] There was talk about adding them a while back, but it seemingly never made the top of the list.
&&
and||
are currently reserved for future use in PowerShell, so there's hope that the same syntax as in Bash can be implemented.(As of PSv5.1, attempting something like
1 && 1
yields error messageThe token '&&' is not a valid statement separator in this version.
)Why PowerShell's
-and
and-or
are no substitute for&&
and||
:Bash's control operators
&&
(short-circuiting logical AND) and||
(short-circuiting logical OR) implicitly check the success status of commands by their exit codes, without interfering with their output streams; e.g.:Whatever
ls
outputs -- both stdout output (the files in/
) and stderr output (the error message from attempting to access non-existent filenosuchfile
) -- is passed through, but&&
checks the (invisible) exit code of thels
command to decide if theecho
command - the RHS of the&&
control operator - should be executed.ls
reports exit code1
in this case, signaling failure -- because filenosuchfile
doesn't exist -- so&&
decides thatls
failed and, by applying short-circuiting, decides that theecho
command need not be executed.Note that it is exit code
0
that signals success in the world ofcmd.exe
andbash
, whereas any nonzero exit code indicates failure.In other words: Bash's
&&
and||
operate completely independently of the commands' output and only act on the success status of the commands.PowerShell's
-and
and-or
, by contrast, act only on the commands' standard (success) output, consume it and then output only the Boolean result of the operation; e.g.:The above:
uses and consumes the success (standard) output -- the listing of
\
-- and interprets it as a Boolean; a non-empty input collection is considered$true
in a Boolean context, so if there's at least one entry, the expression evaluates to$true
.nosuchfile
is passed through, because errors are sent to a separate stream.Given that
Get-ChildItem \, nosuchfile
returns non-empty success output, the LHS evaluated to$true
, so-and
also evaluates the RHS,'ok'
, but, again, consumes its output and interprets it as a Boolean, which, as a nonempty string, also evaluates to$true
.Thus, the overall result of the
-and
expression is$true
, which is (the only success) output.The net effect is:
The success output from both sides of the
-and
expression is consumed during evaluation and therefore effectively hidden.The expression's only (success) output is its Boolean result, which is
$true
in this case (which renders asTrue
in the terminal).Bash 必须做的就是在传递给逻辑运算符时将命令的整数退出代码解释为 true 或 false。 PowerShell 不会执行此操作 - 但可以创建一个函数来包装命令并创建相同的行为:
($? 是一个包含最后退出代码成功的布尔值)
给定两个批处理文件:
并且
...可以测试行为:
逻辑运算符应该是评估方式与 Bash 中相同。首先,设置一个别名:
测试:
What Bash must be doing is interpreting the integer exit code of the commands as true or false when passed to the logical operators. PowerShell doesn't do this - but a function can be made to wrap the command and create the same behavior:
($? is a bool containing the success of the last exit code)
Given two batch files:
and
...the behavior can be tested:
The logical operators should be evaluated the same way as in Bash. First, set an alias:
Test:
你可以这样做,用 [void] 隐藏布尔输出,并且只会产生副作用。在这种情况下,如果 $a 或 $b 为空,则 $c 被分配给 $result。赋值可以是表达式。
输出
You can do something like this, where you hide the boolean output with [void], and only get the side effect. In this case, if $a or $b are null, then $c gets assigned to $result. An assignment can be an expression.
output
我们可以使用try catch finally方法来代替使用&& powershell 中的方法。
We can use try catch finally method instead of using && method in powershell.