1589 lines · 33.6 KB · Text
RawDownload
.HTML "Rc — The Plan 9 Shell.	\" /*% refer -k -e -n -l3,2 -s < % | tbl | troff -ms | lp -dfn.Tm shell programming language	g.de TP	\" An indented paragraph describing some command, tagged with the command name.IP "\\f(CW\\$1\\fR" 5.if \\w'\\f(CW\\$1\\fR'-4n .br...de CI.nr Sf \\n(.f\%\&\\$3\f(CW\\$1\fI\&\\$2\f\\n(Sf...TLRc \(em The Plan 9 Shell.AUTom Dufftd@plan9.bell-labs.com.AB.I Rcis a command interpreter for Plan 9 thatprovides similar facilities to UNIX'sBourne shell,with some small additions and less idiosyncratic syntax.This paper uses numerous examples to describe.I rc 'sfeatures, and contrasts.I rcwith the Bourne shell, a model that many readers will be familiar with..AE.NHIntroduction.PP.I Rcis similar in spirit but different in detail from UNIX'sBourne shell.  This paper describes.I rc 'sprincipal features with many small examples and a few larger ones.It assumes familiarity with the Bourne shell..NHSimple commands.PPFor the simplest uses.I rchas syntax familiar to Bourne-shell users.All of the following behave as expected:.P1datecat /lib/news/buildwho >user.nameswho >>user.nameswc <fileecho [a-f]*.cwho | wcwho; datevc *.c &mk && v.out /*/bin/fb/*rm -r junk || echo rm failed!.P2.NHQuotation.PPAn argument that contains a space or one of.I rc 'sother syntax characters must be enclosed in apostrophes.CW ' ): (.P1rm 'odd file name'.P2An apostrophe in a quoted argument must be doubled:.P1echo 'How''s your father?'.P2.NHPatterns.PPAn unquoted argument that contains any of the characters.CW *.CW ?.CW [is a pattern to be matched against file names.A.CW *character matches any sequence of characters,.CW ?matches any single character, and.CW [\fIclass\fP]matches any character in the.CW class ,unless the first character of.I classis.CW ~ ,in which case the class is complemented.The.I classmay also contain pairs of characters separated by.CW - ,standing for all characters lexically between the two.The character.CW /must appear explicitly in a pattern, as must the path name components.CW .and.CW .. .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;rather it stands for itself..NHVariables.PPUNIX's Bourne shell offers string-valued variables..I Rcprovides variables whose values are lists of arguments \(emthat is, arrays of strings.  This is the principal differencebetween.I rcand traditional UNIX command interpreters.Variables may be given values by typing, for example:.P1path=(. /bin)user=tdfont=/lib/font/bit/pelm/ascii.9.font.P2The parentheses indicate that the value assigned to.CW pathis a list of two strings. The variables.CW userand.CW fontare assigned lists containing a single string..PPThe value of a variable can be substituted into a command bypreceding its name with a.CW $ ,like this:.P1echo $path.P2If.CW pathhad been set as above, this would be equivalent to.P1echo . /bin.P2Variables may be subscripted by numbers or lists of numbers,like this:.P1echo $path(2)echo $path(2 1 2).P2These are equivalent to.P1echo /binecho /bin . /bin.P2There can be no space separating the variable's name from theleft parenthesis; otherwise, the subscript would be considereda separate parenthesized list..PPThe number of strings in a variable can be determined by the.CW $#operator.  For example,.P1echo $#path.P2would print 2 for this example..PPThe following two assignments are subtly different:.P1empty=()null=''.P2The first sets.CW emptyto a list containing no strings.The second sets.CW nullto a list containing a single string,but the string contains no characters..PPAlthough these may seem like more or lessthe same thing (in Bourne's shell, they areindistinguishable), they behave differentlyin almost all circumstances.Among other things.P1echo $#empty.P2prints 0, whereas.P1echo $#null.P2prints 1..PPAll variables that have never been set have the value.CW () ..PPOccasionally, it is convenient to treat a variable's valueas a single string.  The elements of a string are concatenatedinto a single string, with spaces between the elements, bythe.CW $"operator.Thus, if we set.P1list=(How now brown cow)string=$"list.P2then both.P1echo $list.P2and.P1echo $string.P2cause the same output, viz:.P1How now brown cow.P2but.P1echo $#list $#string.P2will output.P14 1.P2because.CW $listhas four members, but.CW $stringhas a single member, with three spaces separating its words..NHArguments.PPWhen.I rcis reading its input from a file, the file has accessto the arguments supplied on.I rc 'scommand line.  The variable.CW $*initially has the list of arguments assigned to it.The names.CW $1 ,.CW $2 ,etc. are synonyms for.CW $*(1) ,.CW $*(2) ,etc.In addition,.CW $0is the name of the file from which.I rc 'sinput is being read..NHConcatenation.PP.I Rchas a string concatenation operator, the caret .CW ^ ,to build arguments out of pieces..P1echo hully^gully.P2is exactly equivalent to.P1echo hullygully.P2Suppose variable.CW icontains the name of a command.Then.P1vc $i^.cvl -o $1 $i^.v.P2might compile the command's source code, leaving theresult in the appropriate file..PPConcatenation distributes over lists. The following.P1echo (a b c)^(1 2 3)src=(main subr io)cc $src^.c.P2are equivalent to.P1echo a1 b2 c3cc main.c subr.c io.c.P2In detail, the rule is: if both operands of.CW ^are lists of the same non-zero number of strings, they are concatenatedpairwise.  Otherwise, if one of the operands is a single string,it is concatenated with each member of the other operand in turn.Any other combination of operands is an error..NHFree carets.PPUser demand has dictated that.I rcinsert carets in certain places, to make the syntaxlook more like the Bourne shell.  For example, this:.P1cc -$flags $stems.c.P2is equivalent to.P1cc -^$flags $stems^.c.P2In general,.I rcwill insert.CW ^between two arguments that are not separated by white space.Specifically, whenever one of.CW "$'`follows a quoted or unquoted word, or an unquoted word followsa quoted word with no intervening blanks or tabs, an implicit.CW ^is inserted between the two.  If an unquoted word immediately following a.CW $contains a character other than an alphanumeric, underscore or.CW * ,a.CW ^is inserted before the first such character..NHCommand substitution.PPIt is often useful to build an argument list from the output of a command..I Rcallows a command, enclosed in braces and preceded by a left quote,.CW "`{...}" ,anywhere that an argument is required.  The command is executed and itsstandard output captured.The characters stored in the variable.CW ifsare used to split the output into arguments.For example,.P1cat `{ls -tr|sed 10q}.P2will concatenate the ten oldest files in the current directory in temporal order, given thedefault.CW ifssetting of space, tab, and newline..NHPipeline branching.PPThe normal pipeline notation is general enough for almost all cases.Very occasionally it is useful to have pipelines that are not linear.Pipeline topologies more general than trees can require arbitrarily large pipe buffers,or worse, can cause deadlock..I Rchas syntax for some kinds of non-linear but treelike pipelines.For example,.P1	cmp <{old} <{new}.P2will regression-test a new version of a command..CW <or.CW >followed by a command in braces causes the command to be run withits standard output or input attached to a pipe.  The parent command.CW cmp "" (in the example)is started with the other end of the pipe attached to some file descriptoror other, and with an argument that will connect to the pipe when opened(e.g.,.CW /dev/fd/6 ).Some commands are unprepared to deal with input files that turn out not to be seekable.For example.CW diffneeds to read its input twice..NHExit status.PPWhen a command exits it returns status to the program that executed it.On Plan 9 status is a character string describing an error condition.On normal termination it is empty..PP.I Rccaptures command exit status in the variable.CW $status .For a simple command the value of.CW $statusis just as described above.  For a pipeline.CW $statusis set to the concatenation of the statuses of the pipeline components with.CW |characters for separators..PP.I Rchas a several kinds of control flow,many of them conditioned by the status returned from previouslyexecuted commands.  Any.CW $statuscontaining only.CW 0 'sand.CW | 'shas boolean value.I true .Any other status is.I false ..NHCommand grouping.PPA sequence of commands enclosed in.CW {}may be used anywhere a command is required.For example:.P1{sleep 3600;echo 'Time''s up!'}&.P2will wait an hour in the background, then print a message.Without the braces,.P1sleep 3600;echo 'Time''s up!'&.P2would lock up the terminal for an hour,then print the message in the background..NHControl flow \(em \f(CWfor\fP.PPA command may be executed once for each member of a listby typing, for example:.P1for(i in printf scanf putchar) look $i /usr/td/lib/dw.dat.P2This looks for each of the words.CW printf ,.CW scanfand.CW putcharin the given file.The general form is.P1for(\fIname\fP in \fIlist\fP) \fIcommand\fP.P2or.P1for(\fIname\fP) \fIcommand\fP.P2In the first case.I commandis executed once for each member of.I listwith that member assigned to variable.I name .If the clause.CW in "" ``.I list ''is missing,.CW in "" ``.CW $* ''is assumed..NHConditional execution \(em \f(CWif\fP.PP.I Rcalso provides a general if-statement.  For example:.P1for(i in *.c) if(cpp $i >/tmp/$i) vc /tmp/$i.P2runs the C compiler on each C source program thatcpp processes without error.An `if not' statement provides a two-tailed conditional.For example:.P1for(i){    if(test -f /tmp/$i) echo $i already in /tmp    if not cp $i /tmp}.P2This loops over each file in.CW $* ,copying to.CW /tmpthose that do not already appear there, andprinting a message for those that do..NHControl flow \(em \f(CWwhile\fP.PP.I Rc 'swhile statement looks like this:.P1while(newer subr.v subr.c) sleep 5.P2This waits until.CW subr.vis newer than.CW subr.c ,presumably because the C compiler finished with it..PPIf the controlling command is empty, the loop will not terminate.Thus,

Lines 1–500 of 1589.