LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting "skip to"? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-skip-to-587264/)

WingnutOne 09-25-2007 11:59 AM

Bash scripting "skip to"?
 
I grew up with ancient Commodore64 BASIC and really miss the GOTO command. Is there anything similar in BASH?

For instance, in a bash script, is there a way to tell it to:
Code:

if condition X exists
then
skip to point Y within this script and continue working from that point



And incidentally, is there a more appropriate Forum for non-bug-report questions about BASH and other programs typically included in major Linux distributions? Most of the questions in "Linux - Software" seem a bit heavier than what I've been asking about...

David the H. 09-25-2007 01:03 PM

I'm not a programmer, but from what I've read, GOTO's are generally considered poor programming these days, and should be avoided whenever possible.

A better option, as I understand it, is to set up shell functions that you can import into the 'if' loops as needed. linuxcommand.org has an easy-to-understand explanation of them here and here.

As for bash-related questions, I've generally gotten the best advice simply from the general linux forum. There's also the programming forum, which I guess might be better for advanced scripting questions. The forums all have clear descriptions listed, so simply choose the one that sounds like it best matches your question. If you're wrong, the moderators will simply move it to where it should be.

bigrigdriver 09-25-2007 02:12 PM

I think the 'case' construct is what you need. In the use of case, you test for a condition, then code the action to take if that condition exists.

b0uncer 09-25-2007 02:27 PM

Quote:

I'm not a programmer, but from what I've read, GOTO's are generally considered poor programming these days, and should be avoided whenever possible.
This is mostly true, but there are some situations where 'goto' really comes in handy. It's "problem" is that it often causes the code to be difficult to understand because of sudden jumps..however in right places it's a handy tool, and since it can be used to ease things up (with well designed code, of course), learning to use it shouldn't be avoided. I tend to think it's a way of programming that shouldn't be used right from the beginning, but after one has learned more and knows "the way around". In most places a "goto structure" can be replaced with other means, like 'case' for example, but that's still not a reason to completely abandon it.

osor 09-25-2007 03:00 PM

I remember having this conversation before


All times are GMT -5. The time now is 12:58 AM.