forsyth20121205-174063a80cc13 years agoHistory
980 lines · 22.0 KB · Text
RawDownload
.TH SH 1.SH NAMEsh, builtin, exit, load, loaded, local, whatis, quote, run, set, unload, unquote \- command language.SH SYNOPSIS.B sh[.B -ilxvn][.B -c command][.I file[.I arg ...].SH DESCRIPTION.I Shis a programmable user level interface (a shell) for Inferno.It executes command lines read from a terminal or a file or, with the.B -cflag, from.IR sh 'sargument list. It can also be used to give programmable functionalityto Limbo modules (see.IR sh "" "" (2))..SS Command LinesA command line is a sequence of commands, separated by ampersands or semicolons.RB ( &or.BR ; ),terminated by a newline.The commands are executed in sequencefrom left to right..I Shdoes not wait for a command followed by.B &to finish executing before startingthe following command.Whenever a command followed by.B &is executed, its process id is assigned to the.I shvariable.BR $apid .Whenever a command.I notfollowed by.B &exits or is terminated, the.I shvariable.B $statusgets the process's wait message (see.IR prog (3));it will be the null string if the command was successful..PPA number-sign.RB ( # )and any following characters up to (but not including) the next newlineare ignored, except in quotation marks..SS Simple CommandsA simple command is a sequence of arguments interspersed with I/O redirections.If the first argument is the name of a.I shbuiltin or it is a braced command block (see.IR "Compound Commands",below), it is executed by.IR sh .If the first character of the name is a brace.RB ( { ),the shell tries to parse it and execute it as a braced command block;if the parsing fails, an exception is raised.Otherwise.I shlooks for an external program to execute..PPIf the name ends in.BR .dis ,.I shlooks for a Dis module of that name; otherwiseit tries first to find a Dis module ofthat name with.B .disappended and failing that, it looks foran executable file of the same name, which shouldbe a readable, executable script file.If the name does not start with a slash.RB ( / )or dot-slash.RB ( ./ ),then the name is first looked for relative to.BR /dis ,and then relative to the current directory.A Dis module will be executed only if itimplements the.B Commandinterface (see.IR sh (1));a script file will be executed only if itstarts with the characters.RB `` #! ''followed by the name of a file executableunder the rules above. In this case thecommand will be executed with any following arguments mentionedin the.B #!header, followed by the path of the script file,followed by any arguments originally given to the command..PPFor example, to execute the simple command.BR "ls" ,.I shwill look for one of the following things, in order,stopping the search when one is found:.RS.IP 1)a built-in command named.RB `` ls ''..IP 2)a Dis module named.RB `` /dis/ls.dis '',.IP 3)an executable script file named.RB `` /dis/ls '',.IP 4)a Dis module named.RB `` ./ls.dis '',.IP 5)an executable script file named.RB `` ./ls ''..RE.SS Arguments and VariablesA number of constructions may be used where.I sh'ssyntax requires an argument to appear.In many cases a construction'svalue will be a list of arguments rather than a single string..PPThe simplest kind of argument is the unquoted word:a sequence of one or more characters none of which is a blank, tab,newline, or any of the following:.EX	# ; & | ^ $ ` ' { } ( ) < > " =.EEAn unquoted word that contains any of the characters.B *.B ?.B [is a pattern for matching against file names.The character.B *matches any sequence of characters,.B ?matches any single character, and.BI [ class ]matches any character in the.IR class .If the first character of.I classis.BR ^ ,the class is complemented. (As this characteris special to the shell, it may only be included in a patternif this character is quoted, as long as the leading.B [is not quoted).The.I classmay also contain pairs of characters separated by.BR - ,standing for all characters lexically between the two.The character.B /must appear explicitly in a pattern.A pattern is replaced by a list of arguments, one for each path name matched,except that a pattern matching no names is not replaced by the empty list,but rather stands for itself.Pattern matching is done after all otheroperations.Thus,.EX	x=/tmp; echo $x^/*.b.EEmatches.BR /tmp/*.b ,rather than matching.B "/*.band then prefixing.BR /tmp ..PPA quoted word is a sequence of characters surrounded by single quotes.RB ( ' ).A single quote is represented in a quoted word by a pair of quotes.RB ( '' )..PP.ne 3Each of the following is an argument..PD 0.HP.BI ( arguments ).brThe value of a sequence of arguments enclosed in parentheses isa list comprising the members of each element of the sequence.Argument lists have no recursive structure, although their syntax maysuggest it.The following are entirely equivalent:.EX	echo hi there everybody	((echo) (hi there) everybody)	echo (hi	there	everybody	).EENewlines within parentheses count as simple white space;they do not terminate the command. This can be useful to givesome more freedom of layout to commands that take severalcommands as arguments, for instance several of the commandsdefined in.IR sh-std (1)..HP.BI $ argument.brThe.I argumentafter the.B $is the name of a variable whose value is substituted.Multiple levelsof indirection are possible.Variable valuesare lists of strings.If.I argumentis a number.IR n ,the value is the.IR n thelement of.BR $* ,unless.B $*doesn't have.I nelements, in which case the value is empty.Assignments to variables are described under.I "Assignment" ,below..HP.BI $# argument.brThe value is the number of elements in the named variable.A variablenever assigned a value has zero elements..HP\f5$"\fP\fIargument\fP.brThe value is a single string containing the components of the named variableseparated by spaces.  A variable with zero elements yields the empty string..HP.BI `{ command }.HP.I\f5"{\fPcommand\f5}\fP.br.I Shexecutes the.I commandand reads its standard output. If backquote.RB ( ` )is used, it is split into a list of arguments,using characters in.B $ifsas separators.If.B $ifsis not otherwise set, its value is.BR "'\ \et\en'" .If doublequote (\f5"\fP)is used, no tokenization takes place..HP.IB argument ^ argument.brThe.B ^operator concatenates its two operands.If the two operandshave the same number of components, they are concatenated pairwise.If not,then one operand must have one component, and the other must be non-empty,and concatenation is distributive..HP.BI ${ command }.br.I Commandmust be a simple command with no redirections;its first wordmust be the name of a builtin substitution operator.The operator is invoked and its value substituted.See.IR "Built-in Commands" ,below, for more information on builtins..HP.BI <{ command }.HP.BI >{ command }.brThe.I commandis executed asynchronously with its standard output or standard inputconnected to a pipe.The value of the argument is the name of a filereferring to the other end of the pipe.This allows the construction ofnon-linear pipelines.For example, the following runs two commands.B oldand.B newand uses.B cmpto compare their outputs.EX	cmp <{old} <{new}.EE.PD.SS Free CaretsIn most circumstances,.I shwill insert the.B ^operator automatically between words that are not separated by white space.Whenever one of.B $.B '.B `follows a quoted or unquoted word or an unquoted word follows a quoted wordwith no intervening blanks or tabs,a.B ^is inserted between the two.If an unquoted word immediately follows a.BR $ and contains a character other than an alphanumeric, underscore,or.BR * ,a.B ^is inserted before the first such character.Thus.IP.B limbo -$flags $stem.b.LPis equivalent to.IP.B limbo -^$flags $stem^.b.SS AssignmentA command of the form.IB name = valueor.IB name := valueassigns.I valueto the environment variable named.IR name ..I Valueis either a list of arguments or an assignment statement. Inthe latter case.I valueis taken from the value assigned in the assignment statement.If.B :=is used, the value is stored in the innermost local scope.A local scope is created every time a braced block is entered,and destroyed when the block is left. If.B =is used, the value is stored in the innermost scopethat contains any definition of.IR name ..PPA list of names can also be used in place of.IR name ,which causes each element of.I valuein turn to be assigned the respective variable name inthe list. The last variable in the list is assigned any elementsthat are left over. If there are more variable names thanelements in.IR value ,the remaining elements are assigned the null list.For instance, after the assignment:.EX	(a b c) = one two three four five.EE.B $ais.BR one ,.B $bis.BR two ,and.B $ccontains the remaining three elements.BR "(three four five)" ..SS I/O RedirectionsThe sequence.BI > fileredirects the standard output file (file descriptor 1, normally theterminal) to the named.IR file ;.BI >> fileappends standard output to the file.The standard input file (file descriptor 0, also normally the terminal)may be redirected from a file by the sequence.BI < file \f1,or by the sequence.BI <> file \f1,which opens the file for writing as well as reading.Note that if.I fileis in fact a parsed braced block, the redirection will be treated aspipe to the given command - it is identical to the.B "<{}"operator mentioned above..PPRedirections may be applied to a file-descriptor other than standard inputor output by qualifying the redirection operatorwith a number in square brackets.For example, the diagnostic output (file descriptor 2)may be redirected by writing.BR "limbo junk.b >[2] junk" ..PPA file descriptor may be redirected to an already open descriptor by writing.BI >[ fd0 = fd1 ]or.BI <[ fd0 = fd1 ]\f1..I Fd1is a previously opened file descriptor and.I fd0becomes a new copy (in the sense of .IR sys-dup (2))of it..PPRedirections are executed from left to right.Therefore,.B limbo junk.b >/dev/null >[2=1]and.B limbo junk.b >[2=1] >/dev/nullhave different effects: the first puts standard output in.BR /dev/nulland then puts diagnostic output in the same place, where the seconddirects diagnostic output to the terminal and sends standard output to.BR /dev/null ..SS Compound CommandsA pair of commands separated by a pipe operator.RB ( | )is a command.The standard output of the left command is sent through a pipeto the standard input of the right command.The pipe operator may be decoratedto use different file descriptors..BI |[ fd ]connects the output end of the pipe to file descriptor.I fdrather than 1..BI |[ fd0 = fd1 ]connects output to.I fd1of the left command and input to.I fd0of the right command..PPA sequence of commands separated by.BR & ,.BR ; ,or newlinemay be grouped by surroundingthem with braces.RB ( {} ),elsewhere referred to as a.IR "braced block" .A braced block may be used anywhere that a simple wordis expected. If a simple command is found witha braced block as its first word, thevariable.B $*is set to any following arguments,.B $0is set to the block itself, and the commandsare executed in sequence. If a braced blockis passed as an argument, no execution takes place:the block is converted to a functionally equivalentstring, suitable for later re-interpretation by the shell.The null command .RB ( {} )has no effect and always gives a nil status. For instancethe following commands all produce the same result:.EX	echo hello world

Lines 1–500 of 980.