如何从Windows命令行获取应用程序退出代码?

h9vpoimq  于 2022-11-18  发布在  Windows
关注(0)|答案(7)|浏览(161)

我正在运行一个程序,想看看它的返回代码是什么(因为它根据不同的错误返回不同的代码)。
我知道在巴什我可以跑
echo $?
在Windows上使用cmd.exe时该怎么办?

55ooxyrt

55ooxyrt1#

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

echo Exit Code is %errorlevel%

此外,if命令具有特殊语法:

if errorlevel

有关详细信息,请参见if /?

示例

@echo off
my_nifty_exe.exe
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)
  • 警告:如果设置了环境变量名称errorlevel%errorlevel%将返回该值,而不是退出代码。使用(set errorlevel=)清除环境变量,允许通过%errorlevel%环境变量访问errorlevel的真实值。*
whitzsjs

whitzsjs2#

测试ErrorLevel适用于 console 应用程序,但正如by dmihailescu所暗示的,如果您试图运行一个 windowed 应用程序,则此测试将不起作用(例如Win32架构)。 windows 化的应用程序会在背景执行,并且控制将立即返回到命令提示符(很可能ErrorLevel为零,表示进程已成功 * 创建 *)。当窗口化应用程序最终退出时,其退出状态将丢失。
不过,与使用其他地方提到的基于控制台的C++启动器不同,一个更简单的替代方法是使用命令提示符的START /WAIT命令启动窗口应用程序。这将启动窗口应用程序,等待它退出,然后将控制返回给命令提示符,并在ErrorLevel中设置进程的退出状态。

start /wait something.exe
echo %errorlevel%
gkn4icbw

gkn4icbw4#

如果您想完全匹配错误代码(例如等于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 0errorlevel〉= 0匹配。

请参阅if /?
或者,如果你不成功:

if  %ERRORLEVEL% NEQ 0 (
    echo Failed with exit-code: %errorlevel%
    exit /b %errorlevel%
)
yruzcnhs

yruzcnhs5#

值得注意的是,.BAT和.CMD文件的操作方式不同。
阅读https://ss64.com/nt/errorlevel.html时,会注意到以下内容:
.CMD和.BAT批处理文件设置错误级别的方式有一个关键区别:
运行“新”内部命令的旧.BAT批处理脚本:APPEND、ASSOC、PATH、PROMPT、FTYPE和SET只在发生错误时设置ERRORLEVEL。因此,如果批处理脚本中有两个命令,并且第一个命令失败,即使第二个命令成功,ERRORLEVEL仍将保持设置。
这会使调试问题BAT脚本更加困难,CMD批处理脚本更加一致,并且会在您运行[source]的每个命令后设置ERRORLEVEL。
当我执行连续的命令时,这给我带来了无尽的悲伤,但即使在失败的情况下,ERRORLEVEL也不会改变。

zf9nrax1

zf9nrax16#

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

oxosxuxt7#

有一次我需要准确地将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

相关问题