#177798 - 2007-07-11 07:57 PM
GOTO is bad, right?
|
tylan
Starting to like KiXtart
Registered: 2005-11-17
Posts: 115
Loc: Johnstown, PA
|
I know that goto is frowned upon in these forums, so what's the "good coder" way of ending the script immediately? I want to make the script skip over 90% of the code, and resume with the last few lines of code for my admin accounts. I don't want printers installing on servers etc...
|
Top
|
|
|
|
#177799 - 2007-07-11 08:08 PM
Re: GOTO is bad, right?
[Re: tylan]
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
Yup goto is bad.
The answer depends on where you "are" in the script (the context/scope):
If you want to quit out of the mainline script - use the quit statement:
Break On
If 1=1
Quit
Else
?"Much stuff..."
?"Much stuff..."
?"Much stuff..."
Endif
Exit 0
If you want to exit out of a "called" script - dont use quit - use exit, like this:
Break On
If 1=1
Exit
Else
?"Much stuff..."
?"Much stuff..."
?"Much stuff..."
Endif
Exit 0
Hope this answers your question.
|
Top
|
|
|
|
#177802 - 2007-07-11 08:17 PM
Re: GOTO is bad, right?
[Re: tylan]
|
tylan
Starting to like KiXtart
Registered: 2005-11-17
Posts: 115
Loc: Johnstown, PA
|
I would like to do something like this, but still follow good coding practices...
IF InGroup("ADMIN_USERS")
GOTO NEAREND
ENDIF
?"Main script"
?"Blah blah blah"
?"etc etc etc"
?"Do stuff and things"
:NEAREND
SHELL "\\somerset-dhcp\audit\scan\scan32.exe"
:END
|
Top
|
|
|
|
#177803 - 2007-07-11 08:22 PM
Re: GOTO is bad, right?
[Re: tylan]
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
Yeah - I hear you. Its these cases that make one want to use goto. We have a similar situation with our login script, maybe do this:
;; Handle some short code for admins and exit early...
IF InGroup("ADMIN_USERS")
SHELL "\\somerset-dhcp\audit\scan\scan32.exe"
EXIT
ENDIF
;; else the bulk of script ...
?"Main script"
?"Blah blah blah"
?"etc etc etc"
?"Do stuff and things"
EXIT 0
|
Top
|
|
|
|
#177804 - 2007-07-11 08:26 PM
Re: GOTO is bad, right?
[Re: Shawn]
|
tylan
Starting to like KiXtart
Registered: 2005-11-17
Posts: 115
Loc: Johnstown, PA
|
Makes sense. I like that code.
What is EXIT 0? I know what exit means, but what does the zero do?
|
Top
|
|
|
|
#177805 - 2007-07-11 08:27 PM
Re: GOTO is bad, right?
[Re: tylan]
|
tylan
Starting to like KiXtart
Registered: 2005-11-17
Posts: 115
Loc: Johnstown, PA
|
Never mind. Went to kix help.
If EXIT is followed by a numeric expression, then @ERROR is set to the value of that expression and you can check it in the calling script or batch file.
|
Top
|
|
|
|
#177807 - 2007-07-11 08:31 PM
Re: GOTO is bad, right?
[Re: Shawn]
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
If your short-code isn't really that short - but still want to avoid gotos - you can create a FUNCTION (UDF) in your script just for admins, then call it from your IF at the top ... like this:
IF InGroup("ADMIN_USERS")
AdminLogin()
EXIT
ENDIF
;; much user stuff ...
Function AdminLogin()
;; much admin stuff
EndFunction
|
Top
|
|
|
|
#177808 - 2007-07-11 08:36 PM
Re: GOTO is bad, right?
[Re: Shawn]
|
tylan
Starting to like KiXtart
Registered: 2005-11-17
Posts: 115
Loc: Johnstown, PA
|
As for right now, your example is find. I'll keep in mind the UDF.
Thanks
|
Top
|
|
|
|
#177822 - 2007-07-11 10:40 PM
Re: GOTO is bad, right?
[Re: Les]
|
Witto
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
What about
IF Not InGroup("ADMIN_USERS")
?"Main script"
?"Blah blah blah"
?"etc etc etc"
?"Do stuff and things"
ENDIF
SHELL "\\somerset-dhcp\audit\scan\scan32.exe"
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 248 anonymous users online.
|
|
|