batch file - Is Windows %ERRORLEVEL% == $? in Linux -


trying hands on windows batch files, in below code found searching in www.

@echo off rem call 2 arguments, , add them. set a=%1+%2 if %errorlevel%==0 (goto errors-0) else (goto errors-1) rem instead of using goto variable, uses if-else structure :errors-0 rem if successful echo %a% goto exit :errors-1 rem if had error: echo errors occurred. goto exit rem guess what, rem never ever read!  skipped on gotos :exit echo. echo press key exit. pause>nul 

the code suppose take 2 arguments, add them , echo result.

but won't execute success on windows 8.1 machine. below get:

c:\projectdoc>add3.bat errors occurred.  press key exit. 

so, u added echo errorlevel see value after executing command set. below output:

c:\projectdoc>add3.bat 2 3 9009 errors occurred.  press key exit. c:\projectdoc> 

so, errorlevel in windows equal $? of linux. should returning 0 every successful execution of command or different? reading docs relates linux $? isn't working $? in linux.

yes, precise, %errorlevel% analogous $? in bash shell.

in batch file, set a=%1+%2 not doing expect do. sets value of variable a string "2+3" assuming ran file arguments 2 3. if want arithmetic need use /a option: set /a a=%1+%2.

the set command (and many other built-in commands) set errorlevel if there has been error. if successful, errorlevel retain previous value. think you're witnessing in question.

by contrast, when command runs executable file, when process completes sets errorlevel.

as checking errorlevel variable specific values, idiomatic (for historical reasons) check errorlevel using following expression

if errorlevel 1 echo hello

this run given command if errorlevel 1 or above - in other words, if error has occurred.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -