Page 1 of 1 1
Topic Options
#177798 - 07/07/11 07:57 PM GOTO is bad, right?
tylan Offline
Starting to like KiXtart

Registered: 05/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 - 07/07/11 08:08 PM Re: GOTO is bad, right? [Re: tylan]
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
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:

 Code:
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:

 Code:
Break On

If 1=1

 Exit

Else

 ?"Much stuff..."
 ?"Much stuff..."
 ?"Much stuff..."

Endif

Exit 0


Hope this answers your question.

Top
#177800 - 07/07/11 08:11 PM Re: GOTO is bad, right? [Re: Shawn]
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
You can also use Exit in the mainscript - cause Kixtart knows what is mainline versus called script. If in mainline - exit totally.
Top
#177802 - 07/07/11 08:17 PM Re: GOTO is bad, right? [Re: tylan]
tylan Offline
Starting to like KiXtart

Registered: 05/11/17
Posts: 115
Loc: Johnstown, PA
I would like to do something like this, but still follow good coding practices...

 Code:
 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 - 07/07/11 08:22 PM Re: GOTO is bad, right? [Re: tylan]
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
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:

 Code:

;; 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 - 07/07/11 08:26 PM Re: GOTO is bad, right? [Re: Shawn]
tylan Offline
Starting to like KiXtart

Registered: 05/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 - 07/07/11 08:27 PM Re: GOTO is bad, right? [Re: tylan]
tylan Offline
Starting to like KiXtart

Registered: 05/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 - 07/07/11 08:31 PM Re: GOTO is bad, right? [Re: Shawn]
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
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:

 Code:
IF InGroup("ADMIN_USERS")
 AdminLogin()
 EXIT
ENDIF

;; much user stuff ...

Function AdminLogin()

 ;; much admin stuff

EndFunction

Top
#177808 - 07/07/11 08:36 PM Re: GOTO is bad, right? [Re: Shawn]
tylan Offline
Starting to like KiXtart

Registered: 05/11/17
Posts: 115
Loc: Johnstown, PA
As for right now, your example is find. I'll keep in mind the UDF.

Thanks

Top
#177812 - 07/07/11 09:30 PM Re: GOTO is bad, right? [Re: tylan]
Glenn Barnas Moderator Offline
KiX Supporter
*****

Registered: 03/01/28
Posts: 4211
Loc: New Jersey
Just FYI

Exit # is the "return code" - it should always be zero unless an error occurred, as it sets the @ERROR macro. This is valid only for user defined functions, or exiting the main script.

If you define a UDF called MyUdf(), you can also set a "return value" by loading the data you want to retun into a variable the same name as the UDF. Thus, if you want to return "42" from your MyUdf calculations, you would put this at the end of your UDF:
 Code:
$MyUdf = 42
Exit 0

However, if the calculation failed, you might do this at the point you detected the error:
 Code:
$MyUdf = 0
Exit 1

Exiting with 1 is "incorrect function" - a generic error. You can use many other, more meaningful error values. Some common ones are
2 - File Not Found
5 - Access Denied
13 - The data is invalid
50 - the request is not supported
87 - The parameter is incorrect

Glenn
_________________________

Actually I am a Rocket Scientist! \:D

Top
#177814 - 07/07/11 09:41 PM Re: GOTO is bad, right? [Re: Glenn Barnas]
Les Offline
KiX Master
*****

Registered: 01/06/11
Posts: 12734
Loc: fortfrances.on.ca
IMHO, Exit should always have a parm follow it. I wrote a FAQ on that topic.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#177822 - 07/07/11 10:40 PM Re: GOTO is bad, right? [Re: Les]
Witto Offline
MM club member
*****

Registered: 04/09/29
Posts: 1828
Loc: Belgium
What about
 Code:
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
#177823 - 07/07/11 10:59 PM Re: GOTO is bad, right? [Re: Witto]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 01/06/05
Posts: 22294
Loc: Claremore, OK
that would do the trick but would require you to remember that endif.
_________________________
!

download KiXnet

Top
#177826 - 07/07/11 11:10 PM Re: GOTO is bad, right? [Re: Lonkero]
Witto Offline
MM club member
*****

Registered: 04/09/29
Posts: 1828
Loc: Belgium
I do not see the difference. The "EndIf" was there. I just added the "Not". By inverting the logic, the Goto is not needed anymore.
Top
#177830 - 07/07/11 11:27 PM Re: GOTO is bad, right? [Re: Witto]
Shawn Administrator Offline
Administrator
*****

Registered: 99/08/13
Posts: 8608
I think he wanted to keep the bulk of his script non-indented. You know - too many wasted bytes and (more importantly) those darn long-lines don't need any help getting longer.
Top
#177842 - 07/07/12 04:06 AM Re: GOTO is bad, right? [Re: Shawn]
Les Offline
KiX Master
*****

Registered: 01/06/11
Posts: 12734
Loc: fortfrances.on.ca
I only indent by two spaces in my scripts. ASE has the vertical indentation lines to help keep track of the indents. Only prob is they are such a light grey that I have to tilt my LCD screen to see them.

Then they have that vertical line at column 83 which is great for knowing where to break those long lines.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Chris S., Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (green78) and 144 anonymous users online.
Newest Members
Keris2112, Jorromans, qrstuv140696, Bloodboy, NIJAKIX
17451 Registered Users

Generated in 0.206 seconds in which 0.148 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org