Operating System - HP-UX
1847249 Members
2865 Online
110263 Solutions
New Discussion

Re: goto command in shell

 
SOLVED
Go to solution
andi_1
Frequent Advisor

goto command in shell

Hi guys,

Is there a goto command in shell? And if there is one, does anyone know how to use it?

Thank you!
10 REPLIES 10
Justo Exposito
Esteemed Contributor

Re: goto command in shell

Hi Andi,

As I know there is not a goto command in shell, but if you think there is not necessary to do the things that you want to do.

If you want I can help you with your script.

Regards,

Justo.
Help is a Beatiful word
James R. Ferguson
Acclaimed Contributor
Solution

Re: goto command in shell

Hi:

Sorry, no. However, in Posix shell (or 'ksh') you have 'break' to exit from a 'for' 'select', 'until', or 'while' loop, and 'continue' to resume the next iteration of the same (for, select, until, while); and 'return' to immediately exit a function.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: goto command in shell

There is no goto in the Bourne, Korn, or POSIX shell but csh does have one. You should be able to circumvent the need for a goto with a 'break' to exit from the enclosing while, until, for, or select loop.

If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: goto command in shell

As Edsgar Dijkstra pointed out in the 60's, the goto is harmful, which is one of the reasons that higher level languages accomplish tasks in a top down manner. Looping is handled with for/while/until and bailing out due to errors or special exits is accomplished with functions, traps and break or continue.

Nothing messes up easily understood code better than a goto.


Bill Hassell, sysadmin
Paula J Frazer-Campbell
Honored Contributor

Re: goto command in shell

Hi

As all have said no goto but calling a script from a script can be made to behave like a goto.

Paula
If you can spell SysAdmin then you is one - anon
James R. Ferguson
Acclaimed Contributor

Re: goto command in shell

Hi (again):

Bill made me chuckle, having been around awhile... Be glad for 'break' and 'continue' at least. I once developed some communication "algorithms" in a network language (NDLII on Burroughs (Unisys)) that not only lacked "goto" but also *any* way to break out of a loop short of setting a boolean variable, testing it and conditionally executing or not the next block of code.

Regards!

...JRF...
Jeff Schussele
Honored Contributor

Re: goto command in shell

Hi andi,

As Paula stated you can call another script from w/in a script - but keep in mind you cannot go back. And don't try to call back to the org script or you'll be in loop hell as it would go back to the start of the org script & hit the call, which hits the call in 2nd script, etc.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Tom Danzig
Honored Contributor

Re: goto command in shell

There is a goto in csh. Not recommended though. It's thought to be poor programming practice.
James R. Ferguson
Acclaimed Contributor

Re: goto command in shell

Hi:

...and the 'csh' is a poor practice shell... :-))

...JRF...
Ed Sampson
Frequent Advisor

Re: goto command in shell

One good way to branch to different logic is the case statement. An example can be found in the accounting script /usr/lib/acct/runacct.
It uses the case statement to restart accounting is if fails partway through the processing.