tylan
(Starting to like KiXtart)
2007-07-11 07:57 PM
GOTO is bad, right?

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

ShawnAdministrator
(KiX Supporter)
2007-07-11 08:08 PM
Re: GOTO is bad, right?

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.


ShawnAdministrator
(KiX Supporter)
2007-07-11 08:11 PM
Re: GOTO is bad, right?

You can also use Exit in the mainscript - cause Kixtart knows what is mainline versus called script. If in mainline - exit totally.

tylan
(Starting to like KiXtart)
2007-07-11 08:17 PM
Re: GOTO is bad, right?

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




ShawnAdministrator
(KiX Supporter)
2007-07-11 08:22 PM
Re: GOTO is bad, right?

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


tylan
(Starting to like KiXtart)
2007-07-11 08:26 PM
Re: GOTO is bad, right?

Makes sense. I like that code.

What is EXIT 0? I know what exit means, but what does the zero do?


tylan
(Starting to like KiXtart)
2007-07-11 08:27 PM
Re: GOTO is bad, right?

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.


ShawnAdministrator
(KiX Supporter)
2007-07-11 08:31 PM
Re: GOTO is bad, right?

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


tylan
(Starting to like KiXtart)
2007-07-11 08:36 PM
Re: GOTO is bad, right?

As for right now, your example is find. I'll keep in mind the UDF.

Thanks


Glenn BarnasAdministrator
(KiX Supporter)
2007-07-11 09:30 PM
Re: GOTO is bad, right?

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


Les
(KiX Master)
2007-07-11 09:41 PM
Re: GOTO is bad, right?

IMHO, Exit should always have a parm follow it. I wrote a FAQ on that topic.

Witto
(MM club member)
2007-07-11 10:40 PM
Re: GOTO is bad, right?

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"


LonkeroAdministrator
(KiX Master Guru)
2007-07-11 10:59 PM
Re: GOTO is bad, right?

that would do the trick but would require you to remember that endif.

Witto
(MM club member)
2007-07-11 11:10 PM
Re: GOTO is bad, right?

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.

ShawnAdministrator
(KiX Supporter)
2007-07-11 11:27 PM
Re: GOTO is bad, right?

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.

Les
(KiX Master)
2007-07-12 04:06 AM
Re: GOTO is bad, right?

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.