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

📄 special-chars.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
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="EMPHASIS">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="EMPHASIS">broken pipe</I>, this	       condition sends a <ICLASS="EMPHASIS">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		<ICLASS="EMPHASIS">either</I> of the linked test conditions		is true.</P></DIV></DD><DT><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><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><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		<ICLASS="EMPHASIS">both</I> 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.</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><TTCLASS="USERINPUT"><B>set -- $variable</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</PRE></TD></TR></TABLE>  	      </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">stdout</TT> [dash]. </B><ANAME="COXEX"></A></P></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;(cd /source/directory &#38;&#38; tar cf - . ) | (cd /dest/directory &#38;&#38; tar xpvf -)   2&nbsp;# Move entire file tree from one directory to another   3&nbsp;# [courtesy Alan Cox &#60;a.cox@swansea.ac.uk&#62;, with a minor change]   4&nbsp;   5&nbsp;# 1) cd /source/directory    Source directory, where the files to be moved are.   6&nbsp;# 2) &#38;&#38;                     "And-list": if the 'cd' operation successful, then execute the next command.   7&nbsp;# 3) tar cf - .              The 'c' option 'tar' archiving command creates a new archive,   8&nbsp;#                            the 'f' (file) option, followed by '-' designates the target file as stdout,   9&nbsp;#                            and do it in current directory tree ('.').  10&nbsp;# 4) |                       Piped to...  11&nbsp;# 5) ( ... )                 a subshell  12&nbsp;# 6) cd /dest/directory      Change to the destination directory.  13&nbsp;# 7) &#38;&#38;                     "And-list", as above  14&nbsp;# 8) tar xpvf -              Unarchive ('x'), preserve ownership and file permissions ('p'),  15&nbsp;#                            and send verbose messages to stdout ('v'),  16&nbsp;#                            reading data from stdin ('f' followed by '-').  17&nbsp;#  18&nbsp;#                            Note that 'x' is a command, and 'p', 'v', 'f' are options.  19&nbsp;# Whew!  20&nbsp;  21&nbsp;  22&nbsp;  23&nbsp;# More elegant than, but equivalent to:  24&nbsp;#   cd source/directory  25&nbsp;#   tar cf - . | (cd ../dest/directory; tar xpvf -)  26&nbsp;#  27&nbsp;#     Also having same effect:  28&nbsp;# cp -a /source/directory/* /dest/directory  29&nbsp;#     Or:  30&nbsp;# cp -a /source/directory/* /source/directory/.[^.]* /dest/directory  31&nbsp;#     If there are hidden files in /source/directory.</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;bunzip2 linux-2.6.13.tar.bz2 | tar xvf -   2&nbsp;# --uncompress tar file--    | --then pass it to "tar"--   3&nbsp;# If "tar" has not been patched to handle "bunzip2",   4&nbsp;# this needs to be done in two discrete steps, using a pipe.   5&nbsp;# The purpose of the exercise is to unarchive "bzipped" kernel source.</PRE></TD></TR></TABLE></P><P>Note that in this context the <SPANCLASS="QUOTE">"-"</SPAN> is not            itself a Bash operator, but rather an option recognized by	    certain UNIX utilities that write to	    <TTCLASS="FILENAME">stdout</TT>, such as <BCLASS="COMMAND">tar</B>,	    <BCLASS="COMMAND">cat</B>, etc.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "whatever" | cat -</B></TT> <TTCLASS="COMPUTEROUTPUT">whatever</TT> </PRE></TD></TR></TABLE>	    </P><P>Where a filename is expected,	      <TTCLASS="REPLACEABLE"><I>-</I></TT> redirects output to	      <TTCLASS="FILENAME">stdout</TT> (sometimes seen with	      <TTCLASS="USERINPUT"><B>tar cf</B></TT>), or accepts input from	      <TTCLASS="FILENAME">stdin</TT>, rather than from a file. This	      is a method of using a file-oriented utility as a filter	      in a pipe.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>file</B></TT> <TTCLASS="COMPUTEROUTPUT">Usage: file [-bciknvzL] [-f namefile] [-m magicfiles] file...</TT> 	      </PRE></TD></TR></TABLE>	    By itself on the command line, <AHREF="filearchiv.html#FILEREF">file</A> fails with an error message.	    </P><P>	    Add a <SPANCLASS="QUOTE">"-"</SPAN> for a more useful result. This causes the	      shell to await user input.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>file -</B></TT> <TTCLASS="USERINPUT"><B>abc</B></TT> <TTCLASS="COMPUTEROUTPUT">standard input:              ASCII text</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>file -</B></TT> <TTCLASS="USERINPUT"><B>#!/bin/bash</B></TT> <TTCLASS="COMPUTEROUTPUT">standard input:              Bourne-Again shell script text executable</TT> 	      </PRE></TD></TR></TABLE>	      Now the command accepts input from <TTCLASS="FILENAME">stdin</TT>	        and analyzes it.	    </P><P>The <SPANCLASS="QUOTE">"-"</SPAN> can be used to pipe	      <TTCLASS="FILENAME">stdout</TT> to other commands. This permits	      such stunts as <AHREF="assortedtips.html#PREPENDREF">prepending lines	      to a file</A>.</P><P>Using <AHREF="filearchiv.html#DIFFREF">diff</A> to	      compare a file with a <ICLASS="EMPHASIS">section</I>	      of another:</P><P><TTCLASS="USERINPUT"><B>grep Linux file1 | diff file2 -</B></TT></P><P>Finally, a real-world example using	      <TTCLASS="REPLACEABLE"><I>-</I></TT> with <AHREF="filearchiv.html#TARREF">tar</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX58"></A><P><B>Example 3-4. Backup of all files changed in last day</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  Backs up all files in current directory modified within last 24 hours   4&nbsp;#+ in a "tarball" (tarred and gzipped file).   5&nbsp;   6&nbsp;BACKUPFILE=backup-$(date +%m-%d-%Y)   7&nbsp;#                 Embeds date in backup filename.   8&nbsp;#                 Thanks, Joshua Tschida, for the idea.   9&nbsp;archive=${1:-$BACKUPFILE}  10&nbsp;#  If no backup-archive filename specified on command line,  11&nbsp;#+ it will default to "backup-MM-DD-YYYY.tar.gz."  12&nbsp;  13&nbsp;tar cvf - `find . -mtime -1 -type f -print` &#62; $archive.tar  14&nbsp;gzip $archive.tar  15&nbsp;echo "Directory $PWD backed up in archive file \"$archive.tar.gz\"."  16&nbsp;  17&nbsp;  18&nbsp;#  Stephane Chazelas points out that the above code will fail  19&nbsp;#+ if there are too many files found  20&nbsp;#+ or if any filenames contain blank characters.  21&nbsp;  22&nbsp;# He suggests the following alternatives:  23&nbsp;# -------------------------------------------------------------------  24&nbsp;#   find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar"  25&nbsp;#      using the GNU version of "find".  26&nbsp;  27&nbsp;  28&nbsp;#   find . -mtime -1 -type f -exec tar rvf "$archive.tar" '{}' \;  29&nbsp;#         portable to other UNIX flavors, but much slower.  30&nbsp;# -------------------------------------------------------------------  31&nbsp;  32&nbsp;  33&nbsp;exit 0</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>Filenames beginning with	      <SPANCLASS="QUOTE">"-"</SPAN> may cause problems when coupled with the	      <SPANCLASS="QUOTE">"-"</SPAN> redirection operator. A script should	      check for this and add an appropriate prefix to such	      filenames, for example <TTCLASS="FILENAME">./-FILENAME</TT>,	      <TTCLASS="FILENAME">$PWD/-FILENAME</TT>, or	      <TTCLASS="FILENAME">$PATHNAME/-FILENAME</TT>.</P><P>If the value of a variable begins with a	        <TTCLASS="REPLACEABLE"><I>-</I></TT>, this may likewise create		problems.		<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;var="-n"   2&nbsp;echo $var		   3&nbsp;# Has the effect of "echo -n", and outputs nothing.</PRE></TD></TR></TABLE>              </P

⌨️ 快捷键说明

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