⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap2.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
in all cases where this specification indicates that a syntax is invalid.<p><li>If the previous character was used as part of an operator andthe current character cannot be usedwith the current characters to form anoperator, the operator containing the previous character will be delimited.<p><li>If the current character is backslash, single-quoteor double-quote(\,'or")and it is not quoted, itwill affect quoting for subsequent charactersup to the end of the quoted text.The rules for quoting are as described in<xref href=quoting><a href="#tag_001_002">Quoting</a></xref>.During token recognition no substitutionswill be actually performed, and the result token will contain exactly thecharacters that appear in the input(except fornewline characterjoining), unmodified, including anyembedded or enclosing quotes or substitution operators, between thequote mark and the end of thequoted text.The token will not be delimited by the end of the quoted field.<p><li>If the current character is an unquoted "$"or `, the shell will identify the start of any candidatesfor parameter expansion,command substitution,or arithmetic expansionfrom their introductory unquoted character sequences: "$"or${,$(or `, and$((,respectively.The shell will read sufficient input to determine theend of the unit to be expanded (as explained in the cited sections).While processing the characters, if instances of expansions orquoting are found nested within the substitution, the shellwill recursively process them in the manner specified for the constructthat is found.The characters found from the beginning of thesubstitution to its end, allowing for any recursion necessary torecognise embedded constructs, will be included unmodified in theresult token, including any embedded or enclosing substitutionoperators or quotes.The token will not be delimited by the end of the substitution.<p><li>If the current character is not quoted and can be used as thefirst character of a new operator,the current token (if any) will be delimited.The current character will be used as the beginning of the next(operator) token.<p><li>If the current character is an unquotednewline character,the current token will be delimited.<p><li>If the current character is an unquotedblank character,any token containing the previous character is delimited and thecurrent character will be discarded.<p><li>If the previous character was part of a word, the currentcharacter will be appended to that word.<p><li>If the current character is a"#",it and all subsequentcharacters up to, but excluding, the nextnewline characterwill be discarded as a comment.Thenewline characterthat ends the line is not consideredpart of the comment.The"#"starts a comment only when it is at the beginning of a token.Since the search forthe end-of-comment does not consider an escapednewline characterspecially, a comment cannot be continuedto the next line.<p><li>The current character will be used as the start of a new word.<p></ol><p>Once a token is delimited, it will be categorisedas required by the grammar in<xref href=shgram><a href="#tag_001_010">Shell Grammar</a></xref>.<h4><a name = "tag_001_003_001">&nbsp;</a>Alias Substitution</h4><xref type="3" name="alsub"></xref>The processing of aliases is supported on all XSI-conformant systems.<p>After a token has been delimited,but before applying the grammatical rules in<xref href=shgram><a href="#tag_001_010">Shell Grammar</a></xref>,a resulting wordthat is identified to be the command name word of a simple commandis examined to determine if it is an unquoted, valid alias name.However, reserved words in correct grammatical contextare not candidates for alias substitution.A valid alias name (see the termalias nameinthe <b>XBD</b> specification, <a href="../xbd/glossary.html"><b>Glossary</b>&nbsp;</a> )is one that has been defined by the<i><a href="alias.html">alias</a></i>utilityand not subsequently undefined using<i><a href="unalias.html">unalias</a></i>.Implementations also may provide predefined valid aliasesthat are in effect when the shell is invoked.To prevent infinite loops in recursive aliasing,if the shell is not currently processing an alias of the same name,the word will be replaced by the value of the alias;otherwise, it will not be replaced.<p>If the value of the alias replacing the word ends in ablank character,the shell will check the next command word for alias substitution;this process continues until a word is found that is nota valid alias or an alias value does not end in ablank character.<p>When used as specified by this specification,alias definitions will not be inherited by separate invocations of the shellor by the utility execution environments invoked by the shell; see<xref href=shexenv><a href="#tag_001_012">Shell Execution Environment</a></xref>.<p>The definition of<i>alias name</i>precludes an alias name containing a slash character.Since the text applies to the command words of simplecommands, reserved words (in their proper places)cannot be confused with aliases.<p>An example concerning trailingblank charactersand reserved words follows.If the user types:<pre><code><b>$</b> alias foo="/bin/ls "<b>$</b> alias while="/"</code></pre><p>The effect of executing:<pre><code><b>$</b> while true<b>&gt;</b> do<b>&gt;</b> echo "Hello, World"<b>&gt;</b> done</code></pre>is a never-ending sequence of<b>Hello,&nbsp;World</b>strings to the screen.However, if the user types:<pre><code><b>$</b> foo while</code></pre>the result will be an<i><a href="ls.html">ls</a></i>listing of <b>/</b>.Since the alias substitution for<b>foo</b>ends in a space character,the next word is checked for alias substitution.The next word,<b>while</b>,has also been aliased, so it is substituted as well.Since it is not in the proper position as a commandword, it is not recognised as a reserved word.<p>If the user types:<pre><code><b>$</b> foo; while</code></pre><b>while</b>retains its normal reserved-word properties.<hr size=2 noshade><h3><a name = "tag_001_004">&nbsp;</a>Reserved Words</h3>Reserved words are words that have special meaning to the shell.(See<xref href=shcmds><a href="#tag_001_009">Shell Commands</a></xref>.)The following words will be recognised as reserved words:<pre><dl compact><dt> <dd><table <tr valign=top><td align=left>!<td align=left>elif<td align=left>fi<td align=left>in<td align=left>while<tr valign=top><td align=left>case<td align=left>else<td align=left>for<td align=left>then<td align=left>{*<tr valign=top><td align=left>do<td align=left>esac<td align=left>if<td align=left>until<td align=left>}<tr valign=top><td align=left>done</table></dl></pre><font size=2>(*) In some historical systems, the curly bracesare treated as control operators.To assist in future standardisation activities,portable applications should avoid using unquotedbraces to represent the characters themselves.It is possible that a future version of the ISO/IEC 9945-2:1993 standardmay require that { and }be treated individually as control operators,although the token {}will probably be a special-case exemptionfrom this because of the often-used <i>find</i>{}construct.</font><p>This recognition will occur onlywhen none of the characters are quoted and when the word is usedas:<ul><p><li>the first word of a command<p><li>the first word following one of the reserved words other than<b>case</b>,<b>for</b>,or<b>in</b><p><li>the third word in a<b>case</b>or<b>for</b>command (only<b>in</b>is valid in this case).<p></ul><p>See the grammar in<xref href=shgram><a href="#tag_001_010">Shell Grammar</a></xref>.<p>The following words may be recognised as reserved wordson some systems (when none of the characters are quoted),causing unspecified results:<pre><dl compact><dt> <dd><table <tr valign=top><th align=left>function<th align=left>select<th align=left>[[<th align=left>]]</table></dl></pre><p>This list of unspecified reserved words is from the KornShell,so portable applications cannot use them in places a reservedword would be recognised without quoting some or all oftheir characters.<p>Words that are the concatenation of a name and a colon(:)are reserved;their use produces unspecified results.This reservationis to allow future implementations that support namedlabels for flow control.<p>Reserved words are recognised only when they are delimited (that is,meet the definition of<b>word</b>in the <b>XSH</b> specification),whereas operators are themselves delimiters.For instance,"("and ")"are control operators, so that nospace characteris needed in(list).However,"{"and"}"are reserved words in{&nbsp;list;},so that in this case the leadingspace characterand semicolon are required.<hr size=2 noshade><h3><a name = "tag_001_005">&nbsp;</a>Parameters and Variables</h3><xref type="2" name="parms"></xref>A parameter can be denoted by a name,a number or one of the special characterslisted in<xref href=specpar><a href="#tag_001_005_002">Special Parameters</a></xref>.A variable is a parameter denoted by a name.<p>A parameter is set if it has an assigned value(null is a valid value).Once a variable is set, it can only be unset by using the<i><a href="chap2.html#tag_001_014_015">unset</a></i>special built-in command.<h4><a name = "tag_001_005_001">&nbsp;</a>Positional Parameters</h4>A positional parameter is a parameter denoted by the decimal valuerepresented by one or more digits, other than the single digit 0.The digits denoting the positional parameters are alwaysinterpreted as a decimal value, even if there is a leading zero.When a positional parameter with more than one digit isspecified, the application must enclose the digits in braces (see<xref href=parmexp><a href="#tag_001_006_002">Parameter Expansion</a></xref>).Positional parameters are initially assigned when the shell is invoked(see<i><a href="sh.html">sh</a></i>),temporarily replaced when a shell function is invoked (see<xref href=funccmd><a href="#tag_001_009_005">Function Definition Command</a></xref>),and can be reassigned with the<i>set</i>special built-in command.<h4><a name = "tag_001_005_002">&nbsp;</a>Special Parameters</h4><xref type="3" name="specpar"></xref>Listed below are thespecial parametersand the values to which they will expand.Only the values of the special parameters are listed;see<xref href=wordexp><a href="#tag_001_006">Word Expansions</a></xref>for a detailed summaryof all the stages involved in expanding words.<dl compact><dt><b>*</b><dd>Expands to the positional parameters, starting from one.When the expansion occurs within a double-quoted string (see<xref href=dquote><a href="#tag_001_002_003">Double-quotes</a></xref>),it expands to a singlefield with the value of each parameter separated by the firstcharacter of thevariable, or by aspace characterifis unset.Ifis set to a null string, this is not equivalentto unsetting it; its first character will not exist,so the parameter values are concatenated.For example:<pre><code><b>$</b> IFS=''<b>$</b> set foo bar bam<b>$</b> echo "$@"<b>foo bar bam</b><b>$</b> echo "$*"<b>foobarbam</b><b>$</b> unset IFS<b>$</b> echo "$*"<b>foo bar bam</b></code></pre><dt>@<dd>Expands to the positional parameters, starting from one.When the expansion occurs within double-quotes,and where field splitting (see<xref href=fldspl><a href="#tag_001_006_005">Field Splitting</a></xref>)is performed, each positional parameter expands as a separatefield, with the provision that the expansion of thefirst parameter is stilljoined with the beginning part of the original word(assuming that the expanded parameter was embedded within a word),and the expansion of the last parameter is stilljoined with the last part of the original word.If there are no positional parameters, the expansion of "@"generates zero fields, even when "@" is double-quoted.<dt>#<dd>Expands to the decimal number of positional parameters.The command name (parameter0)is not counted in the number given by"#"because it is a special parameter, not a positional parameter.<dt><b>?</b><dd>Expands to the decimal exit status of the most recent pipeline (see<xref href=pipel><a href="#tag_001_009_002">Pipelines</a></xref>).<dt><b>-</b><dd>(Hyphen.)Expands to the current option flags(the single-letter option namesconcatenated into a string)as specified on invocation,by the<i>set</i>special built-in command or implicitly by the shell.The$-special parameter can be used to save and restore<i>set</i>options:<pre><code>Save=$(echo $- | sed 's/[ics]//g')...set +aCefnuvxif [ -n "$Save" ]; then     set -$Savefi</code></pre>The three options are removed using<i><a href="sed.html">sed</a></i>in the example because they may appear in the value of$-(from the<i><a href="sh.html">sh</a></i>command line), but are not valid options to<i>set</i>.<dt><b>$</b><dd>Expands to the decimal process ID of the invoked shell.In a subshell (see<xref href=shexenv><a href="#tag_001_012">Shell Execution Environment</a></xref>),"$" expands to the same value as that of the current shell.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -