如何从 Windows 命令行获取应用程序退出代码? [英] How do I get the application exit code from a Windows command line?

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

问题描述

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

I am running a program and want to see what its return code is (since it returns different codes based on different errors).

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

I know in Bash I can do this by running

回声 $?

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

What do I do when using cmd.exe on Windows?

推荐答案

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

A pseudo environment variable named errorlevel stores the exit code:

echo Exit Code is %errorlevel%

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

Also, the if command has a special syntax:

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的真实值.

Warning: If you set an environment variable name errorlevel, %errorlevel% will return that value and not the exit code. Use (set errorlevel=) to clear the environment variable, allowing access to the true value of errorlevel via the %errorlevel% environment variable.

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

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