goto

The goto command allows you to redirect the order that commands in a script file are executed. It allows you to skip to another location in a script file rather than executing the next command sequentially. The goto command is particularly useful with if and until commands to create conditional branches in a script.

The goto command should be used in this format:
 

goto label

where label is a special label or "tag" that you insert at the location you want to jump to. For example:
 

goto stop'everything
goto step2

In these examples, you would need to insert the labels:
 

stop'everything:
and
step2:

at the appropriate locations. The label itself must be the first nonblank character on a line and must end with a colon, although you don't include the colon when you reference the label in a goto command.

Here's a small sample script file that uses two goto commands:

   dial 555-1212               ; dial remote system
   if err0 # 0 goto no'connect ; if no answer, jump to new location
   say "name^M"                ; log on
   goto logged'on              ; jump to next step
no'connect:
   :<No answer or busy. Try again later.>
   finish
   :X
logged'on:
   say "log dsk7:[77,7]^M"
Notice that if the dial results in an error (such as no answer), we skip to the no'connect label. The next command that would execute would be the colon prompt :< > command that would display the message No answer or busy. Try again later.

If the dial command results in a connection with the remote modem, the next say command logs us on, then another goto command lets the script file skip over the no'connect part of the file and jump directly to the logged'on label. The next command that executes in this case is the say command that logs us to the directory DSK7:[77,7] on the remote system.

goto can jump to a label that appears before or after the current location in the script file. This means you can jump forward or backward, and can construct "loops" as in this example:

   set reg(1) = 0           ; we'll use this as a "counter" to try 3 times
loop:
   set reg(1) = reg(1) + 1  ; "bump up" the counter
   if reg(1) > 3 goto no'connect ; give up after 3rd try, jump out of loop
   dial 555-1212
   if err0 # 0 goto loop    ; if no answer, try again!
   goto carry'on            ; otherwise, skip "no connect" & carry on
no'connect:
   :<Sorry, no answer or busy. Please try again later.>
   finish
   :X
carry'on:
   ; remote modem answered, everything's okay, continue as normal
   ...
In this example, the command if err0 # 0 goto loop creates a series of commands that repeat until either the dial command results in a connection, or until we've tried three times without success (we use reg(1) to count).