893

我正在运行一个程序,想看看它的返回码是什么(因为它会根据不同的错误返回不同的代码)。

我知道在 Bash 中我可以通过运行来做到这一点

回声$?

在 Windows 上使用 cmd.exe 时我该怎么办?

4

7 回答 7

1093

一个名为的伪环境变量errorlevel存储退出代码:

echo Exit Code is %errorlevel%

此外,该if命令有一个特殊的语法:

if errorlevel

详情请参阅if /?

例子

@echo off
my_nify_exe.exe
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

警告:如果您设置环境变量 name errorlevel%errorlevel%将返回该值而不是退出代码。使用 ( ) 清除环境变量,允许通过set errorlevel=环境变量访问真实值。errorlevel%errorlevel%

于 2008-12-02T18:07:58.780 回答
298

测试ErrorLevel适用于控制台应用程序,但正如dmihailescu所暗示的,如果您尝试从命令提示符运行窗口应用程序(例如基于 Win32 的),这将不起作用。一个窗口应用程序将在后台运行,并且控制将立即返回到命令提示符(很可能用一个ErrorLevel零表示该进程已成功创建)。当窗口应用程序最终退出时,它的退出状态将丢失。

不过,不使用其他地方提到的基于控制台的 C++ 启动器,更简单的替代方法是使用命令提示符的START /WAIT命令启动窗口应用程序。这将启动窗口应用程序,等待它退出,然后将控制权返回到命令提示符,进程的退出状态设置为ErrorLevel.

start /wait something.exe
echo %errorlevel%
于 2012-07-13T18:57:54.333 回答
117

使用内置的 ERRORLEVEL 变量:

echo %ERRORLEVEL%

请注意,如果应用程序定义了一个名为 ERRORLEVEL 的环境变量

于 2008-12-02T18:09:03.240 回答
27

如果您想完全匹配错误代码(例如等于 0),请使用:

@echo off
my_nify_exe.exe
if %ERRORLEVEL% EQU 0 (
   echo Success
) else (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

if errorlevel 0匹配errorlevel>= 0。请参阅if /?

于 2014-07-29T16:08:20.903 回答
14

使用未附加到控制台的程序时,它可能无法正常工作,因为当您认为您拥有退出代码时,该应用程序可能仍在运行。在 C++ 中执行此操作的解决方案如下所示:

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "tchar.h"
#include "stdio.h"
#include "shellapi.h"

int _tmain( int argc, TCHAR *argv[] )
{

    CString cmdline(GetCommandLineW());
    cmdline.TrimLeft('\"');
    CString self(argv[0]);
    self.Trim('\"');
    CString args = cmdline.Mid(self.GetLength()+1);
    args.TrimLeft(_T("\" "));
    printf("Arguments passed: '%ws'\n",args);
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc < 2 )
    {
        printf("Usage: %s arg1,arg2....\n", argv[0]);
        return -1;
    }

    CString strCmd(args);
    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        (LPTSTR)(strCmd.GetString()),        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d)\n", GetLastError() );
        return GetLastError();
    }
    else
        printf( "Waiting for \"%ws\" to exit.....\n", strCmd );

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    int result = -1;
    if(!GetExitCodeProcess(pi.hProcess,(LPDWORD)&result))
    { 
        printf("GetExitCodeProcess() failed (%d)\n", GetLastError() );
    }
    else
        printf("The exit code for '%ws' is %d\n",(LPTSTR)(strCmd.GetString()), result );
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return result;
}
于 2010-06-25T17:05:45.520 回答
12

值得注意的是 .BAT 和 .CMD 文件的操作方式不同。

阅读https://ss64.com/nt/errorlevel.html它注意到以下内容:

.CMD 和 .BAT 批处理文件设置错误级别的方式有一个关键区别:

运行“新”内部命令的旧 .BAT 批处理脚本:APPEND、ASSOC、PATH、PROMPT、FTYPE 和 SET 只会在发生错误时设置 ERRORLEVEL。因此,如果批处理脚本中有两个命令并且第一个命令失败,则即使第二个命令成功,ERRORLEVEL 仍将保持设置。

这会使调试问题 BAT 脚本更加困难,CMD 批处理脚本更加一致,并且会在您运行的每个命令后设置 ERRORLEVEL [来源]。

当我执行连续的命令时,这让我感到无限的悲痛,但即使发生故障,ERRORLEVEL 也将保持不变。

于 2018-09-18T06:13:09.710 回答
0

有一次,我需要将日志事件从 Cygwin 准确推送到 Windows 事件日志。我希望 WEVL 中的消息是自定义的,具有正确的退出代码、详细信息、优先级、消息等。所以我创建了一个小 Bash 脚本来处理这个问题。它在 GitHub 上,logit.sh

部分摘录:

usage: logit.sh [-h] [-p] [-i=n] [-s] <description>
example: logit.sh -p error -i 501 -s myscript.sh "failed to run the mount command"

这是临时文件内容部分:

LGT_TEMP_FILE="$(mktemp --suffix .cmd)"
cat<<EOF>$LGT_TEMP_FILE
    @echo off
    set LGT_EXITCODE="$LGT_ID"
    exit /b %LGT_ID%
EOF
unix2dos "$LGT_TEMP_FILE"

这是在 WEVL 中创建事件的函数:

__create_event () {
    local cmd="eventcreate /ID $LGT_ID /L Application /SO $LGT_SOURCE /T $LGT_PRIORITY /D "
    if [[ "$1" == *';'* ]]; then
        local IFS=';'
        for i in "$1"; do
            $cmd "$i" &>/dev/null
        done
    else
        $cmd "$LGT_DESC" &>/dev/null
    fi
}

执行批处理脚本并调用 __create_event:

cmd /c "$(cygpath -wa "$LGT_TEMP_FILE")"
__create_event
于 2015-02-28T19:33:25.587 回答