📄 special-chars.html
字号:
the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN"><</SPAN>"</SPAN> and <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">></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"><<</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"><<<</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"><</SPAN>, <SPANCLASS="TOKEN">></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 veg1=carrots 2 veg2=tomatoes 3 4 if [[ "$veg1" < "$veg2" ]] 5 then 6 echo "Although $veg1 precede $veg2 in the dictionary," 7 echo -n "this does not necessarily imply anything " 8 echo "about my culinary preferences." 9 else 10 echo "What kind of dictionary are you using, anyhow?" 11 fi</PRE></TD></TR></TABLE></P></DIV></DD><DT><SPANCLASS="TOKEN">\<</SPAN>, <SPANCLASS="TOKEN">\></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 '\<the\>' 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 echo ls -l | sh 2 # Passes the output of "echo ls -l" to the shell, 3 #+ with the same result as a simple "ls -l". 4 5 6 cat *.lst | sort | uniq 7 # 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 #!/bin/bash 2 # uppercase.sh : Changes input to uppercase. 3 4 tr 'a-z' 'A-Z' 5 # Letter ranges must be quoted 6 #+ to prevent filename generation from single-letter filenames. 7 8 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 cat file1 file2 | ls -l | sort 2 # 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 variable="initial_value" 2 echo "new_value" | read variable 3 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">>|</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">&</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Run job in background. </B>A command followed by an <SPANCLASS="TOKEN">&</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 &</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 #!/bin/bash 2 # background-loop.sh 3 4 for i in 1 2 3 4 5 6 7 8 9 10 # First loop. 5 do 6 echo -n "$i " 7 done & # Run this loop in background. 8 # Will sometimes execute after second loop. 9 10 echo # This 'echo' sometimes will not display. 11 12 for i in 11 12 13 14 15 16 17 18 19 20 # Second loop. 13 do 14 echo -n "$i " 15 done 16 17 echo # This 'echo' sometimes will not display. 18 19 # ====================================================== 20 21 # The expected output from the script: 22 # 1 2 3 4 5 6 7 8 9 10 23 # 11 12 13 14 15 16 17 18 19 20 24 25 # Sometimes, though, you get: 26 # 11 12 13 14 15 16 17 18 19 20 27 # 1 2 3 4 5 6 7 8 9 10 bozo $ 28 # (The second 'echo' doesn't execute. Why?) 29 30 # Occasionally also: 31 # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 32 # (The first 'echo' doesn't execute. Why?) 33 34 # Very rarely something like: 35 # 11 12 13 1 2 3 4 5 6 7 8 9 10 14 15 16 17 18 19 20 36 # The foreground loop preempts the background one. 37 38 exit 0 39 40 # Nasimuddin Ansari suggests adding sleep 1 41 #+ after the echo -n "$i" in lines 6 and 14, 42 #+ 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">&&</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">&&</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 if [ $file1 -ot $file2 ] 2 then # ^ 3 echo "File $file1 is older than $file2." 4 fi 5 6 if [ "$a" -eq "$b" ] 7 then ^ 8 echo "$a is equal to $b." 9 fi 10 11 if [ "$c" -eq 24 -a "$d" -eq 47 ] 12 then ^ ^ 13 echo "$c equals 24 and $d equals 47." 14 fi 15 16 17 param2=${param1:-$DEFAULTVAL} 18 # ^</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 + -