如果程序失败,是否有一个优雅的 Powershell 脚本设置可以退出正在运行的 Powershell 脚本(或 shell 实例)?
我正在想象类似 Bash 功能set -o errexit(或set -e)但用于 Powershell 的东西。在该功能中,如果 bash 脚本中的程序失败(进程返回代码不是0),则 bash shell 实例会立即退出。
在 powershell 中,脚本可以检查$LastExitCode. 但是,对于每个程序调用来说,这变得很麻烦。也许powershell有一个功能可以自动检查程序返回码并做出反应。
Set-Powershell-Auto-Exit 'ProgramReturnCode' # what should this be?
& program.exe --fail # this program fails with an error code
& foo.exe # this never runs because script has exited
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
249 次 |
| 最近记录: |