什么是PowerShell的等价猛砸的&放大器;&安培;和||运营商? [英] What are the PowerShell equivalent of Bash's && and || operators?
问题描述
在猛砸我可以轻松地做这样的事情。
In Bash I can easily do something like
command1 && command2 || command3
这意味着运行Command1和在command1成功运行命令2,如果是command1无法运行指令代码。
which means to run command1 and if command1 succeeds to run command2 and if command1 fails to run command3.
什么是在PowerShell中的相同呢?
What's the equivalent in PowerShell?
推荐答案
什么猛砸必须做传递到逻辑运算符时隐式铸造的命令布尔的出口code。 PowerShell不会做到这一点 - 但可以做一个函数来包装命令,并创建相同的行为:
What Bash must be doing is implicitly casting the exit code of the commands to a Boolean 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:
> function Get-ExitBoolean($cmd) { & $cmd | Out-Null; $? }
由于两个批处理文件:
#pass.cmd
exit
和
#fail.cmd
exit /b 200
...的行为可以被测试
...the behavior can be tested:
> if (Get-ExitBoolean .\pass.cmd) { write pass } else { write fail }
pass
> if (Get-ExitBoolean .\fail.cmd) { write pass } else { write fail }
fail
逻辑运算符应进行评估的方式与巴什一样。首先,设置一个别名:
The logical operators should be evaluated the same way as in Bash. First, set an alias:
> Set-Alias geb Get-ExitBoolean
测试:
> (geb .\pass.cmd) -and (geb .\fail.cmd)
False
> (geb .\fail.cmd) -and (geb .\pass.cmd)
False
> (geb .\pass.cmd) -and (geb .\pass.cmd)
True
> (geb .\pass.cmd) -or (geb .\fail.cmd)
True
这篇关于什么是PowerShell的等价猛砸的&放大器;&安培;和||运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!