📄 special-chars.html
字号:
>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 (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -) 2 # Move entire file tree from one directory to another 3 # [courtesy Alan Cox <a.cox@swansea.ac.uk>, with a minor change] 4 5 # 1) cd /source/directory 6 # Source directory, where the files to be moved are. 7 # 2) && 8 # "And-list": if the 'cd' operation successful, 9 # then execute the next command. 10 # 3) tar cf - . 11 # The 'c' option 'tar' archiving command creates a new archive, 12 # the 'f' (file) option, followed by '-' designates the target file 13 # as stdout, and do it in current directory tree ('.'). 14 # 4) | 15 # Piped to ... 16 # 5) ( ... ) 17 # a subshell 18 # 6) cd /dest/directory 19 # Change to the destination directory. 20 # 7) && 21 # "And-list", as above 22 # 8) tar xpvf - 23 # Unarchive ('x'), preserve ownership and file permissions ('p'), 24 # and send verbose messages to stdout ('v'), 25 # reading data from stdin ('f' followed by '-'). 26 # 27 # Note that 'x' is a command, and 'p', 'v', 'f' are options. 28 # 29 # Whew! 30 31 32 33 # More elegant than, but equivalent to: 34 # cd source/directory 35 # tar cf - . | (cd ../dest/directory; tar xpvf -) 36 # 37 # Also having same effect: 38 # cp -a /source/directory/* /dest/directory 39 # Or: 40 # cp -a /source/directory/* /source/directory/.[^.]* /dest/directory 41 # 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 bunzip2 -c linux-2.6.16.tar.bz2 | tar xvf - 2 # --uncompress tar file-- | --then pass it to "tar"-- 3 # If "tar" has not been patched to handle "bunzip2", 4 #+ this needs to be done in two discrete steps, using a pipe. 5 # 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 #!/bin/bash 2 3 # Backs up all files in current directory modified within last 24 hours 4 #+ in a "tarball" (tarred and gzipped file). 5 6 BACKUPFILE=backup-$(date +%m-%d-%Y) 7 # Embeds date in backup filename. 8 # Thanks, Joshua Tschida, for the idea. 9 archive=${1:-$BACKUPFILE} 10 # If no backup-archive filename specified on command line, 11 #+ it will default to "backup-MM-DD-YYYY.tar.gz." 12 13 tar cvf - `find . -mtime -1 -type f -print` > $archive.tar 14 gzip $archive.tar 15 echo "Directory $PWD backed up in archive file \"$archive.tar.gz\"." 16 17 18 # Stephane Chazelas points out that the above code will fail 19 #+ if there are too many files found 20 #+ or if any filenames contain blank characters. 21 22 # He suggests the following alternatives: 23 # ------------------------------------------------------------------- 24 # find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar" 25 # using the GNU version of "find". 26 27 28 # find . -mtime -1 -type f -exec tar rvf "$archive.tar" '{}' \; 29 # portable to other UNIX flavors, but much slower. 30 # ------------------------------------------------------------------- 31 32 33 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 var="-n" 2 echo $var 3 # 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 a=28 2 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 + -