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

📄 special-chars.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
	      the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">&#60;</SPAN>"</SPAN> and	      <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">&#62;</SPAN>"</SPAN> characters act	      as <AHREF="comparison-ops.html#ICOMPARISON1">integer comparison	      operators</A>. See also <AHREF="moreadv.html#EX45">Example 15-9</A>.</P></DD><DT><ANAME="HEREDOCRRREF"></A><SPANCLASS="TOKEN">&#60;&#60;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>redirection used in a <AHREF="here-docs.html#HEREDOCREF">here document</A>. </B></P></DIV></DD><DT><ANAME="HERESTRINGREF"></A><SPANCLASS="TOKEN">&#60;&#60;&#60;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>redirection used in a <AHREF="here-docs.html#HERESTRINGSREF">here string</A>. </B></P></DIV></DD><DT><SPANCLASS="TOKEN">&#60;</SPAN>, <SPANCLASS="TOKEN">&#62;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="comparison-ops.html#LTREF">ASCII	    comparison</A>. </B><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;veg1=carrots   2&nbsp;veg2=tomatoes   3&nbsp;   4&nbsp;if [[ "$veg1" &#60; "$veg2" ]]   5&nbsp;then   6&nbsp;  echo "Although $veg1 precede $veg2 in the dictionary,"   7&nbsp;  echo -n "this does not necessarily imply anything "   8&nbsp;  echo "about my culinary preferences."   9&nbsp;else  10&nbsp;  echo "What kind of dictionary are you using, anyhow?"  11&nbsp;fi</PRE></TD></TR></TABLE></P></DIV></DD><DT><SPANCLASS="TOKEN">\&#60;</SPAN>, <SPANCLASS="TOKEN">\&#62;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="regexp.html#ANGLEBRAC">word	    boundary</A> in a <AHREF="regexp.html#REGEXREF">regular	    expression</A>. </B></P></DIV><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>grep '\&#60;the\&#62;' textfile</B></TT></P></DD><DT><SPANCLASS="TOKEN">|</SPAN></DT><DD><P><ANAME="PIPEREF"></A></P><DIVCLASS="FORMALPARA"><P><B>pipe. </B>Passes the output (<TTCLASS="FILENAME">stdout</TT>		of a previous command to the input		(<TTCLASS="FILENAME">stdin</TT>) of the next one, or		to the shell. This is a method of chaining commands		together.</P></DIV><P>              <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo ls -l | sh   2&nbsp;#  Passes the output of "echo ls -l" to the shell,   3&nbsp;#+ with the same result as a simple "ls -l".   4&nbsp;   5&nbsp;   6&nbsp;cat *.lst | sort | uniq   7&nbsp;# Merges and sorts all ".lst" files, then deletes duplicate lines.</PRE></TD></TR></TABLE>	    </P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN1380"></A><P>	      A pipe, as a classic method of interprocess	      communication, sends the <TTCLASS="FILENAME">stdout</TT>	      of one <AHREF="special-chars.html#PROCESSREF">process</A> to the	      <TTCLASS="FILENAME">stdin</TT> of another. In a typical case,	      a command, such as <AHREF="external.html#CATREF">cat</A> or	      <AHREF="internal.html#ECHOREF">echo</A>, pipes a stream of	      data to a	      <ANAME="FILTERDEF"></A>	      <ICLASS="FIRSTTERM">filter</I>, a command that	      transforms its input for processing.		<ANAME="AEN1389"HREF="#FTN.AEN1389">[5]</A>	      </P><P>  	      <TTCLASS="USERINPUT"><B>cat $filename1 $filename2 | grep $search_word</B></TT>            </P><P>For an interesting note on the complexity of using UNIX	      pipes, see <AHREF="http://www.faqs.org/faqs/unix-faq/faq/part3/"TARGET="_top">the UNIX FAQ,	      Part 3</A>.</P></DIV></TD></TR></TABLE><P><ANAME="UCREF"></A>The output of a command or commands	      may be piped to a script.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# uppercase.sh : Changes input to uppercase.   3&nbsp;   4&nbsp;tr 'a-z' 'A-Z'   5&nbsp;#  Letter ranges must be quoted   6&nbsp;#+ to prevent filename generation from single-letter filenames.   7&nbsp;   8&nbsp;exit 0</PRE></TD></TR></TABLE>              Now, let us pipe the output of <BCLASS="COMMAND">ls -l</B> to this	      script.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l | ./uppercase.sh</B></TT> <TTCLASS="COMPUTEROUTPUT">-RW-RW-R--    1 BOZO  BOZO       109 APR  7 19:49 1.TXT -RW-RW-R--    1 BOZO  BOZO       109 APR 14 16:48 2.TXT -RW-R--R--    1 BOZO  BOZO       725 APR 20 20:56 DATA-FILE</TT> 	      </PRE></TD></TR></TABLE>	    </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <TTCLASS="FILENAME">stdout</TT> of each process in	       a pipe must be read as the <TTCLASS="FILENAME">stdin</TT>	       of the next. If this is not the case, the data stream	       will <ICLASS="FIRSTTERM">block</I>, and the pipe will not	       behave as expected.	         <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat file1 file2 | ls -l | sort   2&nbsp;# The output from "cat file1 file2" disappears.</PRE></TD></TR></TABLE>             </P><P>A pipe runs as a <AHREF="othertypesv.html#CHILDREF">child	       process</A>, and therefore cannot alter script	       variables.	         <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;variable="initial_value"   2&nbsp;echo "new_value" | read variable   3&nbsp;echo "variable = $variable"     # variable = initial_value</PRE></TD></TR></TABLE>             </P><P>If one of the commands in the pipe	       aborts, this prematurely terminates execution of the	       pipe. Called a <ICLASS="FIRSTTERM">broken pipe</I>, this	       condition sends a <ICLASS="FIRSTTERM">SIGPIPE</I> <AHREF="debugging.html#SIGNALD">signal</A>.</P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">&#62;|</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>force redirection (even if		the <AHREF="options.html#NOCLOBBERREF">noclobber option</A>		is set). </B>This will forcibly overwrite an existing file.</P></DIV></DD><DT><SPANCLASS="TOKEN">||</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="operations.html#ORREF">OR logical operator</A>. </B>In a <AHREF="tests.html#TESTCONSTRUCTS1">test		construct</A>, the <SPANCLASS="TOKEN">||</SPAN> operator causes		a return of <SPANCLASS="RETURNVALUE">0</SPAN> (success) if		<SPANCLASS="emphasis"><ICLASS="EMPHASIS">either</I></SPAN> of the linked test conditions		is true.</P></DIV></DD><DT><ANAME="BGJOB"></A><SPANCLASS="TOKEN">&#38;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Run job in background. </B>A command followed by an <SPANCLASS="TOKEN">&#38;</SPAN>	        will run in the background.</P></DIV><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>sleep 10 &#38;</B></TT> <TTCLASS="COMPUTEROUTPUT">[1] 850</TT> <TTCLASS="COMPUTEROUTPUT">[1]+  Done                    sleep 10</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>Within a script, commands and even <AHREF="loops.html#FORLOOPREF1">loops</A> may run in the	      background.</P><P><ANAME="BGLOOP0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="BGLOOP"></A><P><B>Example 3-3. Running a loop in the background</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# background-loop.sh   3&nbsp;   4&nbsp;for i in 1 2 3 4 5 6 7 8 9 10            # First loop.   5&nbsp;do   6&nbsp;  echo -n "$i "   7&nbsp;done &#38; # Run this loop in background.   8&nbsp;       # Will sometimes execute after second loop.   9&nbsp;  10&nbsp;echo   # This 'echo' sometimes will not display.  11&nbsp;  12&nbsp;for i in 11 12 13 14 15 16 17 18 19 20   # Second loop.  13&nbsp;do  14&nbsp;  echo -n "$i "  15&nbsp;done    16&nbsp;  17&nbsp;echo   # This 'echo' sometimes will not display.  18&nbsp;  19&nbsp;# ======================================================  20&nbsp;  21&nbsp;# The expected output from the script:  22&nbsp;# 1 2 3 4 5 6 7 8 9 10   23&nbsp;# 11 12 13 14 15 16 17 18 19 20   24&nbsp;  25&nbsp;# Sometimes, though, you get:  26&nbsp;# 11 12 13 14 15 16 17 18 19 20   27&nbsp;# 1 2 3 4 5 6 7 8 9 10 bozo $  28&nbsp;# (The second 'echo' doesn't execute. Why?)  29&nbsp;  30&nbsp;# Occasionally also:  31&nbsp;# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  32&nbsp;# (The first 'echo' doesn't execute. Why?)  33&nbsp;  34&nbsp;# Very rarely something like:  35&nbsp;# 11 12 13 1 2 3 4 5 6 7 8 9 10 14 15 16 17 18 19 20   36&nbsp;# The foreground loop preempts the background one.  37&nbsp;  38&nbsp;exit 0  39&nbsp;  40&nbsp;#  Nasimuddin Ansari suggests adding    sleep 1  41&nbsp;#+ after the   echo -n "$i"   in lines 6 and 14,  42&nbsp;#+ for some real fun.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>A command run in the background within a	      script may cause the script to hang, waiting	      for a keystroke. Fortunately, there is a <AHREF="internal.html#WAITHANG">remedy</A> for this.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="LOGICALAND"></A><SPANCLASS="TOKEN">&#38;&#38;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="operations.html#LOGOPS1">AND logical	    operator</A>. </B>In a <AHREF="tests.html#TESTCONSTRUCTS1">test		construct</A>, the <SPANCLASS="TOKEN">&#38;&#38;</SPAN> operator causes		a return of <SPANCLASS="RETURNVALUE">0</SPAN> (success) only if		<SPANCLASS="emphasis"><ICLASS="EMPHASIS">both</I></SPAN> the linked test conditions		are true.</P></DIV></DD><DT><ANAME="DASHREF"></A><SPANCLASS="TOKEN">-</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>option, prefix. </B>Option flag for a command or filter. Prefix for		an operator. Prefix for a <AHREF="parameter-substitution.html#DEFPARAM1">default parameter</A>		in <AHREF="parameter-substitution.html#PARAMSUBREF">parameter		substitution</A>.</P></DIV><P><TTCLASS="USERINPUT"><B>COMMAND -[Option1][Option2][...]</B></TT></P><P><TTCLASS="USERINPUT"><B>ls -al</B></TT></P><P><TTCLASS="USERINPUT"><B>sort -dfu $filename</B></TT></P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ $file1 -ot $file2 ]   2&nbsp;then #      ^   3&nbsp;  echo "File $file1 is older than $file2."   4&nbsp;fi   5&nbsp;   6&nbsp;if [ "$a" -eq "$b" ]   7&nbsp;then      ^   8&nbsp;  echo "$a is equal to $b."   9&nbsp;fi  10&nbsp;  11&nbsp;if [ "$c" -eq 24 -a "$d" -eq 47 ]  12&nbsp;then      ^              ^  13&nbsp;  echo "$c equals 24 and $d equals 47."  14&nbsp;fi  15&nbsp;  16&nbsp;  17&nbsp;param2=${param1:-$DEFAULTVAL}  18&nbsp;#               ^</PRE></TD></TR></TABLE>  	      </P><P><ANAME="DOUBLEDASHREF"></A></P><P><BCLASS="COMMAND">--</B></P><P>The <ICLASS="FIRSTTERM">double-dash</I>		<TTCLASS="OPTION">--</TT> prefixes <ICLASS="FIRSTTERM">long</I>		(verbatim) options to commands.</P><P><TTCLASS="USERINPUT"><B>sort --ignore-leading-blanks</B></TT></P><P>Used with a <AHREF="internal.html#BUILTINREF">Bash	        builtin</A>, it means the <ICLASS="FIRSTTERM">end of		options</I> to that particular command.</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This provides a handy means of removing		  files whose <SPANCLASS="emphasis"><ICLASS="EMPHASIS">names begin with a dash</I></SPAN>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l</B></TT> <TTCLASS="COMPUTEROUTPUT">-rw-r--r-- 1 bozo bozo 0 Nov 25 12:29 -badname</TT>   <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rm -- -badname</B></TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l</B></TT> <TTCLASS="COMPUTEROUTPUT">total 0</TT></PRE></TD></TR></TABLE>	    </P></TD></TR></TABLE></DIV><P>The <ICLASS="FIRSTTERM">double-dash</I> is also used in	        conjunction with <AHREF="internal.html#SETREF">set</A>.</P><P><TTCLASS="USERINPUT"><B>set -- $variable</B></TT> (as in <AHREF="internal.html#SETPOS">Example 14-18</A>)</P></DD><DT><ANAME="DASHREF2"></A><SPANCLASS="TOKEN">-</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>redirection from/to <TTCLASS="FILENAME">stdin</TT> or <TTCLASS="FILENAME"

⌨️ 快捷键说明

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