-   Linux - Software (
-   -   goto/label command for scripting in bash shell (

terry.trent 04-04-2004 01:16 PM

goto/label command for scripting in bash shell
i am writing a program for a class, and i've run into a road blcok. i'm told that there is a goto/label command for bash shell scripting, but i've yet been able to find it.

i know that it's not proper to use goto and label commands when writing programs, because of the danger newbiness of it, but this is something i need to be able to do if i'm going to write this program.

this is what i'd like to do basically:
line # command

1: echo something
2: echo something else
3: read else
4: else=$else
5: if [ $else=yes ]
6: then echo ok
7: elif [ $else=no ]
8: then goto line 1 (this would be where i would have the goto thingy)

could anyone help me with this? i'm still fairly new to linux/unix and scripting, but i'd like to get this working.

if i can't do this, there is another way to make the program work-i just don't want to go that route because it'd take longer, as well as take more effort (in my opinoin).

thanks in advance!

AutOPSY 04-04-2004 04:18 PM

thsi is a simple shell script, if you want to re return to the beginning, after all your if, else, and elif statements

have a then statemnet that re3executes the script , done.

I do not know of a way to return to the begginning except a more complex else/if blocks.

hw-tph 04-04-2004 04:37 PM

You could use functions in a more or less recursive way. Non-working example using your pseudo-code:

do_the_thing () {
    echo something
    echo something else
    read else
    if [ $else=yes ]
    then echo ok
    elif [ $else=no ]
    then do_the_thing
do_the_thing            # call the function above

Above the do_the_thing function does whatever it is you're doing and it is called at the bottom of the script so it will launch right away as soon as you launch it. It will call itself recursively until the elif at the end of the function doesn't return true.

fnds 07-09-2010 10:15 AM

In order to do what you want, put the code inside a loop and use the "break" command to stop, and the "continue" command to jump back to the beginning.

I modified your pseudo code below and added the shell constructs. The changed lines are marked with a "*"

while true
  1: echo something
  2: echo something else
  3: read else
  4: else=$else
  5: if [ $else=yes ]
  *6: then echo ok; break
  7: elif [ $else=no ]
  *8: then continue

All times are GMT -5. The time now is 09:11 AM.