Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent to $? in Windows?

Does anybody know what is the equivalent to $? in Windows command line? Is there any?

EDIT: $? is the UNIX variable which holds the exit code of the last process

like image 737
victor hugo Avatar asked Jun 23 '09 23:06

victor hugo


People also ask

What is the equivalent Command in Windows?

The where command is a Windows which equivalent in a command-line prompt (CMD). In a Windows PowerShell the alternative for the which command is the Get-Command utility.

What is equivalent in PowerShell?

Use gcm as the Equivalent of Which Command in PowerShell You can use the gcm alias as the equivalent of which command in PowerShell.

What is the equivalent of LS in Windows?

Windows “dir” Command is “ls” Command Equivalent Windows MS-DOS and PowerShell command-line interface provide the dir command in order to list files and folders.


3 Answers

You want to check the value of %ERRORLEVEL%.

like image 64
Tim Gilbert Avatar answered Oct 12 '22 11:10

Tim Gilbert


Windows Batch Files

%ERRORLEVEL% Returns the error code of the most recently used command. A non zero value usually indicates an error.

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

Windows Powershell

$? Contains True if last operation succeeded and False otherwise. And

$LASTEXITCODE Contains the exit code of the last Win32 executable execution.

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

Cygwin Bash Scripting

$? Expands to the exit status code of the most recently executed foreground program.

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

like image 9
heavyd Avatar answered Oct 12 '22 11:10

heavyd


Sorry to dredge up an old thread, but it's worth noting that %ERRORLEVEL% doesn't get reset with every command. You can still test "positive" for errorlevel after several lines of subsequent--and successful--batch code.

You can reliably reset errorlevel to a clean status with ver. This example works with UnxUtils for a more Linux-ish directory listing. The reset might seem extraneous at the end, but not if I need to call this script from another.

:------------------------------------------------------------------------------
:  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

Feel free to use this. If you haven't seen UnxUtils, check 'em out.

like image 3
zedmelon Avatar answered Oct 12 '22 11:10

zedmelon