Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be the cmd.exe alternative to bash's set -e?

I want to obtain the same functionaty that is offered by bash set -e but inside Windows batch files. Is this possible how?

Currently this allow me to exit but I don't want to add this line after each line of the batch file.

IF %ERRORLEVEL% NEQ 0 exit /B  %ERRORLEVEL%
like image 235
sorin Avatar asked Jul 27 '13 14:07

sorin


2 Answers

For Reference

-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

This is unfortunately not possible with just a single command.

You will have to add a check after each command you want to exit upon error. Instead of a whole separate line you can just use an or check || on the command result.

command || exit /b

This can also be simplified by putting it in a variable at the beginning.

set "e=|| exit /b"
command1 %e%
command2 %e%
like image 59
David Ruhmann Avatar answered Oct 07 '22 13:10

David Ruhmann


The context of the your batch file is important. This should allow you to launch multiple files and exit on a true errorlevel.

@echo off
for %%a in (
"exeone"
"exetwo"
"exethree"
"exefour"
) do "%%~a" || exit /B  %ERRORLEVEL%
like image 42
foxidrive Avatar answered Oct 07 '22 14:10

foxidrive