繁体   English   中英

什么相当于$? 在Windows?

[英]What is the equivalent to $? in Windows?

有人知道什么是相当于$? 在Windows命令行? 有没有?

编辑: $? 是UNIX变量,它保存最后一个进程的退出代码

您想要检查%ERRORLEVEL%的值。

Windows批处理文件

%ERRORLEVEL%返回最近使用的命令的错误代码。 非零值通常表示错误。

http://technet.microsoft.com/en-us/library/bb490954.aspx

Windows Powershell

$? 如果上次操作成功则包含True,否则包含False。

$ LASTEXITCODE包含上次Win32可执行文件执行的退出代码。

http://blogs.msdn.com/powershell/archive/2006/09/15/ErrorLevel-equivalent.aspx

Cygwin Bash脚本

$? 扩展到最近执行的前台程序的退出状态代码。

http://unix.sjcc.edu/cis157/BashParameters.htm

很抱歉疏通一个旧线程,但值得注意的是%ERRORLEVEL%不会被每个命令重置。 在几行后续 - 和成功 - 批处理代码之后,您仍然可以测试errorlevel的“positive”。

您可以使用ver可靠地将errorlevel重置为干净状态。 此示例适用于UnxUtils以获取更多Linux-ish目录列表。 重置可能在最后看起来无关紧要,但如果我需要从另一个调用此脚本则不行。

:------------------------------------------------------------------------------
:  ll.bat - batch doing its best to emulate UNIX
:  Using UnxUtils when available, it's nearly unix.
:------------------------------------------------------------------------------
:  ll.bat - long list: batch doing its best to emulate UNIX
:  ------------------------------------
:  zedmelon, designer, 2005 | freeware
:------------------------------------------------------------------------------
@echo off
    setlocal
    : use the UnxUtil ls.exe if it is in the path
    ls.exe -laF %1 2>nul

if errorlevel 1 (
    echo.
    echo ----- ls, DOS-style, as no ls.exe was found in the path -----
    echo.
    dir /q %1
    )

: reset errorlevel
    ver>nul 
    endlocal

随意使用它。 如果您还没有看过UnxUtils,请检查它们。

@echo off
run_some_command
if errorlevel 2 goto this
if errorlevel 1 goto that
goto end

:this
echo This
goto end

:that
echo That
goto end

:end

%ERRORLEVEL%

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM