Hey :) I am trying to understand the command goto. I have some questions regarding the goto manual. Here you can find the original manual: http://man.cat-v.org/unix-6th/1/goto

1. Goto is allowed only when t̲h̲e̲ S̲h̲e̲l̲l̲ i̲s̲ t̲a̲k̲i̲n̲g̲ c̲o̲m̲m̲a̲n̲d̲s̲ f̲r̲o̲m̲ a̲ f̲i̲l̲e̲.

What does the underlined part mean? I'm not allowed to type goto in the shell? But if goto is written into a file everything is okay?

2. The file is searched from the beginning for a line beginning with ':' followed by one or more spaces followed by the label.

So if a part of my file looks like this

goto end

Then he would look for a part like this : endBut I have seen some scripts and I think they were from Unix v6, it looked everytime like this end: So what is now the right order?

3. If such a line is found, the goto command returns. Since the read pointer in the command file points to the line after the label, the effect is to cause the Shell to transfer to the labelled line.

Is it legal to write any other command in the line with the ":"? For example :end echo "Hello" or end: echo "Hello"

Thank you for your help and Merry Christmas :)

  • It means that goto is only allowed in shell scripts, not when typing to the shell interactively. I don't know the answer to your other questions. Do you have a v6 system you can test on? – Barmar Dec 26 '14 at 2:57

Here are the key features of Unix and the V6 shell that make goto work:

  • When a process forks, all of its open file descriptors are shared between parent and child.
  • This means that any repositioning of the file read/write offset by any process will be reflected in all processes that have the same shared file descriptor.
  • The V6 shell reads a shell script one character at a time, from its standard input, with no buffering.
  • When the V6 shell is running a script, any process created by the V6 shell that reads from stdin, or uses the seek system call to move stdin's read/write offset, will be reading from, and moving the read/write offset of, the shell script.
  • The shell's : command is a command that does (almost) nothing, so one of its uses is as a goto target. Aside from checking arguments for syntax errors (and, in modern shells, expansion of shell variables, which may cause side effects), nothing is done with its arguments.
    : > file is a way to truncate a file.
    : end echo "hello" will not output anything. Those other examples,
    :end echo "hello" and end: echo "hello", will try to run the commands :end and end:, which likely don't exist.
  • The goto command sets the read/write offset of stdin to 0 - the beginning of the shell script - and reads stdin looking for a line that begins with a :, one or more blanks, the argument passed to goto, and a blank or newline. When it finds a match, it exits, leaving the read/write offset to be just after that line, and that is where the shell will resume executing the script.

You can see the source for goto here: http://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s1/goto.c

You asked

I'm not allowed to type goto in the shell?

You can. The goto command will check if stdin is from a terminal; if so, it will write an error message and exit.

  • Were lines beginning with : (and whitespace) true comments (like lines beginning with #) or was : treated as a null command? For instance, ISTR (1) : echo "hello" > foo would create an empty file named foo, (2) : ) would be a syntax error, and : label; echo "hello" would write hello to stdout. – Scott Dec 26 '14 at 21:14
  • @Scott The sh(1) man page just says : is simply ignored, but it was as you said, a null command, which could be part of a compound command and have i/o redirection, rather than a comment like the modern # character. I'll fix my answer so it doesn't used the term comment. Thanks. goto does swallow up an entire line after it finds a matching label, so putting anything more than a label after : may lead to unexpected results if it's a target of a goto. – Mark Plotnick Dec 26 '14 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.