From: dik@cwi.nl (Dik T. Winter)
Newsgroups: alt.folklore.computers
Subject: Mashey Shell (longish article)

There were a few people that wanted information about the Mashey shell.`
I already gave a short summary, here follow the man pages for sh(I),
exit(I), goto(I), if(I) and shift(I).
Typed in from an original edition of Unix Programmer's Manual, Fifth
Edition, K. Thompson and D. M. Ritchie.  Dated: June, 1974.
Most typos are probably mine (and the program typo is no longer
available ;-)).  The inconsequent use of "file name" vs. "file-name"
is from the document, as is the missing of a pipe symbol in the
example for quoting.  Also the singular singular process in the section
on command file errors is from the document.  I have suspicions about the
description of file globbing, but can not test and the man page for
glob(VIII) is not in the manual.

---------------------------------------------------------------------
SH(1)				5/15/74				SH(1)

NAME
	sh - shell (command interpreter)

SYNOPSIS
	sh [ name [ arg1 ... [ arg9 ] ] ]

DESCRIPTION
	Sh is the standard command interpreter.  It is the program which
	reads and arranges the execution of the command lines typed by
	most users.  It may itself be called as a command to interpret
	files of commands.  Before discussing the arguments to the Shell
	used as a command, the structure of command lines themselves will
	be given.

	Commands.  Each command is a sequence of non-blank command
	arguments separated by blanks.  The first argument specifies the
	name of a command to be executed.  Except for certain types of
	special arguments discussed below, the arguments other than the
	command name are passed without interpretation to the invoked
	command.

	If the first argument is the name of an executable file, it is
	invoked; otherwise the string '/bin/' is prepended to the
	argument.  (In this way most standard commands which reside in
	'/bin' are found.)  If no such command is found, the string
	'/usr' is further prepended (to give '/usr/bin/command') and
	annother attempt is made to execute the resulting file.  (Certain
	lesser used commands live in '/usr/bin'.)  If the '/usr/bin' file
	exists, but is not executable, it is used by the Shell as a
	command file.  That is to say it is executed as though it were
	typed from the console.  If all attempts fail, a diagnostic is
	printed.

	Command lines.  One or more commands separated by '|' or '^'
	constitute a chain of filters.  The standard output of each
	command but the last is taken as the standard input of the next
	command.  Each command is run as a separate process, connected
	by pipes (see pipe(II)) to its neighbors.  A command line contained
	in parenthesis '( )' may appear in place of a single command as
	a filter.

	A command line consists of one or more pipelines separated, and
	perhaps terminated by ';' or '&'.  The semicolon designates
	sequential execution.  The ampersand causes the preceding pipeline
	to be executed without waiting for it to finish.  The process id of
	such a pipeline is reported, so that it may be used if necessary
	for a subsequent wait or kill.

	Termination Reporting.  If a command (not followed by '&')
	terminates abnormally, a message is printed.  (All terminations
	other than exit and interrupt are considered abnormal.)
	Termination reports for commands followed by '&' are given upon
	receipt of the first command subsequent to the termination of the
	command, or when a wait is executed.  The following is a list of
	the abnormal termination messages:
		Bus error
		Trace/BPT trap
		Illegal instruction
		IOT trap
		EMT trap
		Bad system call
		Quit
		Floating exception
		Memory violation
		Killed
	If a core image is produced, '- Core dumped' is appended to the
	appropriate message.

	Redirection of I/O.  There are three character sequences that
	cause the immediately following string to be interpreted as a
	special argument to the Shell itself.  Such an argument may appear
	anywhere among the arguments of a simple command, or before or
	after a parenthesized command list, and is associated with that
	command or command list.

	An argument of the form '<arg' causes the file 'arg' to be used
	as the standard input (file descriptor 0) of the associated
	command.

	An argument of the form '>arg' causes file 'arg' to be used as
	the standard output (file descriptor 1) for the associated
	command.  'Arg' is created if it did not exist, and in any case
	truncated at the outset.

	An argument of the form '>>arg' cases file 'arg' to be used as
	the standard output for the associated command.  If 'arg' did not
	exist, it is created; if it did exist, the command output is
	appended to the file.

	For example, either of the command lines
		ls >junk; cat tail >>junk
		(ls; cat tail) >junk
	creates, on file 'junk', a listing of the working directory,
	followed immediately by the contents of file 'tail'.

	Either of the constructs '>arg' or '>>arg' associated with any
	but the last command of a pipeline is ineffectual, as is '<arg'
	in any but the first.

	In commands called by the Shell, file descriptor 2 refers to
	the standard output of the Shell before any redirection.  Thus
	filters may write diagnostics to a location where they have a
	chance to be seen.

	Generation of argument lists.  If any argument contains any of
	the characters '?', '*' or '[', it is treated specially as
	follows.  The current directory is searched for files which
	match the given argument.

	The character '*' in an argument matches any string of characters
	in a file name (including the null string).

	The character '?' matches any single character in a file name.

	Square brackets '[...]' specify a class of characters which
	matches any single file-name character in the class.  Within
	the brackets, each ordinary character is taken to be a member
	of the class.  A pair of characters separated by '-' places in
	the class each character lexically greater than or equal to the
	first and less than or equal to the second member of the pair.

	Other characters match only the same character in the file name.

	For example, '*' matches all file names; '?' matches all one-
	character file names; '[ab]*.s' matches all file names beginning
	with 'a' or 'b' and ending in '.s'; '?[zi-m]' matches all two-
	character file names ending with 'z or the letters 'i' through
	'm'.

	If the argument with '*' or '?' also contains a '/', a slightly
	different procedure is used; instead of the current directory,
	the directory used is the one obtained by taking the argument
	up to the last '/' before a '*' or '?'.    The matching process
	matches the remainder of the arguments after this '/' against
	the files in the derived directory.  For example: '/usr/dmr/a*.s'
	matches all files in directory '/usr/dmr' which begin with 'a'
	and end with '.s'.

	In any event, a list of names is obtained which match the argument.
	This list is sorted into alphabetical order, and the resulting
	sequence of arguments replaces the single argument containing the
	'*', '[' or '?'.  The same process is carried out for each
	argument (the resulting lists are not merged) and finally the
	command is called with the resulting list of arguments.

	For example: directory /usr/dmr contains the files a1.s, a2.s,
	..., a9.s.  From any directory, the command
		as /usr/dmr/a?.s
	calls as with arguments /usr/dmr/a1.s, /usr/dmr/a2.s, ...,
	/usr/dmr/a9.s in that order.

	Quoting.  The character '\' causes the immediately following
	character to lose any special meaning it may have to the Shell;
	in this way '<', '>', and other characters meaningful to the
	Shell may be passed as part of arguments.  A special case of
	this feature allows the continuation of commands onto more than
	one line: a new-line preceded by '\' is translated into a blank.

	Sequences of characters enclosed in double (") or single (')
	quotes are also taken literally.  For example:
		ls  pr -h "My directory"
	causes a dierectory listing to be produced by ls, and passed on
	to pr to be printed with the heading 'My directory'.  Quotes
	permit the inclusion of blanks in the heading, which is a single
	argument to pr.

	Argument passing.  When the Shell is invoked as a command, it
	has additional processing capabilities.  Recall that the form
	in which the Shell is invoked is
		sh [ name [ arg1 ... [ arg9 ] ] ]
	The name is the name of a file which will be read and interpreted.
	If not given, this subinstance of the Shell will continue to read
	from the standard input file.

	In command lines in the file (not in command input), character
	sequences of the form '$n', where n is a digit, are replaced by
	the nth argument to the invocation of the Shell (argn).  '$0' is
	replaced by name.

	End of file.  An end-of-file in the Shell's input causes it to
	exit.  A side effect of this fact means that the way to log out
	from UNIX is to type an EOT.

	Special commands.  The following commands are treated specially
	by the Shell.

	chdir is done without spawning a new process by executing sys
	chdir(II).

	login is done by executing /bin/login without creating a new
	process.

	wait is done without spawning a new process by executing sys
	wait(II).

	shift is done by manipulating the arguments to the Shell.

	':' is simply ignored.

	Command file errors: interrupts.  Any Shell-detected error, or
	an interrupt signal, during the execution of a command file
	causes the Shell to cease execution of that file.

	Process that are created with a '&' ignore interrupts.  Also
	if such a process has not redirected its input with a '<', its
	input is automatically redirected to the zero length file
	/dev/null.

FILES
	/etc/glob, which interprets '*', '?' amd '['.
	/dev/null as source of end-of-file.

SEE ALSO
	'The UNIX Time-sharing System', which gives the theory of
	operation of the Shell.
	chdir(I), login(I), wait(I), shift(I)

BUGS
	There is no way to redirect the diagnostic output.
---------------------------------------------------------------------
EXIT(1)				3/15/72			      EXIT(1)

NAME
	exit - terminate command file

SYNOPSIS
	exit

DESCRIPTION
	Exit performs a seek to the end of its standard input file.  Thus,
	if it is invoked inside a file of commands, upon return from exit
	the shell will discover an end-of-file and terminate.

SEE ALSO
	if(I), goto(I), sh(I)

BUGS
---------------------------------------------------------------------
GOTO(1)				3/15/72			      GOTO(1)

NAME
	goto - command transfer

SYNOPSIS
	goto label

DESCRIPTION
	Goto is only allowed when the Shell is taking commands from a file.
	The file is searched from the beginning for a line beginning with
	':' followed by one or more spaces followed by the label.  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 a do-nothing command that is ignored by the Shell and only
	serves to place a label.

SEE ALSO
	sh(I)

BUGS
---------------------------------------------------------------------
IF(1)				5/2/74				IF(1)

NAME
	if - conditional command

SYNOPSIS
	if expr command [ arg ... ]

DESCRIPTION
	If evaluates the expression expr, and if its value is true,
	executes the given command with the given arguments.

	The following primitives are used to construct the expr:

	-r file		true if the file exists and is readable.

	-w file		true if the file exists and is writable.

	s1 = s2		true if the strings s1 and s2 are equal.

	s1 != s2	true if the strings s1 and s2 are not equal.

	{ command }	The bracketed command is executed to obtain the
			exit status.  Status zero is considered true.
			The command must not be another if.

	These primaries may be combined with the following operators:

	!		unary negation operator

	-a		binary and operator

	-o		binary or operator

	( expr )	parentheses for grouping.

	-a has higher precedence than -o.  Notice that all operators
	and flags are separate arguments to if and hence must be
	surrounded by spaces.  Notice also that parentheses are
	meaningful to the Shell and must be escaped.

SEE ALSO
	sh(I), find(I)

BUGS
---------------------------------------------------------------------
SHIFT(1)			8/21/73			     SHIFT(1)

NAME
	shift - adjust Shell arguments

SYNOPSIS
	shift

DESCRIPTION
	Shift is used in Shell command files to shift the argument list
	left by 1, so that old $2 can now be referred to by $1 and so
	forth.  Shift is useful to iterate over several arguments to a
	command file.  For example, the command file
		: loop
		if $1x = x exit
		pr -3 $1
		shift
		goto loop
	prints each of its arguments in 3-column format.

	Shift is executed within the Shell.

SEE ALSO
	sh(I)

BUGS
---------------------------------------------------------------------
-- 
