Reputation: 3615
Running this batchfile from the Windows commandline results in an %errorlevel% of 5 (i.e. running echo %errorlevel%
on the commandline after having executing the batch file prints the number 5):
EXIT /B 5
This is fine.
However, running this batchfile results in an %errorlevel% of 0, no matter what:
sleep 1
EXIT /B 5
I want it to return the error code 5. How can I do that?
Note: If I add sys.exit(13) to the sleep.py (see below), then the second batch file will return with an exit code of 13. So my batch file will return with the exit code of the sleep.py script instead of the exit code specified via the EXIT command (which is strange).
sleep.bat:
sleep.py %1
sleep.py:
import sys
import time
if len(sys.argv) == 2:
time.sleep(int(sys.argv[1]))
Upvotes: 1
Views: 636
Reputation: 354446
sleep
is a batch file. When calling a batch file from another batch file this way, control never returns to the caller (akin to exec
in POSIX). Use call sleep 1
instead.
Or just sleep with built-in programs:
:sleep1
setlocal
set /a X=%~1 + 1
ping ::1 -n %X% >nul 2>&1
endlocal
goto :eof
:sleep2
timeout /T %1 /nobreak
goto :eof
Upvotes: 3