.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''..EXLines 1–500 of 532. Load more
.TH SH-STD 1 .SH NAME std, 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 Std is a loadable module for .IR sh (1) that provides the equivalent of a ``standard library'' for the shell, including a set of control-flow constructs and some other miscellaneous commands. In the following descriptions, if an argument is executed, then it should be a braced block suitable 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 value of a command is that of the last command that it executed. If invalid arguments are passed to any command, a .B usage exception is raised, and a message printed to stderr. .PP Each of the looping commands .BR for , .BR apply , .BR while , and .B getlines installs an exception handler for the duration of the loop to catch the exceptions .B break and .BR continue . If a .B break exception is caught, the loop is terminated; if a .B continue exception is caught, the loop will continue executing as usual. 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 value against each .I pattern in turn, returning true if any of them match and false otherwise. The patterns are of the same form as those accepted by the shell for filename pattern matching except that / is not treated specially. (see .IR filepat (2)). Patterns must be quoted to stop the shell from interpreting them. .TP .B no True if there are no arguments. Useful for testing if there are any items in a list without counting the items with .BR $# . .TP .BI and .B And evaluates each .I command in turn until one returns false. .TP .B apply .B Apply evaluates .I command once for each .IR arg , passing it in the variable .BR $1 . .TP .B getlines .B Getlines reads lines from the standard input, executing .I command for each line, setting the environment variable .B $line to the line read, with any terminating character removed. If .I separators is given, a line is terminated when any character in .I separators is found; the default separator string is a single newline character. .TP .B flag Either set .RB ( + ), clear .RB ( - ), or test (neither .B + or .BR - ) the flag .IR f , where .I f is a single character, one of the command line flags to .I sh (see .IR sh (1)). .TP .B fn .B Fn defines a new builtin command named .IR name ; when run, this command evaluates .IR command . The command is stored in the environment variable .BI fn- name\f1;\fP any variables of this form found when when .B std is loaded will be defined in this way. If .I command is not given, then the builtin will be removed. .TP .B subfn .B Subfn is similar to .B fn except that it defines a new substitution builtin .IR name . When .I name is invoked, it creates a new local variable .B result and executes .IR command . The value of .B $result when .I command has terminated is the value yielded by the substitution builtin .IR name . .I Command is stored in and restored from the environment in a similar way to .BR fn , except that .BI sfn- name is used as the name of the environment variable. .TP .B if .B If executes .IR condition ; if it returns true, then .I action is executed, otherwise each of the next .IR condition - action pairs is evaluated in the same way; if no .I condition is satisfied, then .I elseaction will be executed, if present. .TP .B for .B For is similar to .BR apply ; it runs .I command once for each .IR arg , but it performs a local assignment of .I arg to .I var each time. .TP .B or .B Or evaluates each .I command in turn until one returns true. .TP .B pctl .B Pctl is an interface to the Inferno system call .IR sys-pctl (2); each argument specifies one bit in the bitmask passed to that function. The possible flags are .BR newfd , .BR forkfd , .BR newns , .BR forkns , .BR newpgrp and .BR nodevs . See .IR sys-pctl (2) for details of the meaning of these flags. .B Pctl returns true. .TP .B raise .B Raise raises the exception .IR name ; .I name will be truncated if it is longer than that allowed by .I raise (128 bytes in .IR utf (6) representation). Control will be transferred to the innermost rescue block in the same process that matches .IR name . If there is no rescue block in place, the current process will exit, yielding .I name as its exit status. If no .I name is given, the exception named in .B $exception is raised; if this is null, a .B bad raise context exception is raised. The default command prompt catches all exceptions. .TP .B rescue .B Rescue executes .I command with an exception handler installed for the duration of the call. It will catch all exceptions with a name matching .IR pattern , where .I pattern is of the same form accepted by Limbo's .B exception handling 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 rescue executes .IR rescueblock , setting .B $exception to the name of the exception raised. .TP .B status returns its first argument word as its exit status, or nil if none is given. .TP .B while .B While repeatedly executes .I condition and then .I action until .I condition does not return true. .TP .B ${env} .B Env yields a list of the names of all currently set non-nil environment variables. .TP .B ${hd} .B Hd yields the first of its arguments, or nil if there are no arguments. .TP .B ${index} .B Index yields the .IR n 'th element in its argument list, indexed from 1. .I N must be a decimal integer. .TP .B ${join} .B Join yields a single element which is the concatenation of all the elements in .I list separated by .IR separator . If there are no elements in .IR list , it yields an empty string. The shell operator \f5$"\f2var\f1 is exactly equivalent to .BI "${join ' ' $" var }\f1.\fP .TP .B ${parse} .B Parse parses .I arg according to the usual syntax rules, raising a .B parse error exception if it fails. .I Arg must be a well-formed command block surrounded by braces. .B Parse yields a functionally equivalent version of .IR arg . .TP .B ${pid} .B Pid yields the process id of the current process. .TP .B ${pipe} .B Pipe runs .I command asynchronously, with one of its file descriptors connected to a bidirectional pipe. The first argument to .B pipe determines which file descriptor is connected: if the argument is .BR from , its standard output is connected; if the argument is .BR to , its standard input is connected; otherwise file descriptor .I fdnum is connected. .B Pipe yields the name of a file that can be opened to access the other end of the pipe. Note that this command is now deprecated in favour of the .B <{} redirection operator built in to the shell. .TP .B ${split} .B Split splits .I arg into list elements at every point where one or more characters in .I separators appear. If .I separators is not given, the value of .B $ifs is used. .TP .B ${tl} .B Tl yields all but the first of its arguments, or nil if there are no arguments. .SS Syntactic considerations It is worth being aware of a few pitfalls that await the user of some of these commands. Unlike other shells, the syntax of .I sh does not include the syntax of the control flow commands, so it is important to be aware of the rules that govern the gathering of the arguments for a command. In particular, the following code, written to print a message a filename ends in .B .b will not work: it will always print ``file is Limbo source''. .EX