根据文档,PS 7 引入了流水线链接操作符,例如||
和&&
.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipeline_chain_operators?view=powershell-7
您应该能够执行 C# 风格的短路操作,例如:
Write-Error 'Bad' && Write-Output 'Second'
上面的例子有效。并且文档说管道链接操作员使用两个字段(不确定它是如何精确工作的):$?
和$LASTEXITCODE
我如何将这些应用到我自己的函数中?
例如:
function yes() {
echo 'called yes'
return $True
}
function no() {
echo 'called no'
return $False
}
我觉得我应该能够运行以下命令no && yes
并看到以下输出
叫不
错误的
但相反,我看到
叫不
错误的
叫是
真的
那么,我如何以可以使用流水线链接和短路的方式开发功能呢?
&&
编辑:我现在能想出的唯一方法是构造一个自定义函数来使an 短路throw
,但这在一般情况下似乎不太有用。