|
|
|||||||
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... |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
You can also use Exit in the mainscript - cause Kixtart knows what is mainline versus called script. If in mainline - exit totally. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
Makes sense. I like that code. What is EXIT 0? I know what exit means, but what does the zero do? |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
As for right now, your example is find. I'll keep in mind the UDF. Thanks |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
IMHO, Exit should always have a parm follow it. I wrote a FAQ on that topic. |
||||||||
|
|
|||||||
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" |
||||||||
|
|
|||||||
that would do the trick but would require you to remember that endif. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |