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
: end
But 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 🙂
Best Answer
Here are the key features of Unix and the V6 shell that make
goto
work:seek
system call to move stdin's read/write offset, will be reading from, and moving the read/write offset of, the shell script.:
command is a command that does (almost) nothing, so one of its uses is as agoto
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"
andend: echo "hello"
, will try to run the commands:end
andend:
, which likely don't exist.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 togoto
, 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.cYou asked
You can. The
goto
command will check if stdin is from a terminal; if so, it will write an error message and exit.