batch-file - bash 的 set -e 的 cmd.exe 替代品是什么?

标签 batch-file cmd exit-code

我想获得与 bash set -e 提供的相同的功能但在 Windows 批处理文件中。这怎么可能?

目前这允许我退出,但我不想在批处理文件的每一行之后添加这一行。

IF %ERRORLEVEL% NEQ 0 exit /B  %ERRORLEVEL%

最佳答案

以供引用

-e   errexit
      Exit immediately if a simple command exits with a non-zero
      status, unless the command that fails is part of an until or
      while loop, part of an if statement, part of a && or || list,
      or if the command's return status is being inverted using !.
http://ss64.com/bash/set.html
不幸的是,仅使用一个命令是不可能的。
您必须在 之后添加支票每个命令 你想在出错时退出。您可以使用或检查 || 而不是整个单独的行在命令结果上。
command || exit /b
这也可以通过将其放在开头的变量中来简化。
set "e=|| exit /b"
command1 %e%
command2 %e%

关于batch-file - bash 的 set -e 的 cmd.exe 替代品是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17898688/

相关文章:

batch-file - Robocopy 无效参数 #3

windows - 我无法通过批处理脚本将多个文件上传到 FTP

java - 在 javac 中指定编码会产生与在 Windows CMD 中更改 Activity 代码页然后直接编译相同的结果吗?

MySQL 没有捕捉到错误

batch-file - 在 Windows 批处理文件中,如何使用十进制数的十六进制表示设置环境变量?

windows - 我需要仅使用 native Windows 命令匹配或替换批处理环境变量中的星号 *。这可能吗?

cmd - 编程时如何缩短控制台上显示的错误代码

c# - 如何使用退出代码 : C# 退出程序

python - 重命名目录中的子文件夹 - python