Terminating a script without jumping to the end

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
abellavia
Newbie
Posts: 3
Joined: Fri Jan 19, 2007 4:55 pm

Terminating a script without jumping to the end

Post by abellavia » Fri Jan 19, 2007 5:04 pm

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.
Andy B.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jan 19, 2007 5:24 pm

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

abellavia
Newbie
Posts: 3
Joined: Fri Jan 19, 2007 4:55 pm

Terminating a script without jumping to the end

Post by abellavia » Fri Jan 19, 2007 5:30 pm

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.

User avatar
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

Post by Marcus Tettmar » Fri Jan 19, 2007 5:35 pm

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

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?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jan 19, 2007 5:38 pm

If it helps get you out faster, you can put a label at the end of the subroutine that you "bolt" to after the failed logon test. All else in the method would remain the same. Just gets you out of the subroutine sooner.

abellavia
Newbie
Posts: 3
Joined: Fri Jan 19, 2007 4:55 pm

Terminating a script without jumping to the end

Post by abellavia » Fri Jan 19, 2007 5:44 pm

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.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts