windows 窗口 shell 脚本返回码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2381778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:04:26  来源:igfitidea点击:

Window shell script return codes

windowsshell

提问by theactiveactor

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.

是否有一组标准的 Window shell 脚本(*.bat 文件)返回码?我正在寻找类似于 Linux 退出代码的东西,其中 0==success 和非零==failure。我需要一种方法来以编程方式检查我的 shell 脚本在执行过程中是否失败。

采纳答案by Colin Pickard

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.

最常见的做法与 Unix 标准相同,因此返回码(在批处理文件中也称为错误级别)为 0 是成功,而任何高于 0 的都是错误。

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

不过,有许多相关的问题需要寻找 - 请查看本指南:

Batch Files - Errorlevels

批处理文件 - 错误级别

回答by John Knoeller

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.

0 表示成功,非 0 表示失败也是 Windows 批处理命令的约定。当命令失败时,它会设置 ERRORLEVEL,这是一个可以在批处理文件中测试的特殊变量。

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.

只要您不运行其他命令,错误级别就会传递到运行 .bat 文件的调用者。

回答by bta

Typically, Windows utilities return 0on 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.

通常,Windows 实用程序0在成功时返回,在错误时返回非零值(通过 ERRORLEVEL 变量),就像 Linux 应用程序一样。不幸的是,没有强制的、官方的“标准”,也不是每个实用程序或脚本都设置错误级别。

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

如果您有一个要测试其返回状态的脚本,请确保该脚本退出时使用EXIT /B ##导致错误级别设置为##.

回答by Patrick

You can check the errorlevel value.

您可以检查错误级别值。

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

IF shell 语句的帮助告诉我以下内容:

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.

回答by user1344919

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.

这样我就确切地知道错误级别是什么。我认为最多可能有 250 个错误级别。