Does anyone know a way to terminate a script (keeping MS running) without simply jumping to the end? I have a script which calls a series of subroutines. In one of those subroutines the script attempts to log on to a network resource. It is usually there, but sometimes not. In that case the logon subroutine does not get files needed later, so errors are generated as each new subroutine is called.
Because the need to terminate is in a subroutine I cannot simply jump to the end. If I could terminate the script directly in the subroutine that failed to log on to the network resource, all would be good. Thanks.
Terminating a script without jumping to the end
Moderators: JRL, Dorian (MJT support)
You can set a "flag" in your subroutine and test it immediately after returning to the main script. Something like:
SRT>LogOn
//do stuff
//Test for logon success, perhaps by testing for a file.
IfFileExists>%filename%
Let>logonFlag=1
Else
Let>LogonFlag=0
EndIF
END>LogOn
GoSub>LogOn
If>%LogOnFlag=0,end
//Other GoSubs
Label>end
SRT>LogOn
//do stuff
//Test for logon success, perhaps by testing for a file.
IfFileExists>%filename%
Let>logonFlag=1
Else
Let>LogonFlag=0
EndIF
END>LogOn
GoSub>LogOn
If>%LogOnFlag=0,end
//Other GoSubs
Label>end
Terminating a script without jumping to the end
Thanks, I thought of that too and can make it work. But it seems so inelegant compared to just bolting. Even after scripting for a couple of years it always baffled me that I could not find a simple script termination method. But I guess if it is not there, it is not there. Thanks again.
Andy B.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Re: Terminating a script without jumping to the end
Your view is contrary to most programmers I know! The general consensus amongst developers is that it is inelegant to "just bolt" and better to craft the logic to organize the flow.abellavia wrote:Thanks, I thought of that too and can make it work. But it seems so inelegant compared to just bolting. Even after scripting for a couple of years it always baffled me that I could not find a simple script termination method. But I guess if it is not there, it is not there. Thanks again.
My preference is for what JRL suggested - the subroutine should return a var indicating success or not, and the script should then branch accordingly.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Terminating a script without jumping to the end
OK I'm busted! I'm not a professional programmer, just a respectable amateur. The logon script already has several error tests with recovery branches which are fairly robust. But the script runs in the middle of the night and every now and again it will not log on. So under those conditions an exit (graceful or not) is called for, after first sending me a failure notice by email. My script does that now, but then marches through all the other subroutines. So opinions on elegance aside, I will go with the method suggested by JRL. Thanks for your reply.
Andy B.