#1 29 Jan 2007 15:04

stafergy
Member
Registered: 29 Jan 2007
Posts: 2

Internal subroutines and exit /b

Hi,

Its my understanding that issuing an "exit /b" from anywhere within the script will cause execution to stop. However, I'm issuing an "exit /b" from within an internal subroutine ":s_sub1" and the behavior that I'm experiencing is that the subroutine is being exited but then processing continues with the call to the next subroutine, ":s_sub2". Example:

::MAIN
call :s_sub1
call :s_sub2
:: END MAIN

:s_sub1
IF [condition] exit /b
GOTO :eof

What am I misunderstanding?

Thanks,

Scott

Offline

#2 29 Jan 2007 21:50

daat99
Member
From: Israel
Registered: 29 Jan 2007
Posts: 18
Website

Re: Internal subroutines and exit /b

I would try to do exit 0 or exit -1 instead of /b, not really sure what's the difference though.


I always try to help wink
Sometimes I don't know how sad

Offline

#3 01 Feb 2007 16:26

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: Internal subroutines and exit /b

I hadn't explored this feature(?) myself, but I'm presuming that what happens when you issue a "call :label" command, effectively another instance of CMD is launched, which processes the procedure.  Thus when you "exit /b" from a called procedure you are terminating this child CMD instance, rather than the parent.

This actually looks quite handy, because you can exit a procedure with a trapable errorlevel set.  See this code sample (and your own experience!)

@echo off
call :this
echo %errorlevel%
call :that
echo %errorlevel%
pause

goto :EOF

:this
echo %~0

exit /b 1

goto :EOF

:that

echo %~0

exit /b 2

goto :EOF

To make it do what you want to do, you'd either need to lose the "/b" switch (resulting in a quick and dirty end to the script), or check the exit condition after the "call :label" line and issue an "exit", "exit /b" or "goto :EOF" at that point.  Does that make sense?  I know what I mean smile

~bxm


cmd | *sh | Ruby | Chef

Offline

Board footer

Powered by FluxBB