从"cmd"开始获取应用程序的退出代码.和“开始"命令 [英] Getting the exit code of an application started with the "cmd" and "start" commands

查看:94
本文介绍了从"cmd"开始获取应用程序的退出代码.和“开始"命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序.通过TCP/IP与该应用程序进行交互.

I have a console application. Interaction with this application is done via TCP/IP.

我也有一个测试框架,它基本上是BATCH脚本的集合(...不是我的错).该测试框架为每个测试所做的基本上是这样的:

I also have a test framework for it, which is basically a collection of BATCH scripts (...not my fault). What this test framework does for each test is basically this:

  1. start /min "myapplication.exe",并等待直到收到验证应用程序已启动并正在运行的验证.
  2. 通过TCP/IP将命令发送到此应用程序,接收其答复,并检查时间和值是否与特定测试所期望的一致.
  1. start /min "myapplication.exe" and wait until verification is received that the application is up and running.
  2. send commands via TCP/IP to this application, receive its replies, and check if the timings and values agree with whatever is expected by the specific test.

我当前遇到的一个问题是,由于某些内部错误,该应用程序过早退出.我想区分失败的测试和应用程序崩溃.我对此唯一唯一的指示就是应用程序的退出代码.

One problem that I'm currently encountering is that the application exits prematurely due to some internal error. I would like to distinguish between failed tests, and the application crashing. The one and only indication I have for this, is the application's exit code.

因此,我尝试了以下操作:

So, I tried the following:

start /min cmd /c "myapplication.exe || echo %errorLevel% > exitcode.txt"

,然后在测试脚本中,

if exist exitcode.txt (
    set /p exitcode=<exitcode.txt
    echo ERROR: myapplication.exe returned exitcode %exitcode%.
    goto error
) else (
    goto do_processing
)

但是由于某些奇怪的原因,即使我有时会收到有关应用程序崩溃的对话框,并且即使我用已知的非零退出代码强制使它失败,该文本文件也永远不会出现.测试仅通过do_processing,并且(当然)导致失败.

but for some strange reason, the text file never appears, even though I sometimes get a dialog about the application crashing, and even though I forcibly make it fail with a known non-zero exit code. The test just goes through do_processing and (of course) results in failure.

编辑 当我运行

start /min cmd /c "nonsense || echo %errorLevel% > test.txt"

我有时 得到一个包含字符串9009的文本文件,但有时该文本文件包含字符串0或有时为1的……什么...?!

I sometimes get a text file containing the string 9009, but other times that text file contains the string 0, or sometimes 1, ...What the...?!

EDIT2 如果您输入

cmd /k "nonsense || echo %errorLevel%"

(请注意/k选项),您会看到0正在新窗口中打印,但是如果您键入echo %errorlevel%,则会得到1....

(note the /k option), you see 0 being printed in the new window, but if you then type echo %errorlevel%, you get 1....

我知道批次不是很理智,但至少应该始终保持疯狂 ...

I knew batch was not very sane, but it should at least be consistently insane...

关于这里可能发生什么的任何想法?

Any ideas on what could be going on here?

推荐答案

解析该语句时会发生像%errorLevel%这样的常规扩展,并且一次解析了整个CMD/C命令行,因此得到的值是在运行命令之前存在的值(始终为0).

Normal expansion like %errorLevel% occurs when the statement is parsed, and the entire CMD /C command line is parsed in one pass, so the value you get is the value that existed before the commands were run (always 0).

您可以在 https://stackoverflow.com/a/4095133/1012053 上获得更精确的解释.首先可能很难理解并理解各个阶段的重要性,但是值得付出努力.

You can get a more precise explanation at https://stackoverflow.com/a/4095133/1012053. It can be difficult to follow and understand the significance of the phases at first, but it is worth the effort.

要解决您的问题,必须将变量的扩展延迟到exe运行之后.

To solve your problem you must delay expansion of the variable until after your exe has run.

您有两个选择:

选项1)使用CALL进行延迟的扩展.

在批处理文件中,您将使百分比增加一倍.但是,使用CMD/C运行的命令是在命令行上下文而不是批处理下运行的.在命令行下无法将百分比加倍.

In a batch file you would double the percents. But the commands run using CMD /C are run under a command line context, not batch. Doubling the percents does not work under the command line.

相反,必须在变量名称中引入插入符号(cmd.exe转义字符).扩展的第一阶段发生在转义被处理之前,因此它寻找带有插入符号的名称,但找不到它.当找不到时,命令行解析器将在找不到变量时保留原始文本.接下来处理特殊字符并使用转义符.因此,在发生CALL扩展回合时,它会看到正确的变量名称.

Instead, you must introduce a caret (cmd.exe escape character) into the variable name. The first phase of expansion occurs before escapes are processed, so it looks for a name with the caret, and doesn't find it. When not found, the command line parser preserves the original text when the variable is not found. Next the special characters are handled and the escape is consumed. So when the CALL round of expansion occurs, it sees the correct variable name.

start /min cmd /c "myapplication.exe || call echo %^errorLevel% > exitcode.txt"

我相信您是在批处理脚本中发出START命令,因此您还必须将百分比加倍,以防止父批处理脚本扩展ERRORLEVEL.

I believe you are issuing the START command within a batch script, so you must also double the percents to prevent the parent batch script from expanding ERRORLEVEL.

start /min cmd /c "myapplication.exe || call echo %%^errorLevel%% > exitcode.txt"

选项2)使用延迟扩展

延迟的扩展语法为!errorlevel!,而不是%errorlevel%.但是,在使用它之前,必须先启用延迟扩展.在批处理脚本中,您将使用setlocal enableDelayedExpansion,但这在命令行上下文中不起作用.而是必须使用cmd.exe /v:on选项.

Delayed expansion syntax is !errorlevel! instead of %errorlevel%. But before you can use it, delayed expansion must be enabled. In a batch script you would use setlocal enableDelayedExpansion, but that doesn't work in a command line context. Instead, you must use the cmd.exe /v:on option.

假设您的批处理脚本未启用延迟扩展,则只需使用以下内容:

Assuming your batch script has not enabled delayed expansion, then you would simply use the following:

start /min cmd /v:on /c "myapplication.exe || echo !errorLevel! > exitcode.txt"

但是,如果您的批处理脚本启用了延迟扩展,则必须转义!,以便父批处理脚本不会扩展ERRORLEVEL.请注意,您仍必须使用/v:on,因为启动"子进程(通常)默认为禁用延迟扩展.

But if your batch script has enabled delayed expansion, then you must escape the ! so that the parent batch script does not expand ERRORLEVEL. Note that you must still use /v:on because the STARTed sub-process (normally) defaults to disabled delayed expansion.

start /min cmd /v:on /c "myapplication.exe || echo ^!errorLevel^! > exitcode.txt"

这篇关于从"cmd"开始获取应用程序的退出代码.和“开始"命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆