开发者

Window shell script return codes

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.


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


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.


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.


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 ##.


I prefer this way:

[run an exe here]

IF %errorlevel% NEQ 0 (
    CALL :SSH_fail filename.sh %errorlevel%
)


:SSH_fail
    Email.exe "%mailTo%" "%mailProgram% - SSH Failure " "Body: errorlevel:%~2 file name: %~1"
goto cont

this way I know exactly what the errorlevel was. I think there can be up to like 250 errorlevels.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜