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

📄 special-chars.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
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&nbsp;a=123   2&nbsp;{ a=321; }   3&nbsp;echo "a = $a"   # a = 321   (value inside code block)   4&nbsp;   5&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# Reading lines in /etc/fstab.   3&nbsp;   4&nbsp;File=/etc/fstab   5&nbsp;   6&nbsp;{   7&nbsp;read line1   8&nbsp;read line2   9&nbsp;} &#60; $File  10&nbsp;  11&nbsp;echo "First line in $File is:"  12&nbsp;echo "$line1"  13&nbsp;echo  14&nbsp;echo "Second line in $File is:"  15&nbsp;echo "$line2"  16&nbsp;  17&nbsp;exit 0  18&nbsp;  19&nbsp;# Now, how do you parse the separate fields of each line?  20&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# rpm-check.sh   3&nbsp;   4&nbsp;# Queries an rpm file for description, listing, and whether it can be installed.   5&nbsp;# Saves output to a file.   6&nbsp;#    7&nbsp;# This script illustrates using a code block.   8&nbsp;   9&nbsp;SUCCESS=0  10&nbsp;E_NOARGS=65  11&nbsp;  12&nbsp;if [ -z "$1" ]  13&nbsp;then  14&nbsp;  echo "Usage: `basename $0` rpm-file"  15&nbsp;  exit $E_NOARGS  16&nbsp;fi    17&nbsp;  18&nbsp;{   19&nbsp;  echo  20&nbsp;  echo "Archive Description:"  21&nbsp;  rpm -qpi $1       # Query description.  22&nbsp;  echo  23&nbsp;  echo "Archive Listing:"  24&nbsp;  rpm -qpl $1       # Query listing.  25&nbsp;  echo  26&nbsp;  rpm -i --test $1  # Query whether rpm file can be installed.  27&nbsp;  if [ "$?" -eq $SUCCESS ]  28&nbsp;  then  29&nbsp;    echo "$1 can be installed."  30&nbsp;  else  31&nbsp;    echo "$1 cannot be installed."  32&nbsp;  fi    33&nbsp;  echo  34&nbsp;} &#62; "$1.test"       # Redirects output of everything in block to file.  35&nbsp;  36&nbsp;echo "Results of rpm test in file $1.test"  37&nbsp;  38&nbsp;# See rpm man page for explanation of options.  39&nbsp;  40&nbsp;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&nbsp;Array[1]=slot_1   2&nbsp;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">&#62;</SPAN> <SPANCLASS="TOKEN">&#38;&#62;</SPAN> <SPANCLASS="TOKEN">&#62;&#38;</SPAN> <SPANCLASS="TOKEN">&#62;&#62;</SPAN> <SPANCLASS="TOKEN">&#60;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="io-redirection.html#IOREDIRREF">redirection</A>. </B></P></DIV><P><TTCLASS="USERINPUT"><B>scriptname &#62;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 &#38;&#62;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 &#62;&#38;2</B></TT> redirects	      <TTCLASS="FILENAME">stdout</TT> of <TTCLASS="FILENAME">command</TT>	      to <TTCLASS="FILENAME">stderr</TT>.</P><P><TTCLASS="USERINPUT"><B>scriptname &#62;&#62;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)&#62;</B></TT></P><P><TTCLASS="USERINPUT"><B>&#60;(command)</B></TT></P><P><AHREF="comparison-ops.html#LTREF">In a different context</A>,	      the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">&#60;</SPAN>"</SPAN> and	      <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">&#62;</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">&#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 12-9</A>.</P></DD><DT><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><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 "this implies nothing about my culinary preferences."   8&nbsp;else   9&nbsp;  echo "What kind of dictionary are you using, anyhow?"  10&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 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&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="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 + -