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

📄 chap2.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<pre><code>x=1echo $(( $(echo 3)+$x ))</code></pre><h4><a name = "tag_001_006_001">&nbsp;</a>Tilde Expansion</h4><xref type="3" name="tildexp"></xref>A<i>tilde-prefix</i>consists of an unquoted tilde character at the beginning of a word,followed byall of the characters preceding the first unquoted slash in the word,or all the characters in the word if there is no slash.In an assignment(see<b>variable assignment</b>in the <b>XBD</b> specification, <a href="../xbd/glossary.html"><b>Glossary</b>&nbsp;</a> )multiple tilde-prefixes can be used:at the beginningof the word (that is, following the equal sign of theassignment), following any unquoted colon or both.A tilde-prefix in an assignmentis terminated by the first unquoted colon or slash.If none of the characters in the tilde-prefix are quoted,the characters in the tilde-prefixfollowing the tilde are treated as a possible login namefrom the user database.A portable login name cannot contain characters outsidethe set given in the description of the<i>LOGNAME</i>environment variable in the <b>XSH</b> specification.If the login name is null(that is, the tilde-prefix contains only the tilde),the tilde-prefix will be replaced by the value of the variable<i>HOME</i>.If<i>HOME</i>is unset, the results are unspecified.Otherwise, the tilde-prefix will be replaced by a pathname of thehome directory associated with the login nameobtained using the <b>XSH</b> specification<i><a href="../xsh/getpwnam.html">getpwnam()</a></i>function.If the system does not recognise the login name,the results are undefined.<p>Tilde expansion generally occurs only at the beginningof words, butan exception based on historical practice has been included:<pre><code>PATH=/posix/bin:~dgk/bin</code></pre>is eligible for tilde expansionbecause tilde follows a colonand none of the relevant characters is quoted.Consideration was given to prohibiting this behaviourbecause any of the following are reasonable substitutes:<pre><code>PATH=$(printf %s: ~rms/bin ~bfox/bin ...)  PATH=$(printf %s ~karels/bin : ~bostic/bin)for Dir in ~maart/bin ~srb/bin ...do    PATH=${PATH:+$PATH:}$Dirdone</code></pre><p>In the first command, any number of directory names areconcatenated and separated with colons, but it may beundesirable to end the variable with a colon because thisis an obsolescent means to include dot at the end of the<i>PATH</i>.&nbsp;In the second, explicit colonsare used for each directory.In all cases, the shell performs tilde expansionon each directory because all are separate words to the shell.<p>Note that expressions in operands such as:<pre><code>make -k mumble LIBDIR=~chet/lib</code></pre>do not qualify as shell variable assignmentsand tilde expansion is not performed (unless the commanddoes so itself, which<i><a href="make.html">make</a></i>does not).<p>The special sequence <b>$~</b>has been designated for future implementations to evaluateas a means of forcing tilde expansion in any word.<p>Because of the requirement that the word is not quoted, thefollowing are not equivalent; only the last will cause tilde expansion:<pre><code>\~hlj/   ~h\lj/   ~"hlj"/   ~hlj\/   ~hlj/</code></pre><p>The results of giving tilde with an unknownlogin name are undefined because the KornShell~+and~-constructs make use of this condition, but, in generalit is an error to give an incorrect login name with tilde.The results of having<i>HOME</i>unset are unspecified because some historical shellstreat this as an error.<h4><a name = "tag_001_006_002">&nbsp;</a>Parameter Expansion</h4><xref type="3" name="parmexp"></xref>The format for parameter expansion is as follows:<pre><code>${<i>expression</i>}</code></pre>where<i>expression</i>consists of all characters until the matching"}".Any"}"escaped by a backslash or within a quoted string,and characters in embeddedarithmetic expansions,command substitutionsand variable expansions,are not examined in determining the matching"}".<p>The simplest form for parameter expansion is:<pre><code>${<i>parameter</i>}</code></pre><p>The value, if any, of<i>parameter</i>will be substituted.<p>Theparametername or symbol can be enclosed inbraces, which are optional except for positional parameterswith more than one digit or when<i>parameter</i>is followed by a character thatcould be interpreted as part of the name.The matching closing brace will be determinedby counting brace levels, skippingover enclosed quoted strings and command substitutions.<p>If the parameter name or symbol is not enclosed in braces,the expansion will use the longestvalid name (see <b>name</b>in the <b>XBD</b> specification, <a href="../xbd/glossary.html"><b>Glossary</b>&nbsp;</a> ),whether or not the symbol represented by that name exists.When the shell is scanning its input to determine the boundariesof a name, it is not bound by its knowledge of what namesare already defined.For example, if<b>F</b>is a defined shell variable, the command:<pre><code>echo&nbsp;$Fred</code></pre>does not echo the value of<b>$F</b>followed by<b>red</b>;it selects the longest possible valid name,<b>Fred</b>,which in this case might be unset.<p>If a parameter expansion occurs inside double-quotes:<ul><p><li>Pathname expansion will not be performedon the results of the expansion.<p><li>Field splitting will not be performedon the results of the expansion,with the exception of "@"; see<xref href=specpar><a href="#tag_001_005_002">Special Parameters</a></xref>.<p></ul><p>In addition, aparameter expansioncan be modified by using one of the following formats.In each casethat a value of<i>word</i>is needed (based on the state of<i>parameter</i>,as described below),<i>word</i>will be subjected totilde expansion,parameter expansion, command substitutionand arithmetic expansion.If<i>word</i>is not needed, it will not be expanded.The"}"character that delimits the following parameter expansionmodifications is determinedas described previously in this section and in<xref href=dquote><a href="#tag_001_002_003">Double-quotes</a></xref>.(For example,${foo-bar}xyz}would result in the expansion of<b>foo</b>followed by the stringxyz}if<b>foo</b>is set, else the stringbarxyz}).<dl compact><dt>${<i>parameter</i>:-<i>word</i>}<dd><b>Use Default Values</b>. If<i>parameter</i>is unset or null, the expansion of<i>word</i>will be substituted; otherwise, the value of<i>parameter</i>will be substituted.<dt>${<i>parameter</i>:=<i>word</i>}<dd><b>Assign Default Values</b>. If<i>parameter</i>is unset or null, the expansion of<i>word</i>will be assigned to<i>parameter</i>.In all cases, the final value of<i>parameter</i>will be substituted.Only variables, not positional parameters or special parameters,can be assigned in this way.<dt>${<i>parameter</i>:?<b>[</b><i>word</i><b>]</b>}<dd><b>Indicate Error if Null or Unset</b>. If<i>parameter</i>is unset or null, the expansion of<i>word</i>(or a message indicating it is unset if<i>word</i>is omitted)will be written tostandard error and the shell will exitwith a non-zero exit status.Otherwise, the value of<i>parameter</i>will be substituted.An interactive shellneed not exit.<dt>${<i>parameter</i>:+<i>word</i>}<dd><b>Use Alternative Value</b>. If<i>parameter</i>is unset or null,null will be substituted;otherwise, the expansion of<i>word</i>will be substituted.</dl><p>In the parameter expansions shown previously,use of the colon in the format results in a testfor a parameter that is unset or null;omission of the colon results in a testfor a parameter that is only unset.The following table summarises the effect of the colon:<pre><table  bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>&nbsp;</b><th align=center><b>parameter<br>set and not null</b><th align=center><b>parameter<br>set but null</b><th align=center><b>parameter<br>unset</b><tr valign=top><td align=left><b>${</b><i>parameter</i><b>:-</b><i>word</i><b>}</b><td align=left>substitute <i>parameter</i><td align=left>substitute <i>word</i><td align=left>substitute <i>word</i><tr valign=top><td align=left><b>${</b><i>parameter</i><b>-</b><i>word</i><b>}</b><td align=left>substitute <i>parameter</i><td align=left>substitute null<td align=left>substitute <i>word</i><tr valign=top><td align=left><b>${</b><i>parameter</i><b>:=</b><i>word</i><b>}</b><td align=left>substitute <i>parameter</i><td align=left>assign <i>word</i><td align=left>assign <i>word</i><tr valign=top><td align=left><b>${</b><i>parameter</i><b>=</b><i>word</i><b>}</b><td align=left>substitute <i>parameter</i><td align=left>substitute <i>parameter</i><td align=left>assign null<tr valign=top><td align=left><b>${</b><i>parameter</i><b>:?</b><i>word</i><b>}</b><td align=left>substitute <i>parameter</i><td align=left>error, exit<td align=left>error, exit<tr valign=top><td align=left><b>${</b><i>parameter</i><b>?</b><i>word</i><b>}</b><td align=left>substitute <i>parameter</i><td align=left>substitute null<td align=left>error, exit<tr valign=top><td align=left><b>${</b><i>parameter</i><b>:+</b><i>word</i><b>}</b><td align=left>substitute <i>word</i><td align=left>substitute null<td align=left>substitute null<tr valign=top><td align=left><b>${</b><i>parameter</i><b>+</b><i>word</i><b>}</b><td align=left>substitute <i>word</i><td align=left>substitute <i>word</i><td align=left>substitute null</table></pre><p>In all cases shown with &quot;substitute&quot;,the expression is replaced with the value shown.In all cases shown with &quot;assign&quot;,<i>parameter</i>is assigned that value, which also replaces the expression.<dl compact><dt><b>${#</b><i>parameter</i><b>}</b><dd><b>String Length</b>. The length in characters of the value of<i>parameter</i>.If<i>parameter</i>is "*" or "@",the result of the expansion is unspecified.</dl><p>The following four varieties of parameter expansionprovide for substring processing.In each case, pattern matching notation (see<xref href=patmat><a href="#tag_001_013">Pattern Matching Notation</a></xref>),rather than regular expression notation, will beused to evaluate the patterns.If<i>parameter</i>is "*" or "@", the result of the expansion is unspecified.Enclosing the full parameter expansion string indouble-quotes will not cause the following fourvarieties of pattern charactersto be quoted, whereasquoting characters within the braces will have this effect.<dl compact><dt>${<i>parameter</i>%<i>word</i>}<dd><b>Remove Smallest Suffix Pattern</b>.The<i>word</i>will be expanded to produce a pattern.The parameter expansion then will result in<i>parameter</i>,with the smallest portion of the suffix matched by the<i>pattern</i>deleted.<dt>${<i>parameter</i>%%<i>word</i>}<dd><b>Remove Largest Suffix Pattern</b>.The<i>word</i>will be expanded to produce a pattern.The parameter expansion then will result in<i>parameter</i>,with the largest portion of the suffix matched by the<i>pattern</i>deleted.<dt>${<i>parameter</i>#<i>word</i>}<dd><b>Remove Smallest Prefix Pattern</b>. The<i>word</i>will be expanded to produce a pattern.The parameter expansion then will result in<i>parameter</i>,with the smallest portion of the prefix matched by the<i>pattern</i>deleted.<dt>${<i>parameter</i>##<i>word</i>}<dd><b>Remove Largest Prefix Pattern</b>. The<i>word</i>will be expanded to produce a pattern.The parameter expansion then will result in<i>parameter</i>,with the largest portion of the prefix matched by the<i>pattern</i>deleted.</dl><p><h5><a name = "tag_001_006_002_001">&nbsp;</a>Examples</h5><dl compact><dt>${<i>parameter</i>:-<i>word</i>}<dd>In this example,<i><a href="ls.html">ls</a></i>is executed only ifxis null or unset.(The$(ls)command substitution notation is explained in<xref href=cmdsub><a href="#tag_001_006_003">Command Substitution</a></xref>.)<pre><code>${x:-$(ls)}</code></pre><dt>${<i>parameter</i>:=<i>word</i>}<dd><code><pre>unset Xecho ${X:=abc}<b>abc</b></code></pre><dt>${<i>parameter</i>:?<i>word</i>}<dd><code><pre>unset posixecho ${posix:?}<b>sh: posix: parameter null or not set</b></code></pre><dt>${<i>parameter</i>:<i>word</i>}<dd><code><pre>set a b cecho ${3:+posix}<b>posix</b></code></pre><dt><b>${#</b><i>parameter</i><b>}<dd></b><code><pre>HOME=/usr/posixecho ${#HOME}<b>10</b></code></pre><dt>${<i>parameter</i>%<i>word</i>}<dd><code><pre>x=file.cecho ${x%.c}.o<b>file.o</b></code></pre><dt>${<i>parameter</i>%%<i>word</i>}<dd><code><pre>x=posix/src/stdecho ${x%%/*}<b>posix</b></code></pre><dt>${<i>parameter</i>#<i>word</i>}<dd><code><pre>x=$HOME/src/cmdecho ${x#$HOME}<b>/src/cmd</b></code></pre>

⌨️ 快捷键说明

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