How to write call command in batch file conditionally?
I have two lines of call command in batch file like this:
call execute.cmd
call launch.cmd
My ne开发者_C百科ed is to call the launch.cmd if and only if call to execute.cmd succeeds. So is there any way by which can I put some condition here?
execute.cmd does not return any value here.
If execute.cmd
returns an integer than you can use a IF command
to check it's return value and if it matches the desired one than you can call launch.cmd
Suppose that execute.cmd
returns 0 if it is successful or an integer >= 1 otherwise. The batch would look like this:
rem call the execute command
call execute.cmd
rem check the return value (referred here as errorlevel)
if %ERRORLEVEL% ==1 GOTO noexecute
rem call the launch command
call launch.cmd
:noexecute
rem since we got here, launch is no longer going to be executed
note that the rem
command is used for comments.
HTH,
JP
I believe this is a duplicate of How do I make a batch file terminate upon encountering an error?.
Your solution here would be:
call execute.cmd
if %errorlevel% neq 0 exit /b %errorlevel%
call launch.cmd
if %errorlevel% neq 0 exit /b %errorlevel%
Unfortunately, it looks like Windows batch files have no equivalent of UNIX bash's set -e
and set -o pipefail
. If you're willing to abandon the very limited batch file language, you could try Windows PowerShell.
精彩评论