📄 bashref.html
字号:
Execute <VAR>consequent-commands</VAR> as long as<VAR>test-commands</VAR> has an exit status of zero.The return status is the exit status of the last command executedin <VAR>consequent-commands</VAR>, or zero if none was executed.</P><P><DT><CODE>for</CODE><DD><A NAME="IDX31"></A>The syntax of the <CODE>for</CODE> command is:<P><TABLE><tr><td> </td><td class=example><pre>for <VAR>name</VAR> [in <VAR>words</VAR> <small>...</small>]; do <VAR>commands</VAR>; done</pre></td></tr></table>Expand <VAR>words</VAR>, and execute <VAR>commands</VAR> once for each memberin the resultant list, with <VAR>name</VAR> bound to the current member.If <SAMP>`in <VAR>words</VAR>'</SAMP> is not present, the <CODE>for</CODE> commandexecutes the <VAR>commands</VAR> once for each positional parameter that isset, as if <SAMP>`in "$@"'</SAMP> had been specified(see section <A HREF="bashref.html#SEC25">3.4.2 Special Parameters</A>).The return status is the exit status of the last command that executes.If there are no items in the expansion of <VAR>words</VAR>, no commands areexecuted, and the return status is zero.</P><P>An alternate form of the <CODE>for</CODE> command is also supported:</P><P><TABLE><tr><td> </td><td class=example><pre>for (( <VAR>expr1</VAR> ; <VAR>expr2</VAR> ; <VAR>expr3</VAR> )) ; do <VAR>commands</VAR> ; done</pre></td></tr></table>First, the arithmetic expression <VAR>expr1</VAR> is evaluated accordingto the rules described below (see section <A HREF="bashref.html#SEC78">6.5 Shell Arithmetic</A>).The arithmetic expression <VAR>expr2</VAR> is then evaluated repeatedlyuntil it evaluates to zero. Each time <VAR>expr2</VAR> evaluates to a non-zero value, <VAR>commands</VAR> areexecuted and the arithmetic expression <VAR>expr3</VAR> is evaluated. If any expression is omitted, it behaves as if it evaluates to 1.The return value is the exit status of the last command in <VAR>list</VAR>that is executed, or false if any of the expressions is invalid.</P><P></DL><P>The <CODE>break</CODE> and <CODE>continue</CODE> builtins (see section <A HREF="bashref.html#SEC56">4.1 Bourne Shell Builtins</A>)may be used to control loop execution.</P><P><A NAME="Conditional Constructs"></A><HR SIZE="6"><A NAME="SEC20"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC19"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC21"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC21"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC15"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC22"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC_About"> ? </A>]</TD></TR></TABLE><H3> 3.2.5 Conditional Constructs </H3><!--docid::SEC20::--><P><DL COMPACT><DT><CODE>if</CODE><DD><A NAME="IDX32"></A><A NAME="IDX33"></A><A NAME="IDX34"></A><A NAME="IDX35"></A><A NAME="IDX36"></A>The syntax of the <CODE>if</CODE> command is:<P><TABLE><tr><td> </td><td class=example><pre>if <VAR>test-commands</VAR>; then <VAR>consequent-commands</VAR>;[elif <VAR>more-test-commands</VAR>; then <VAR>more-consequents</VAR>;][else <VAR>alternate-consequents</VAR>;]fi</pre></td></tr></table></P><P>The <VAR>test-commands</VAR> list is executed, and if its return status is zero,the <VAR>consequent-commands</VAR> list is executed.If <VAR>test-commands</VAR> returns a non-zero status, each <CODE>elif</CODE> listis executed in turn, and if its exit status is zero,the corresponding <VAR>more-consequents</VAR> is executed and the command completes.If <SAMP>`else <VAR>alternate-consequents</VAR>'</SAMP> is present, andthe final command in the final <CODE>if</CODE> or <CODE>elif</CODE> clausehas a non-zero exit status, then <VAR>alternate-consequents</VAR> is executed.The return status is the exit status of the last command executed, orzero if no condition tested true.</P><P><DT><CODE>case</CODE><DD><A NAME="IDX37"></A><A NAME="IDX38"></A><A NAME="IDX39"></A>The syntax of the <CODE>case</CODE> command is:<P><TABLE><tr><td> </td><td class=example><pre><CODE>case <VAR>word</VAR> in [ [(] <VAR>pattern</VAR> [| <VAR>pattern</VAR>]<small>...</small>) <VAR>command-list</VAR> ;;]<small>...</small> esac</CODE></pre></td></tr></table></P><P><CODE>case</CODE> will selectively execute the <VAR>command-list</VAR> corresponding tothe first <VAR>pattern</VAR> that matches <VAR>word</VAR>.The <SAMP>`|'</SAMP> is used to separate multiple patterns, and the <SAMP>`)'</SAMP>operator terminates a pattern list.A list of patterns and an associated command-list is knownas a <VAR>clause</VAR>. Each clause must be terminated with <SAMP>`;;'</SAMP>.The <VAR>word</VAR> undergoes tilde expansion, parameter expansion, commandsubstitution, arithmetic expansion, and quote removal before matching isattempted. Each <VAR>pattern</VAR> undergoes tilde expansion, parameterexpansion, command substitution, and arithmetic expansion.</P><P>There may be an arbitrary number of <CODE>case</CODE> clauses, each terminatedby a <SAMP>`;;'</SAMP>. The first pattern that matches determines thecommand-list that is executed.</P><P>Here is an example using <CODE>case</CODE> in a script that could be used todescribe one interesting feature of an animal:</P><P><TABLE><tr><td> </td><td class=example><pre>echo -n "Enter the name of an animal: "read ANIMALecho -n "The $ANIMAL has "case $ANIMAL in horse | dog | cat) echo -n "four";; man | kangaroo ) echo -n "two";; *) echo -n "an unknown number of";;esacecho " legs."</pre></td></tr></table></P><P>The return status is zero if no <VAR>pattern</VAR> is matched. Otherwise, thereturn status is the exit status of the <VAR>command-list</VAR> executed.</P><P><DT><CODE>select</CODE><DD><A NAME="IDX40"></A><P>The <CODE>select</CODE> construct allows the easy generation of menus.It has almost the same syntax as the <CODE>for</CODE> command:</P><P><TABLE><tr><td> </td><td class=example><pre>select <VAR>name</VAR> [in <VAR>words</VAR> <small>...</small>]; do <VAR>commands</VAR>; done</pre></td></tr></table></P><P>The list of words following <CODE>in</CODE> is expanded, generating a listof items. The set of expanded words is printed on the standarderror output stream, each preceded by a number. If the<SAMP>`in <VAR>words</VAR>'</SAMP> is omitted, the positional parameters are printed,as if <SAMP>`in "$@"'</SAMP> had been specifed.The <CODE>PS3</CODE> prompt is then displayed and a line is read from thestandard input.If the line consists of a number corresponding to one of the displayedwords, then the value of <VAR>name</VAR> is set to that word.If the line is empty, the words and prompt are displayed again.If <CODE>EOF</CODE> is read, the <CODE>select</CODE> command completes.Any other value read causes <VAR>name</VAR> to be set to null.The line read is saved in the variable <CODE>REPLY</CODE>.</P><P>The <VAR>commands</VAR> are executed after each selection until a<CODE>break</CODE> command is executed, at whichpoint the <CODE>select</CODE> command completes.</P><P>Here is an example that allows the user to pick a filename from thecurrent directory, and displays the name and index of the fileselected.</P><P><TABLE><tr><td> </td><td class=example><pre>select fname in *;do echo you picked $fname \($REPLY\) break;done</pre></td></tr></table></P><P><DT><CODE>((<small>...</small>))</CODE><DD><TABLE><tr><td> </td><td class=example><pre>(( <VAR>expression</VAR> ))</pre></td></tr></table><P>The arithmetic <VAR>expression</VAR> is evaluated according to the rulesdescribed below (see section <A HREF="bashref.html#SEC78">6.5 Shell Arithmetic</A>).If the value of the expression is non-zero, the return status is 0;otherwise the return status is 1. This is exactly equivalent to<TABLE><tr><td> </td><td class=example><pre>let "<VAR>expression</VAR>"</pre></td></tr></table>See section <A HREF="bashref.html#SEC57">4.2 Bash Builtin Commands</A>, for a full description of the <CODE>let</CODE> builtin.</P><P><DT><CODE>[[<small>...</small>]]</CODE><DD><A NAME="IDX41"></A><A NAME="IDX42"></A><TABLE><tr><td> </td><td class=example><pre>[[ <VAR>expression</VAR> ]]</pre></td></tr></table><P>Return a status of 0 or 1 depending on the evaluation ofthe conditional expression <VAR>expression</VAR>.Expressions are composed of the primaries described below in<A HREF="bashref.html#SEC77">6.4 Bash Conditional Expressions</A>.Word splitting and filename expansion are not performed on the wordsbetween the <SAMP>`[['</SAMP> and <SAMP>`]]'</SAMP>; tilde expansion, parameter andvariable expansion, arithmetic expansion, command substitution, processsubstitution, and quote removal are performed.</P><P>When the <SAMP>`=='</SAMP> and <SAMP>`!='</SAMP> operators are used, the string to theright of the operator is considered a pattern and matched accordingto the rules described below in <A HREF="bashref.html#SEC35">3.5.8.1 Pattern Matching</A>.The return value is 0 if the string matches or does not matchthe pattern, respectively, and 1 otherwise.Any part of the pattern may be quoted to force it to be matched as astring.</P><P>Expressions may be combined using the following operators, listedin decreasing order of precedence:</P><P><DL COMPACT><DT><CODE>( <VAR>expression</VAR> )</CODE><DD>Returns the value of <VAR>expression</VAR>.This may be used to override the normal precedence of operators.<P><DT><CODE>! <VAR>expression</VAR></CODE><DD>True if <VAR>expression</VAR> is false.<P><DT><CODE><VAR>expression1</VAR> && <VAR>expression2</VAR></CODE><DD>True if both <VAR>expression1</VAR> and <VAR>expression2</VAR> are true.<P><DT><CODE><VAR>expression1</VAR> || <VAR>expression2</VAR></CODE><DD>True if either <VAR>expression1</VAR> or <VAR>expression2</VAR> is true.</DL>The <CODE>&&</CODE> and <CODE>||</CODE> operators do not evaluate <VAR>expression2</VAR> if thevalue of <VAR>expression1</VAR> is sufficient to determine the returnvalue of the entire conditional expression.<P></DL><P><A NAME="Command Grouping"></A><HR SIZE="6"><A NAME="SEC21"></A><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC20"> < </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC22"> > </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC22"> << </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC15"> Up </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC22"> >> </A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC_Top">Top</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC_Contents">Contents</A>]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bashref.html#SEC_About"> ? </A>]</TD></TR></TABLE><H3> 3.2.6 Grouping Commands </H3><!--docid::SEC21::--><P>Bash provides two ways to group a list of commands to be executedas a unit. When commands are grouped, redirections may be appliedto the entire command list. For example, the output of all
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -