Stackoverflow热门问题(二十二)-如何在Windows cmd中得到程序返回值?

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

如何在Windows cmd中得到程序返回值?

  • Skrud asked:
    • 我希望得到程序结束的返回值(因为不同返回值意味着不同错误)。
    • 我知道在Bash里可以通过这样运行得到

      echo $?

    • 但Windows的cmd.exe里面该如何做到?
  • Answers:
    • DrFloyd5 – vote: 1058
      • 有个伪环境变量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=) 来清理这个环境变量,并正常返回程序的返回值。
    • Gary – vote: 291
      • 命令行中errorlevel的确有用,但就如dmihailescu说的一般,在运行一个窗口化的应用时,并不能得到想要的结果。窗口化的应用会后台运行,并立即返回值(通常是0,表示进程成功创建)。当这个应用结束的时候,它的返回值会丢失。
      • 因此推荐使用START /WAIT命令,它会开启窗口应用,并且等待它退出,对应返回值可以在errorlevel得到。
      • start /wait something.exe
        echo %errorlevel%
    • Adam Rosenfield – vote: 106

How do I get the application exit code from a Windows command line?

  • Skrud asked:
    • I am running a program and want to see what its return code is (since it returns different codes based on different errors).
      我希望得到程序结束的返回值(因为不同返回值意味着不同错误)。
    • I know in Bash I can do this by running
      我知道在Bash里可以通过这样运行得到

      echo $?

    • What do I do when using cmd.exe on Windows?
      但Windows的cmd.exe里面该如何做到?
  • Answers:
    • DrFloyd5 – vote: 1058
    • A pseudo environment variable named errorlevel stores the exit code:
      有个伪环境变量errorlevel存储了返回值:
    • echo Exit Code is %errorlevel%
    • Also, the if command has a special syntax:
      对应了if的特殊语法:
    • if errorlevel
    • See if /? for details.
      if /? 了解更多
    • Example
      例子
    • @echo off`
      my_nify_exe.exe
      if errorlevel 1 (
       echo Failure Reason Given is %errorlevel%` 
       exit /b %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.
      警告:如果设置了一个errorlevel的环境变量,%errorlevel%将会返回它的值,而不是程序的返回值。使用 (set errorlevel=) 来清理这个环境变量,并正常返回程序的返回值。
    • Gary – vote: 291
    • Testing ErrorLevel works for console applications, but as hinted at by dmihailescu, this won\’t work if you\’re trying to run a windowed application (e.g. Win32-based) from a command prompt. A windowed application will run in the background, and control will return immediately to the command prompt (most likely with an ErrorLevel of zero to indicate that the process was created successfully). When a windowed application eventually exits, its exit status is lost.
      命令行中errorlevel的确有用,但就如dmihailescu说的一般,在运行一个窗口化的应用时,并不能得到想要的结果。窗口化的应用会后台运行,并立即返回值(通常是0,表示进程成功创建)。当这个应用结束的时候,它的返回值会丢失。
    • Instead of using the console-based C++ launcher mentioned elsewhere, though, a simpler alternative is to start a windowed application using the command prompt\’s START /WAIT command. This will start the windowed application, wait for it to exit, and then return control to the command prompt with the exit status of the process set in ErrorLevel.
      因此推荐使用START /WAIT命令,它会开启窗口应用,并且等待它退出,对应返回值可以在errorlevel得到。
    • start /wait something.exe
      echo %errorlevel%
    • Adam Rosenfield – vote: 106
    • Use the built-in ERRORLEVEL Variable:
      使用内置的ERRORLEVEL变量
    • echo %ERRORLEVEL%
    • But beware if an application has defined an environment variable named ERRORLEVEL!
      要注意是否有应用定义了同名的环境变量

You may also like...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注