📄 special-chars.html
字号:
CLASS="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>No spaces allowed within the braces <ICLASS="EMPHASIS">unless</I> the spaces are quoted or escaped.</P><P><TTCLASS="USERINPUT"><B>echo {file1,file2}\ :{\ A," B",' C'}</B></TT></P><P><TTCLASS="COMPUTEROUTPUT">file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C</TT></P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">{}</SPAN></DT><DD><P><ANAME="CODEBLOCKREF"></A></P><DIVCLASS="FORMALPARA"><P><B>Block of code [curly brackets]. </B>Also referred to as an <SPANCLASS="QUOTE">"inline group"</SPAN>, this construct, in effect, creates an anonymous function. However, unlike a <AHREF="functions.html#FUNCTIONREF">function</A>, the variables in a code block remain visible to the remainder of the script.</P></DIV><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>{ local a; a=123; }</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: local: can only be used in afunction</TT> </PRE></TD></TR></TABLE> </P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 a=123 2 { a=321; } 3 echo "a = $a" # a = 321 (value inside code block) 4 5 # Thanks, S.C.</PRE></TD></TR></TABLE></P><P>The code block enclosed in braces may have <AHREF="io-redirection.html#IOREDIRREF">I/O redirected</A> to and from it.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX8"></A><P><B>Example 3-1. Code blocks and I/O redirection</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Reading lines in /etc/fstab. 3 4 File=/etc/fstab 5 6 { 7 read line1 8 read line2 9 } < $File 10 11 echo "First line in $File is:" 12 echo "$line1" 13 echo 14 echo "Second line in $File is:" 15 echo "$line2" 16 17 exit 0 18 19 # Now, how do you parse the separate fields of each line? 20 # Hint: use awk.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="RPMCHECK"></A><P><B>Example 3-2. Saving the results of a code block to a file</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # rpm-check.sh 3 4 # Queries an rpm file for description, listing, and whether it can be installed. 5 # Saves output to a file. 6 # 7 # This script illustrates using a code block. 8 9 SUCCESS=0 10 E_NOARGS=65 11 12 if [ -z "$1" ] 13 then 14 echo "Usage: `basename $0` rpm-file" 15 exit $E_NOARGS 16 fi 17 18 { 19 echo 20 echo "Archive Description:" 21 rpm -qpi $1 # Query description. 22 echo 23 echo "Archive Listing:" 24 rpm -qpl $1 # Query listing. 25 echo 26 rpm -i --test $1 # Query whether rpm file can be installed. 27 if [ "$?" -eq $SUCCESS ] 28 then 29 echo "$1 can be installed." 30 else 31 echo "$1 cannot be installed." 32 fi 33 echo 34 } > "$1.test" # Redirects output of everything in block to file. 35 36 echo "Results of rpm test in file $1.test" 37 38 # See rpm man page for explanation of options. 39 40 exit 0</PRE></TD></TR></TABLE><HR></DIV><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>Unlike a command group within (parentheses), as above, a code block enclosed by {braces} will <ICLASS="EMPHASIS">not</I> normally launch a <AHREF="subshells.html#SUBSHELLSREF">subshell</A>. <ANAME="AEN931"HREF="#FTN.AEN931">[2]</A> </P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">{} \;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>pathname. </B>Mostly used in <AHREF="moreadv.html#FINDREF">find</A> constructs. This is <ICLASS="EMPHASIS">not</I> a shell <AHREF="internal.html#BUILTINREF">builtin</A>.</P></DIV><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 <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">;</SPAN>"</SPAN> ends the <TTCLASS="OPTION">-exec</TT> option of a <BCLASS="COMMAND">find</B> command sequence. It needs to be escaped to protect it from interpretation by the shell.</P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">[ ]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>test. </B></P></DIV><P><ANAME="LEFTBRACKET"></A><AHREF="tests.html#IFTHEN">Test</A> expression between <BCLASS="COMMAND">[ ]</B>. Note that <BCLASS="COMMAND">[</B> is part of the shell builtin <BCLASS="COMMAND">test</B> (and a synonym for it), <ICLASS="EMPHASIS">not</I> a link to the external command <TTCLASS="FILENAME">/usr/bin/test</TT>.</P></DD><DT><SPANCLASS="TOKEN">[[ ]]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>test. </B></P></DIV><P>Test expression between <SPANCLASS="TOKEN">[[ ]]</SPAN> (shell <AHREF="internal.html#KEYWORDREF">keyword</A>).</P><P>See the discussion on the <AHREF="tests.html#DBLBRACKETS">[[ ... ]] construct</A>.</P></DD><DT><SPANCLASS="TOKEN">[ ]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>array element. </B></P></DIV><P>In the context of an <AHREF="arrays.html#ARRAYREF">array</A>, brackets set off the numbering of each element of that array. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 Array[1]=slot_1 2 echo ${Array[1]}</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">[ ]</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>range of characters. </B></P></DIV><P>As part of a <AHREF="regexp.html#REGEXREF">regular expression</A>, brackets delineate a <AHREF="regexp.html#BRACKETSREF">range of characters</A> to match.</P></DD><DT><SPANCLASS="TOKEN">(( ))</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>integer expansion. </B></P></DIV><P>Expand and evaluate integer expression between <SPANCLASS="TOKEN">(( ))</SPAN>.</P><P>See the discussion on the <AHREF="dblparens.html">(( ... )) construct</A>.</P></DD><DT><SPANCLASS="TOKEN">></SPAN> <SPANCLASS="TOKEN">&></SPAN> <SPANCLASS="TOKEN">>&</SPAN> <SPANCLASS="TOKEN">>></SPAN> <SPANCLASS="TOKEN"><</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="io-redirection.html#IOREDIRREF">redirection</A>. </B></P></DIV><P><TTCLASS="USERINPUT"><B>scriptname >filename</B></TT> redirects the output of <TTCLASS="FILENAME">scriptname</TT> to file <TTCLASS="FILENAME">filename</TT>. Overwrite <TTCLASS="FILENAME">filename</TT> if it already exists.</P><P><TTCLASS="USERINPUT"><B>command &>filename</B></TT> redirects both the <TTCLASS="FILENAME">stdout</TT> and the <TTCLASS="FILENAME">stderr</TT> of <TTCLASS="FILENAME">command</TT> to <TTCLASS="FILENAME">filename</TT>.</P><P><TTCLASS="USERINPUT"><B>command >&2</B></TT> redirects <TTCLASS="FILENAME">stdout</TT> of <TTCLASS="FILENAME">command</TT> to <TTCLASS="FILENAME">stderr</TT>.</P><P><TTCLASS="USERINPUT"><B>scriptname >>filename</B></TT> appends the output of <TTCLASS="FILENAME">scriptname</TT> to file <TTCLASS="FILENAME">filename</TT>. If <TTCLASS="FILENAME">filename</TT> does not already exist, it will be created.</P><DIVCLASS="FORMALPARA"><P><B><AHREF="process-sub.html#PROCESSSUBREF">process substitution</A>. </B></P></DIV><P><TTCLASS="USERINPUT"><B>(command)></B></TT></P><P><TTCLASS="USERINPUT"><B><(command)</B></TT></P><P><AHREF="comparison-ops.html#LTREF">In a different context</A>, the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN"><</SPAN>"</SPAN> and <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">></SPAN>"</SPAN> characters act as <AHREF="comparison-ops.html#SCOMPARISON1">string comparison operators</A>.</P><P><AHREF="comparison-ops.html#INTLT">In yet another context</A>, 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 12-9</A>.</P></DD><DT><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><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 "this implies nothing about my culinary preferences." 8 else 9 echo "What kind of dictionary are you using, anyhow?" 10 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 of previous command to the input 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="AEN1202"></A><P>A pipe, as a classic method of interprocess communication, sends the <TTCLASS="FILENAME">stdout</TT> of one process 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 <SPANCLASS="QUOTE">"filter"</SPAN> (a command that transforms its input) for processing.</P><P> <TTCLASS="USERINPUT"><B>cat $filename1 $filename2 | grep $search_word</B></TT> </P></DIV></TD></TR></TABLE><P><ANAME="UCREF"></A>The output of a command or commands may be piped to a script. <TABLEBORDER="0"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -