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

📄 special-chars.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
>stdout</TT> [dash]. </B><ANAME="COXEX"></A></P></DIV><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat -</B></TT> <TTCLASS="USERINPUT"><B>abc</B></TT> <TTCLASS="COMPUTEROUTPUT">abc</TT>  <TTCLASS="COMPUTEROUTPUT">...</TT>  <TTCLASS="USERINPUT"><B>Ctl-D</B></TT></PRE></TD></TR></TABLE>	    </P><P>As expected, <TTCLASS="USERINPUT"><B>cat -</B></TT> echoes	    <TTCLASS="FILENAME">stdin</TT>, in this case keyboarded user input,	    to <TTCLASS="FILENAME">stdout</TT>. But, does I/O redirection using	    <BCLASS="COMMAND">-</B> have real-world applications?</P><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   6&nbsp;#    Source directory, where the files to be moved are.   7&nbsp;# 2) &#38;&#38;   8&nbsp;#   "And-list": if the 'cd' operation successful,   9&nbsp;#    then execute the next command.  10&nbsp;# 3) tar cf - .  11&nbsp;#    The 'c' option 'tar' archiving command creates a new archive,  12&nbsp;#    the 'f' (file) option, followed by '-' designates the target file  13&nbsp;#    as stdout, and do it in current directory tree ('.').  14&nbsp;# 4) |  15&nbsp;#    Piped to ...  16&nbsp;# 5) ( ... )  17&nbsp;#    a subshell  18&nbsp;# 6) cd /dest/directory  19&nbsp;#    Change to the destination directory.  20&nbsp;# 7) &#38;&#38;  21&nbsp;#   "And-list", as above  22&nbsp;# 8) tar xpvf -  23&nbsp;#    Unarchive ('x'), preserve ownership and file permissions ('p'),  24&nbsp;#    and send verbose messages to stdout ('v'),  25&nbsp;#    reading data from stdin ('f' followed by '-').  26&nbsp;#  27&nbsp;#    Note that 'x' is a command, and 'p', 'v', 'f' are options.  28&nbsp;#  29&nbsp;# Whew!  30&nbsp;  31&nbsp;  32&nbsp;  33&nbsp;# More elegant than, but equivalent to:  34&nbsp;#   cd source/directory  35&nbsp;#   tar cf - . | (cd ../dest/directory; tar xpvf -)  36&nbsp;#  37&nbsp;#     Also having same effect:  38&nbsp;# cp -a /source/directory/* /dest/directory  39&nbsp;#     Or:  40&nbsp;# cp -a /source/directory/* /source/directory/.[^.]* /dest/directory  41&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 -c linux-2.6.16.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.	      <ANAME="FILTERDASH"></A>	      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 <SPANCLASS="emphasis"><ICLASS="EMPHASIS">section</I></SPAN>	      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></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">-</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>previous working directory. </B>A <BCLASS="COMMAND">cd -</B> command changes to the		previous working directory. This uses the		<AHREF="variables2.html#OLDPWD">$OLDPWD</A> <AHREF="othertypesv.html#ENVREF">environmental variable</A>.</P></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>Do not confuse the <SPANCLASS="QUOTE">"-"</SPAN> used in this		sense with the <SPANCLASS="QUOTE">"-"</SPAN> redirection		operator just discussed. The interpretation of the		<SPANCLASS="QUOTE">"-"</SPAN> depends on the context in which it		appears.</P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">-</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Minus. </B>Minus sign in an <AHREF="operations.html#AROPS1">arithmetic	        operation</A>.</P></DIV></DD><DT><SPANCLASS="TOKEN">=</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Equals. </B><AHREF="varassignment.html#EQREF">Assignment operator</A>	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;a=28   2&nbsp;echo $a   # 28</PRE></TD></TR></TABLE></P></DIV><P>In a <AHREF="comparison-ops.html#EQUALSIGNREF">different context</A>,	      the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">=</SPAN>"</SPAN> is a <AHREF="comparison-ops.html#SCOMPARISON1">string comparison</A>	      operator.</P></DD><DT><SPANCLASS="TOKEN">+</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Plus. </B>Addition  <AHREF="operations.html#AROPS1">arithmetic	        operator</A>.</P></DIV><P>In a <AHREF="regexp.html#PLUSREF">different context</A>,	      the <SPANCLASS="TOKEN">+</SPAN> is a <AHREF="regexp.html">Regular	      Expression</A> operator.</P></DD><DT><SPANCLASS="TOKEN">+</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Option. </B>Option flag for a command or filter.</P></DIV><P>Certain commands and <AHREF="internal.html#BUILTINREF">builtins</A> use the	      <TTCLASS="OPTION">+</TT> to enable certain options and the	      <TTCLASS="OPTION">-</TT> to disable them. In <AHREF="parameter-substitution.html#PARAMSUBREF">parameter substitution</A>,	      the <TTCLASS="OPTION">+</TT> prefixes an <AHREF="parameter-substitution.html#PARAMALTV">	      alternate value</A> that a variable expands to.</P></DD><DT><ANAME="MODULO00"></A><SPANCLASS="TOKEN">%</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="operations.html#MODULOREF">modulo</A>. </B>Modulo (remainder of a division) <AHREF="operations.html#AROPS1">arithmetic	        operation</A>.</P></DIV><P>In a <AHREF="parameter-substitution.html#PCTPATREF">different context</A>,	      the <SPANCLASS="TOKEN">%</SPAN> is a <AHREF="parameter-substitution.html#PSUB2">pattern	      matching</A> operator.</P></DD><DT><ANAME="TILDEREF"></A><SPANCLASS="TOKEN">~</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>home directory [tilde]. </B>This corresponds to the <AHREF="variables2.html#HOMEDIRREF">$HOME</A> internal variable.	      <TTCLASS="FILENAME">~bozo</TT> is bozo's home directory,		and <BCLASS="COMMAND">ls ~bozo</B> lists the contents of it.		<SPANCLASS="TOKEN">~/</SPAN> is the current user's home directory,		and <BCLASS="COMMAND">ls ~/</B> lists the contents of it.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ~bozo</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ~</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ~/</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo/</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ~:</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo:</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo ~nonexistent-user</B></TT> <TTCLASS="COMPUTEROUTPUT">~nonexistent-user</TT> 	      </PRE></TD></TR></TABLE>	      </P></DIV></DD><DT><ANAME="WORKINGDIRREF"></A><SPANCLASS="TOKEN">~+</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>current working directory. </B>This corresponds to the <AHREF="variables2.html#PWDREF">$PWD</A> internal variable.</P></DIV></DD><DT><ANAME="PREVWORKINGDIR"></A><SPANCLASS="TOKEN">~-</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>previous working directory. </B>This corresponds to the <AHREF="variables2.html#OLDPWD">$OLDPWD</A> internal variable.</P></DIV></DD><DT><SPANCLASS="TOKEN">=~</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="bashver3.html#REGEXMATCHREF">regular	  expression match</A>. </B>This operator was introduced with <AHREF="bashver3.html#BASH3REF">version 3</A

⌨️ 快捷键说明

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