加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

批处理文件 – 什么是bash的set -e的cmd.exe替代品?

发布时间:2020-12-15 21:33:28 所属栏目:安全 来源:网络整理
导读:我想获得与bash set -e相同但在 Windows批处理文件中提供的相同功能.这有可能怎么样? 目前这允许我退出但我不想在批处理文件的每一行之后添加此行. IF %ERRORLEVEL% NEQ 0 exit /B %ERRORLEVEL% 解决方法 以供参考 -e errexit Exit immediately if a simple
我想获得与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%

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读