tags:

views:

46

answers:

4

Is there a standard set of return code for Window shell scripts (*.bat files)? I'm looking for something analogous to Linux exit codes, where 0==success and non-zero==failure. I need a way to programmatically check if my shell script failed during execution.

+1  A: 

The most common practive is the sames as the Unix standard, so a return code (also called errorlevel in batch files) of 0 is success, whereas anything higher than 0 is an error.

There are a number of related gotchas to look for though - have a look at this guide:

Batch Files - Errorlevels

Colin Pickard
+1  A: 

You can check the errorlevel value.

The help of the IF shell statement tells me the following:

IF [NOT] ERRORLEVEL number command

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.
Patrick
+1  A: 

0 for success and non-0 for failure is also the convention for Windows batch commands. when a command fails it sets ERRORLEVEL which is a special variable that can be tested in batch files.

if errorlevel 1 goto failure

As long as you don't run another command, the errorlevel will carry through to the caller that ran the .bat file.

John Knoeller
+1  A: 

Typically, Windows utilities return 0 on success and non-zero on error (through the ERRORLEVEL variable) like Linux apps do. Unfortunately, there is no enforced, official "standard" and not every utility or script sets errorlevels.

If you have a script that you want to test the return status for, make sure that script exits using EXIT /B ## which causes the errorlevel to be set to ##.

bta