Charles.Forsyth20071011-11329612a0519 years agoHistory
532 lines · 9.0 KB · Text
RawDownload
.TH SH-STD 1.SH NAMEstd, if, while, ~, no, !, apply, getlines, status, pctl, fn, and, or, raise, rescue, hd, tl, index, split, join, pid, parse, pctl, env \- standard shell builtins module..SH SYNOPSIS.B load std .B !.I command.br.B ~.I value[.IR pattern ...].br.B no[.IR arg ...].br.B and.IR command ....br.B apply.I command[.IR arg ...].br.B getlines[.I separators].I command.br.B flag.I f[.B +-].br.B for.I var.B in[.IR arg ...].I command.br.B fn.I name command.br.B if.I condition action[.I condition action]... [.I elseaction].br.B or.IR command ....br.B pctl.IR flag....br.B raise.I name.br.B rescue.I pattern rescueblock command.br.B status.I value.br.B subfn.I name command.br.B while.I condition command.br.B ${hd.IB list }.br.B ${index.I number.IB list }.br.B ${pid}.br.B ${split[.I separators].IB arg }.br.B ${join.I separator.IB list }.br.B ${tl.IB list }.br.B ${parse.IB arg ].br.B ${pipe(.B from|.B to|.I fdnum).IB command }.br.B ${env}.SH DESCRIPTION.B Stdis a loadable module for.IR sh (1)that provides the equivalent of a``standard library'' for the shell, includinga set of control-flow constructs and someother miscellaneous commands.In the following descriptions, if an argument isexecuted, then it should be a braced blocksuitable for executing by.IR sh .A true exit status is defined to be nil;any non-nil exit status is false.Unless otherwise stated, the return valueof a command is that of the last command thatit executed.If invalid arguments are passed to any command,a.B usageexception is raised, and a message printed to stderr..PPEach of the looping commands.BR for ,.BR apply ,.BR while ,and.B getlinesinstalls an exception handler for the duration of theloop to catch the exceptions.B breakand.BR continue .If a.B breakexception is caught, the loop is terminated; if a.B continueexception is caught, the loop will continue executing asusual.The commands are as follows:.TP 10.B !.B !inverts the exit status of a command (non-null is changed to null,null is changed to non-null)..TP.B ~.B ~matches.I valueagainst each.I patternin turn, returning true if any of them match and false otherwise.The patterns are of the same form as those accepted bythe shell for filename pattern matching except that / isnot treated specially. (see.IR filepat (2)).Patternsmust be quoted to stop the shell from interpreting them..TP.B noTrue if there are no arguments. Useful for testing if there are any items in a listwithout counting the items with.BR $# ..TP.BI and.B Andevaluates each.I commandin turn until one returns false..TP.B apply.B Applyevaluates.I commandonce for each.IR arg ,passing it in the variable.BR $1 ..TP.B getlines.B Getlinesreads lines from the standard input,executing.I commandfor each line, setting the environment variable.B $lineto the line read, with any terminating characterremoved. If.I separatorsis given, a line is terminated when any characterin.I separatorsis found; the default separator stringis a single newline character..TP.B flagEither set.RB ( + ),clear.RB ( - ),or test (neither.B +or.BR - )the flag.IR f ,where.I fis a single character, one of thecommand line flags to.I sh(see.IR sh (1))..TP.B fn.B Fndefines a new builtin command named.IR name ;when run, this command evaluates.IR command .The command is stored in the environmentvariable.BI fn- name\f1;\fPany variables of this form found whenwhen.B stdis loaded will be defined in this way.If.I commandis not given, then the builtin will be removed..TP.B subfn.B Subfnis similar to.B fnexcept that it defines a new substitution builtin.IR name .When.I nameis invoked, it creates a new local variable.B resultand executes.IR command .The value of.B $resultwhen.I commandhas terminated is the value yielded by the substitution builtin.IR name ..I Commandis stored in and restored from the environment in a similarway to.BR fn ,except that.BI sfn- nameis used as the name of the environment variable..TP.B if.B Ifexecutes.IR condition ;if it returns true, then.I actionis executed, otherwise each of the next.IR condition - actionpairs is evaluated in the same way; if no.I conditionis satisfied, then.I elseactionwill be executed, if present..TP.B for.B Foris similar to.BR apply ;it runs.I commandonce for each.IR arg ,but it performs a local assignment of.I argto.I vareach time..TP.B or.B Orevaluates each.I commandin turn until one returns true..TP.B pctl.B Pctlis an interface to the Inferno system call.IR sys-pctl (2);each argument specifies one bit in the bitmaskpassed to that function. The possible flags are.BR newfd ,.BR forkfd ,.BR newns ,.BR forkns ,.BR newpgrpand.BR nodevs .See.IR sys-pctl (2)for details of the meaning of these flags..B Pctlreturns true..TP.B raise.B Raiseraises the exception.IR name ;.I namewill be truncated if it is longer thanthat allowed by.I raise(128 bytes in.IR utf (6)representation).Control will be transferred to the innermostrescue block in the same process thatmatches.IR name .If there is no rescue block in place,the current process will exit, yielding.I nameas its exit status.If no.I nameis given, the exception named in.B $exceptionis raised; if this is null, a.B bad raise contextexception is raised.The default command prompt catches allexceptions..TP.B rescue.B Rescueexecutes.I commandwith an exception handler installed for the durationof the call. It will catch all exceptions with a namematching.IR pattern ,where.I patternis of the same form accepted byLimbo's.B exceptionhandling statement.Specifically, the pattern is a string that matches literally,except that a trailing.RB ` * 'character will match any sequence of characters.If an exception is caught,.B rescueexecutes.IR rescueblock ,setting.B $exceptionto the name of the exception raised..TP.B statusreturns its first argument word as its exit status,or nil if none is given..TP.B while.B Whilerepeatedly executes.I conditionand then.I actionuntil.I conditiondoes not return true..TP.B ${env}.B Envyields a list of the names of allcurrently set non-nil environment variables..TP.B ${hd}.B Hdyields the first of its arguments, or nil if thereare no arguments..TP.B ${index}.B Indexyields the.IR n 'thelement in its argument list, indexed from 1..I Nmust be a decimal integer..TP.B ${join}.B Joinyields a single element which is the concatenation of allthe elements in.I listseparated by.IR separator .If there are no elements in.IR list ,it yields an empty string.The shell operator\f5$"\f2var\f1is exactly equivalent to.BI "${join ' ' $" var }\f1.\fP.TP.B ${parse}.B Parseparses.I argaccording to the usual syntax rules, raising a.B parse errorexception if it fails..I Argmust be a well-formed command blocksurrounded by braces..B Parseyields a functionally equivalent version of.IR arg ..TP.B ${pid}.B Pidyields the process id of the current process..TP.B ${pipe}.B Piperuns.I commandasynchronously, with one of its file descriptors connectedto a bidirectional pipe. The first argument to.B pipedetermines which file descriptor is connected: ifthe argument is.BR from ,its standard output is connected; if the argument is.BR to ,its standard input is connected; otherwise file descriptor.I fdnumis connected..B Pipeyields the name of a file that can be opened to accessthe other end of the pipe. Note that this command is nowdeprecated in favour of the.B <{}redirection operator built in to the shell..TP.B ${split}.B Splitsplits.I arginto list elements at every point where one ormore characters in.I separatorsappear. If.I separatorsis not given, the value of.B $ifsis used..TP.B ${tl}.B Tlyields all but the first of its arguments,or nil if there are no arguments..SS Syntactic considerationsIt is worth being aware of a few pitfalls that await the user ofsome of these commands. Unlike other shells, the syntax of.I shdoes not include the syntax of the control flow commands,so it is important to be aware of the rules that governthe gathering of the arguments for a command.In particular, the following code, written to print a messagea filename ends in.B .bwill not work: it will always print ``file is Limbo source''..EX

Lines 1–500 of 532.