📄 rc.1
字号:
.I rcwill set.I varto each element of.Cr $*(excluding.Cr $0 ).For example:.Ds.Cr "for (i in \`{ls -F | grep '\e*$' | sed 's/\e*$//'}) { commands }".De.TP\&will set.Cr $ito the name of each file in the current directory that isexecutable..SS "Switch".TP.Ci "switch (" list ") { case" " ..." " }".I rclooks inside the braces after a.Cr switchfor statements beginning with the word.Cr case .If any of the patterns following.Cr casematch the list supplied to.Cr switch ,then the commands up until the next.Cr casestatement are executed.The metacharacters.Cr "*" ,.Cr [or.Cr ?should not be quoted;matching is performed only against the strings in.IR list ,not against file names.(Matching for case statements is the same as for the.Cr ~command.).SS "Logical Operators"There are a number of operators in.I rcwhich depend on the exit status of a command..Ds.Cr "command && command".De.PPexecutes the first command and then executes the second command if and only ifthe first command exits with a zero exit status (``true'' in Unix)..Ds.Cr "command || command".De.PPexecutes the first command executing the second command if and only ifthe second command exits with a nonzero exit status (``false'' in Unix)..Ds.Cr "! command".De.PPnegates the exit status of a command..SH "PATTERN MATCHING"There are two forms of pattern matching in.IR rc .One is traditional shell globbing.This occurs in matching for file names in argument lists:.Ds.Cr "command argument argument ...".De.PPWhen the characters.Cr "*" ,.Cr [or.Cr ?occur in an argument or command,.I rclooks at theargument as a pattern for matching against files.(Contrary to the behavior other shells exhibit,.I rcwill only perform pattern matching if a metacharacter occurs unquoted andliterally in the input.Thus,.Ds.Cr "foo='*'".Cr "echo $foo".De.PPwill always echo just a star.In order for non-literal metacharacters to be expanded, an.Cr evalstatement must be used in order to rescan the input.)Pattern matching occurs according to the following rules: a.Cr *matches any number (including zero) ofcharacters.A.Cr ?matches any single character, and a.Cr [followed by anumber of characters followed by a.Cr ]matches a single character in thatclass.The rules for character class matching are the same as those for.IR ed (1),with the exception that character class negation is achievedwith the tilde.Rc ( ~ ),not the caret.Rc ( ^ ),since the caret already meanssomething else in.IR rc ..PP.I rcalso matches patterns against strings with the.Cr ~command:.Ds.Cr "~ subject pattern pattern ...".De.PP.Cr ~sets.Cr $statusto zero if and only if a supplied pattern matches anysingle element of the subject list.Thus.Ds.Cr "~ foo f*".De.PPsets status to zero, while.Ds.Cr "~ (bar baz) f*".De.PPsets status to one.The null list is matched by the null list, so.Ds.Cr "~ $foo ()".De.PPchecks to see whether.Cr $foois empty or not.This may also be achievedby the test.Ds.Cr "~ $#foo 0".De.PPNote that inside a.Cr ~command.I rcdoes not match patterns against filenames, so it is not necessary to quote the characters.Cr "*" ,.Cr [and.Cr "?" .However,.I rcdoes expand the glob the subject against filenames if it containsmetacharacters.Thus, the command.Ds.Cr "~ * ?".De.PPreturns true if any of the files in the current directory have asingle-character name.(Note that if the.Cr ~command is given a list as its firstargument, then a successful match against any of the elements of thatlist will cause.Cr ~to return true.For example:.Ds.Cr "~ (foo goo zoo) z*".De.PPis true.).SH "LISTS AND VARIABLES"The primary data structure in.IR rcis the list, which is a sequence of words.Parentheses are used to group lists.The empty list is represented by.Cr "()" .Lists have no hierarchical structure;a list inside another list is expanded so theouter list contains all the elements of the inner list.Thus, the following are all equivalent.Ds.Cr "one two three".Cr "(one two three)".Cr "((one) () ((two three)))".De.PPNote that the null string,.Cr "''" ,and the null list,.Cr "()" ,are two verydifferent things.Assigning the null string to variable is a validoperation, but it does not remove its definition.For example,if.Cr $ais set to.Cr "''" ,then.Cr "$#a" ,returns a 1..SS "List Concatenation"Two lists may be joined by the concatenation operator.Rc ( ^ ).A single word is treated as a list of length one, so.Ds.Cr "echo foo^bar".De.PPproduces the output.Ds.Cr foobar.De.PPFor lists of more than one element,concatenation works according to the following rules:if the two lists have the same number of elements,then concatenation is pairwise:.Ds.Cr "echo (a\- b\- c\-)^(1 2 3)".De.PPproduces the output.Ds.Cr "a\-1 b\-2 c\-3".De.PPOtherwise, one of the lists must have a single element,and then the concatenation is distributive:.Ds.Cr "cc \-^(O g c) (malloc alloca)^.c".De.PPhas the effect of performing the command.Ds.Cr "cc \-O \-g \-c malloc.c alloca.c".De.SS "Free Carets".I rcinserts carets (concatenation operators) for free in certain situations,in order to save some typing on the user's behalf.Forexample, the above example could also be typed in as:.Ds.Cr "opts=(O g c) files=(malloc alloca) cc \-$opts $files.c".De.PP.I rctakes care to insert a free-caret between the.Rc `` \- ''and.Cr "$opts" ,as wellas between.Cr $filesand.Cr ".c" .The rule for free carets is as follows: ifa word or keyword is immediatelyfollowed by another word, keyword, dollar-sign orbackquote, then.I rcinserts a caret between them..SS "Variables"A list may be assigned to a variable, using the notation:.Ds.Ic var " = " list.De.PPAny sequence of non-special characters, except a sequence includingonly digits, may be used as a variable name.All user-defined variables are exported into the environment..PPThe value of a variable is referenced with the notation:.Ds.Ci $ var.De.PPAny variable which has not been assigned a value returns the null list,.Cr "()" ,when referenced.In addition, multiple references are allowed:.Ds.Cr a=foo.Cr b=a.Cr "echo $$b".De.PPprints.Ds.Cr foo.De.PPA variable's definition may also be removed byassigning the null list to a variable:.Ds.Ic var =().De.PPFor ``free careting'' to work correctly,.I rcmust make certain assumptionsabout what characters may appear in a variable name..I rcassumes that a variable name consists only of alphanumeric characters,underscore.Rc ( \|_\| )and star.Rc ( * ).To reference a variable with othercharacters in its name, quote the variable name.Thus:.Ds.Cr "echo $'we$Ird\Variab!le'".De.SS "Local Variables"Any number of variable assignments may be made local to a singlecommand by typing:.Ds.Cr "a=foo b=bar ... command".De.PPThe command may be a compound command, so for example:.Ds.Cr "path=. ifs=() {".Cr " " ....Cr }.De.PPsets.Cr pathto.Cr .and removes.Cr ifsfor the duration of one long compound command..SS "Variable Subscripts"Variables may be subscripted with the notation.Ds.Ci $var( n ).De.PPwhere.I nis a list of integers (origin 1).The list of subscripts neednot be in order or even unique.Thus, if.Ds.Cr "a=(one two three)".De.PPthen.Ds.Cr "echo $a(3 3 3)".De.PPprints.Ds.Cr "three three three".De.PPIf.I nreferences a nonexistent element, then.Ci $var( n )returns the null list.The notation.Ci "$" n\fR,where.I nis an integer, is a shorthand for.Ci $*( n )\fR.Thus,.IR rc 'sarguments may be referred to as.Cr "$1" ,.Cr "$2" ,and so on..PPNote also that the list of subscripts may be given by any of.IR rc 'slist operations:.Ds.Cr "$var(\`{awk 'BEGIN{for(i=1;i<=10;i++)print i;exit; }'})".De.PPreturns the first 10 elements of.Cr $var ..PPTo count the number of elements in a variable, use.Ds.Cr $#var.De.PPThis returns a single-element list, with the number of elements in.Cr $var ..SS "Flat Lists"In order to create a single-element list from a multi-element list,with the components space-separated, use.Ds.Cr $^var.De.PPThis is useful when the normal list concatenation rules need to bebypassed.For example, to append a single period at the end of.Cr $path ,use:.Ds.Cr "echo $^path.".De.SS "Backquote Substitution"A list may be formed from the output of a command by using backquotesubstitution:.Ds.Cr "\`{ command }".De.PPreturns a list formed from the standard output of the command in braces..Cr $ifsis used to split the output into list elements.By default,.Cr $ifshas the value space-tab-newline.The braces may be omitted if the command is a single word.Thus.Cr \`lsmay be used instead of.Cr "\`{ls}" .This last feature is useful when defining functions that expandto useful argument lists.A frequent use is:.Ds.Cr "fn src { echo *.[chy] }".De.PPfollowed by.Ds.Cr "wc \`src".De.PP(This will print out a word-count of all C source files in the currentdirectory.).PPIn order to override the value of.Cr $ifsfor a single backquotesubstitution, use:.Ds.Cr "\`\` (ifs-list) { command }".De.PP.Cr $ifswill be temporarily ignored and the command's output will be split as specified bythe list following the double backquote.For example:.Ds.Cr "\`\` ($nl :) {cat /etc/passwd}".De.PPsplits up.Cr /etc/passwdinto fields, assuming that.Cr $nlcontains a newlineas its value..SH "SPECIAL VARIABLES"Several variables are known to.I rcand are treated specially..TP.Cr *The argument list of.IR rc ..Cr "$1, $2,"etc. are the same as.Cr $*(1) ,.Cr $*(2) ,etc.The variable.Cr $0holds the value of.Cr argv[0]with which.I rcwas invoked.Additionally,.Cr $0is set to the name of a function for the duration ofthe execution of that function, and.Cr $0is also set to the name of thefile being interpreted for the duration of a.Cr .command..TP.Cr apidThe process ID of the last process started in the background..TP.Cr apidsThe process IDs of any background processes which are outstandingor which have died and have not been waited for yet..TP.Cr cdpathA list of directories to search for the target of a.B cdcommand.The empty string stands for the current directory.Note that if the.Cr $cdpathvariable does not contain the current directory, then the currentdirectory will not be searched; this allows directory searching tobegin in a directory other than the current directory.Note also that an assignment to.Cr $cdpathcauses an automatic assignment to.Cr $CDPATH ,and vice-versa..TP.Cr history.Cr $historycontains the name of a file to which commands are appended as.I rcreads them.This facilitates the use of a stand-alone history program(such as.IR history (1))which parses the contents of the history file and presents them to.I rcfor reinterpretation.If.Cr $historyis not set, then.I rcdoes not append commands to any file..TP.Cr homeThe default directory for the builtin.B cdcommand and is the directoryin which.I rclooks to find its initialization file,.Cr .rcrc ,if.I rchas been started up as a login shell.Like.Cr $cdpathand.Cr $CDPATH ,.Cr $homeand.Cr $HOMEare aliased to each other..TP.Cr ifsThe internal field separator, used for splitting up the output ofbackquote commands for digestion as a list..TP.Cr pathThis is a list of directories to search in for commands.The empty string stands for the current directory.Note that like.Cr $cdpathand.Cr $CDPATH ,.Cr $pathand.Cr $PATHare aliased to each other.If.Cr $pathor.Cr $PATHis not set at startup time,.Cr $pathassumes a default value suitable for your system.This is typically.Cr "(/usr/ucb /usr/bin /bin .)".TP.Cr pidThe process ID of the currently running.IR rc ..TP.Cr promptThis variable holds the two prompts (in list form, of course) that.I rcprints..Cr $prompt(1)is printed before each command is read, and.Cr $prompt(2)is printed when input is expected to continue on the nextline..I rcsets.Cr $promptto.Cr "('; ' '')"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -