【问题标题】:How do I get the application exit code from a Windows command line?如何从 Windows 命令行获取应用程序退出代码?
【发布时间】:2010-09-24 23:51:31
【问题描述】:

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

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

回声$?

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

【问题讨论】:

  • 谷歌搜索“Win8 如何获取 CMD 提示以显示退出状态”,就像我们在 Linux 中所做的那样。这是首选,而且很准确。
  • 您可以快速查看应用返回的内容:app.exe & echo %errorlevel%

标签: windows command-line process cmd exit-code


【解决方案1】:

名为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%
)

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

【讨论】:

  • 如果您直接从 Windows 命令行运行并且总是看到返回 0,请参阅 Gary 的回答:stackoverflow.com/a/11476681/31629
  • 另外,如果你在 powershell 中,你可以使用echo Exit Code is $LastExitCode
  • 注意:如果errorlevel >= 1,则“errorlevel 1”为真。所以“errorlevel 0”将匹配所有内容。看看 /?”。相反,您可以使用“if %ERRORLEVEL% EQU 0 (..)”。
  • 发现 %ERRORLEVEL% 为 0 即使发生错误的情况。在 cmd 文件中检查 %ERRORLEVEL% 时发生。尝试start /wait 没有用。唯一有效的是if errorlevel 1 (...)
  • 友情提示:%ErrorLevel%是一个shell变量,不是环境变量,而且它还返回一个string而不是int,这意味着你不能使用EQ/@987654341 @ 有效。
【解决方案2】:

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

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

start /wait something.exe
echo %errorlevel%

【讨论】:

  • 不错的收获。我不知道那个命令。我刚刚看到它适用于 > start /wait notepad.exe
  • 它可能不起作用(始终为零)的另一个原因是它位于 if 或 for 内。考虑改用!errorlevel!,如described in this answer。
【解决方案3】:

使用内置的 ERRORLEVEL 变量:

echo %ERRORLEVEL%

但是beware if an application has defined an environment variable named ERRORLEVEL!

【讨论】:

  • 它不是一个实际的环境变量(很明显,这就是为什么如果存在 一个以这种方式命名的变量,它就会停止工作)。
  • @SteelBrain:在 PowerShell 中称为$LastExitCode。
【解决方案4】:

如果你想完全匹配错误代码(例如等于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 /?。

【讨论】:

  • 区分大小写吗?
  • 没有。 vars、命令(包括“if”)和“equ”在任何情况下都有效。
【解决方案5】:

使用未附加到控制台的程序时,它可能无法正常工作,因为当您认为您有退出代码时,该应用程序可能仍在运行。 在 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;
}

【讨论】:

  • 在某些配置中您应该添加#include 以便识别CString 类型。
【解决方案6】:

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

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

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

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

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

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

【讨论】:

    【解决方案7】:

    在某一时刻,我需要将日志事件从 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
    

    【讨论】:

      猜你喜欢
      • 2010-12-18
      • 2020-01-13
      • 2015-04-03
      • 2018-11-04
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      相关资源
      最近更新 更多