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

📄 extmisc.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
>lpr</B>	      commands send file(s) to the print queue, to be printed as	      hard copy.	      <ANAME="AEN10453"HREF="#FTN.AEN10453">[2]</A>	      These commands trace the origin of their names to the	      line printers of another era.</P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>lp file1.txt</B></TT>	        or <TTCLASS="PROMPT">bash </TT><TTCLASS="USERINPUT"><B>lp		&#60;file1.txt</B></TT></P><P>It is often useful to pipe the formatted output from	        <BCLASS="COMMAND">pr</B> to <BCLASS="COMMAND">lp</B>.</P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pr -options file1.txt | lp</B></TT>	        </P><P>Formatting packages, such as <BCLASS="COMMAND">groff</B> and	        <ICLASS="EMPHASIS">Ghostscript</I> may send their output		directly to <BCLASS="COMMAND">lp</B>.</P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>groff -Tascii file.tr | lp</B></TT>	        </P><P><TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>gs -options | lp file.ps</B></TT>	        </P><P>Related commands are <BCLASS="COMMAND">lpq</B>, for viewing	        the print queue, and <BCLASS="COMMAND">lprm</B>, for removing		jobs from the print queue.</P></DD><DT><ANAME="TEEREF"></A><BCLASS="COMMAND">tee</B></DT><DD><P>[UNIX borrows an idea from the plumbing trade.]</P><P>This is a redirection operator, but with a difference. Like the	      plumber's <SPANCLASS="QUOTE">"tee,"</SPAN> it permits <SPANCLASS="QUOTE">"siponing              off"</SPAN> <ICLASS="EMPHASIS">to a file </I>the output of a command 	      or commands within a pipe, but without affecting the result. This is	      useful for printing an ongoing process to a file or paper, perhaps to	      keep track of it for debugging purposes.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN">                              (redirection)                             |----&#62; to file                             |   ==========================|====================   command ---&#62; command ---&#62; |tee ---&#62; command ---&#62; ---&#62; output of pipe   =============================================== 	      </PRE></TD></TR></TABLE><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat listfile* | sort | tee check.file | uniq &#62; result.file</PRE></TD></TR></TABLE>	      (The file <TTCLASS="FILENAME">check.file</TT> contains	      the concatenated sorted <SPANCLASS="QUOTE">"listfiles"</SPAN>,	      before the duplicate lines are removed by <AHREF="textproc.html#UNIQREF">uniq</A>.)</P></DD><DT><BCLASS="COMMAND">mkfifo</B></DT><DD><P><ANAME="NAMEDPIPEREF"></A>This obscure command	      creates a <ICLASS="EMPHASIS">named pipe</I>, a temporary	      <ICLASS="EMPHASIS">first-in-first-out buffer</I> for	      transferring data between processes.		<ANAME="AEN10515"HREF="#FTN.AEN10515">[3]</A>	      Typically, one process writes to the FIFO, and the other	      reads from it. See <AHREF="contributed-scripts.html#FIFO">Example A-15</A>.</P></DD><DT><BCLASS="COMMAND">pathchk</B></DT><DD><P>This command checks the validity of a filename. If the	      filename exceeds the maximum allowable length (255	      characters) or one or more of the directories in	      its path is not searchable, then an error message	      results.</P><P>Unfortunately, <BCLASS="COMMAND">pathchk</B> does	      not return a recognizable error code, and it is therefore	      pretty much useless in a script. Consider instead the	      <AHREF="fto.html#RTIF">file test operators</A>.</P></DD><DT><ANAME="DDREF"></A><BCLASS="COMMAND">dd</B></DT><DD><P>This is the somewhat obscure and much feared <SPANCLASS="QUOTE">"data	      duplicator"</SPAN> command.  Originally a utility	      for exchanging data on magnetic tapes between UNIX	      minicomputers and IBM mainframes, this command still	      has its uses.  The <BCLASS="COMMAND">dd</B> command simply	      copies a file (or <TTCLASS="FILENAME">stdin/stdout</TT>), but	      with conversions. Possible conversions are ASCII/EBCDIC,	        		<ANAME="AEN10548"HREF="#FTN.AEN10548">[4]</A>	      upper/lower case, swapping of byte pairs between input	      and output, and skipping and/or truncating the head or	      tail of the input file. A <TTCLASS="USERINPUT"><B>dd --help</B></TT>	      lists the conversion and other options that this powerful	      utility takes.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Converting a file to all uppercase:   2&nbsp;   3&nbsp;dd if=$filename conv=ucase &#62; $filename.uppercase   4&nbsp;#                    lcase   # For lower case conversion</PRE></TD></TR></TABLE>            </P><DIVCLASS="EXAMPLE"><HR><ANAME="SELFCOPY"></A><P><B>Example 12-52. A script that copies itself</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# self-copy.sh   3&nbsp;   4&nbsp;# This script copies itself.   5&nbsp;   6&nbsp;file_subscript=copy   7&nbsp;   8&nbsp;dd if=$0 of=$0.$file_subscript 2&#62;/dev/null   9&nbsp;# Suppress messages from dd:   ^^^^^^^^^^^  10&nbsp;  11&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EXERCISINGDD"></A><P><B>Example 12-53. Exercising <BCLASS="COMMAND">dd</B></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# exercising-dd.sh   3&nbsp;   4&nbsp;# Script by Stephane Chazelas.   5&nbsp;# Somewhat modified by document author.   6&nbsp;   7&nbsp;input_file=$0   # This script.   8&nbsp;output_file=log.txt   9&nbsp;n=3  10&nbsp;p=5  11&nbsp;  12&nbsp;dd if=$input_file of=$output_file bs=1 skip=$((n-1)) count=$((p-n+1)) 2&#62; /dev/null  13&nbsp;# Extracts characters n to p from this script.  14&nbsp;  15&nbsp;# -------------------------------------------------------  16&nbsp;  17&nbsp;echo -n "hello world" | dd cbs=1 conv=unblock 2&#62; /dev/null  18&nbsp;# Echoes "hello world" vertically.  19&nbsp;  20&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>To demonstrate just how versatile <BCLASS="COMMAND">dd</B> is,	     let's use it to capture keystrokes.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DDKEYPRESS"></A><P><B>Example 12-54. Capturing Keystrokes</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# dd-keypress.sh: Capture keystrokes without needing to press ENTER.   3&nbsp;   4&nbsp;   5&nbsp;keypresses=4                      # Number of keypresses to capture.   6&nbsp;   7&nbsp;   8&nbsp;old_tty_setting=$(stty -g)        # Save old terminal settings.   9&nbsp;  10&nbsp;echo "Press $keypresses keys."  11&nbsp;stty -icanon -echo                # Disable canonical mode.  12&nbsp;                                  # Disable local echo.  13&nbsp;keys=$(dd bs=1 count=$keypresses 2&#62; /dev/null)  14&nbsp;# 'dd' uses stdin, if "if" (input file) not specified.  15&nbsp;  16&nbsp;stty "$old_tty_setting"           # Restore old terminal settings.  17&nbsp;  18&nbsp;echo "You pressed the \"$keys\" keys."  19&nbsp;  20&nbsp;# Thanks, Stephane Chazelas, for showing the way.  21&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <BCLASS="COMMAND">dd</B> command can do random access on a	      data stream.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo -n . | dd bs=1 seek=4 of=file conv=notrunc   2&nbsp;# The "conv=notrunc" option means that the output file will not be truncated.		   3&nbsp;   4&nbsp;# Thanks, S.C.</PRE></TD></TR></TABLE>            </P><P>The <BCLASS="COMMAND">dd</B> command can copy raw data	      and disk images to and from devices, such as floppies and	      tape drives (<AHREF="contributed-scripts.html#COPYCD">Example A-5</A>). A common use is	      creating boot floppies.</P><P>		<TTCLASS="USERINPUT"><B>dd if=kernel-image of=/dev/fd0H1440</B></TT>             </P><P>Similarly, <BCLASS="COMMAND">dd</B> can copy the entire	       contents of a floppy, even one formatted with a	       <SPANCLASS="QUOTE">"foreign"</SPAN> OS, to the hard drive as an	       image file.</P><P>		<TTCLASS="USERINPUT"><B>dd if=/dev/fd0 of=/home/bozo/projects/floppy.img</B></TT>             </P><P>	      Other applications of <BCLASS="COMMAND">dd</B> include	      initializing temporary swap files (<AHREF="zeros.html#EX73">Example 28-2</A>)	      and ramdisks (<AHREF="zeros.html#RAMDISK">Example 28-3</A>). It can even do a	      low-level copy of an entire hard drive partition, although	      this is not necessarily recommended.</P><P>People (with presumably nothing better to do with	      their time) are constantly thinking of interesting	      applications of <BCLASS="COMMAND">dd</B>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="BLOTOUT"></A><P><B>Example 12-55. Securely deleting a file</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# blot-out.sh: Erase "all" traces of a file.   3&nbsp;   4&nbsp;#  This script overwrites a target file alternately   5&nbsp;#+ with random bytes, then zeros before finally deleting it.   6&nbsp;#  After that, even examining the raw disk sectors by conventional methods   7&nbsp;#+ will not reveal the original file data.   8&nbsp;   9&nbsp;PASSES=7         #  Number of file-shredding passes.  10&nbsp;                 #  Increasing this slows script execution,  11&nbsp;                 #+ especially on large target files.  12&nbsp;BLOCKSIZE=1      #  I/O with /dev/urandom requires unit block size,  13&nbsp;                 #+ otherwise you get weird results.  14&nbsp;E_BADARGS=70     #  Various error exit codes.  15&nbsp;E_NOT_FOUND=71  16&nbsp;E_CHANGED_MIND=72  17&nbsp;  18&nbsp;if [ -z "$1" ]   # No filename specified.  19&nbsp;then  20&nbsp;  echo "Usage: `basename $0` filename"  21&nbsp;  exit $E_BADARGS  22&nbsp;fi  23&nbsp;  24&nbsp;file=$1  25&nbsp;  26&nbsp;if [ ! -e "$file" ]  27&nbsp;then  28&nbsp;  echo "File \"$file\" not found."  29&nbsp;  exit $E_NOT_FOUND  30&nbsp;fi    31&nbsp;  32&nbsp;echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? "  33&nbsp;read answer  34&nbsp;case "$answer" in  35&nbsp;[nN]) echo "Changed your mind, huh?"  36&nbsp;      exit $E_CHANGED_MIND  37&nbsp;      ;;  38&nbsp;*)    echo "Blotting out file \"$file\".";;  39&nbsp;esac  40&nbsp;  41&nbsp;  42&nbsp;flength=$(ls -l "$file" | awk '{print $5}')  # Field 5 is file length.  43&nbsp;pass_count=1  44&nbsp;  45&nbsp;chmod u+w "$file"   # Allow overwriting/deleting the file.  46&nbsp;  47&nbsp;echo  48&nbsp;  49&nbsp;while [ "$pass_count" -le "$PASSES" ]  50&nbsp;do  51&nbsp;  echo "Pass #$pass_count"  52&nbsp;  sync         # Flush buffers.  53&nbsp;  dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength  54&nbsp;               # Fill with random bytes.  55&nbsp;  sync         # Flush buffers again.  56&nbsp;  dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength  57&nbsp;               # Fill with zeros.  58&nbsp;  sync         # Flush buffers yet again.  59&nbsp;  let "pass_count += 1"  60&nbsp;  echo  61&nbsp;done    62&nbsp;  63&nbsp;  64&nbsp;rm -f $file    # Finally, delete scrambled and shredded file.  65&nbsp;sync           # Flush buffers a final time.  66&nbsp;  67&nbsp;echo "File \"$file\" blotted out and deleted."; echo  68&nbsp;  69&nbsp;  70&nbsp;exit 0  71&nbsp;  72&nbsp;#  This is a fairly secure, if inefficient and slow method  73&nbsp;#+ of thoroughly "shredding" a file.  74&nbsp;#  The "shred" command, part of the GNU "fileutils" package,  75&nbsp;#+ does the same thing, although more efficiently.  76&nbsp;  77&nbsp;#  The file cannot not be "undeleted" or retrieved by normal methods.  78&nbsp;#  However . . .  79&nbsp;#+ this simple method would *not* likely withstand  80&nbsp;#+ sophisticated forensic analysis.  81&nbsp;  82&nbsp;#  This script may not play well with a journaled file system.  83&nbsp;#  Exercise (difficult): Fix it so it does.  84&nbsp;  85&nbsp;  86&nbsp;  87&nbsp;#  Tom Vier's "wipe" file-deletion package does a much more thorough job  88&nbsp;#+ of file shredding than this simple script.  89&nbsp;#     http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2  90&nbsp;  91&nbsp;#  For an in-depth analysis on the topic of file deletion and security,  92&nbsp;#+ see Peter Gutmann's paper,  93&nbsp;#+     "Secure Deletion of Data From Magnetic and Solid-State Memory".  94&nbsp;#       http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="ODREF"></A><BCLASS="COMMAND">od</B></DT><DD><P>The <BCLASS="COMMAND">od</B>, or <ICLASS="EMPHASIS">octal	      dump</I> filter converts input (or files) to octal	      (base-8) or other bases. This is useful for viewing or	      processing binary data files or otherwise unreadable system	      device files, such as <TTCLASS="FILENAME">/dev/urandom</TT>,	      and as a filter for binary data. See <AHREF="randomvar.html#SEEDINGRANDOM">Example 9-28</A> and <AHREF="textproc.html#RND">Example 12-13</A>.</P></DD><DT><BCLASS="COMMAND">hexdump</B></DT><DD><P>Performs a hexadecimal, octal, decimal, or ASCII	      dump of a binary file. This command is the rough equivalent	      of <BCLASS="COMMAND">od</B>, above, but not nearly as	      useful.</P></DD><DT><BCLASS="COMMAND">objdump</B></DT><DD><P>Displays information about an object file or binary	      executable in either hexadecimal form or as a disassembled	      listing (with the <TTCLASS="OPTION">-d</TT> option).</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>objdump -d /bin/ls</B></TT> <TTCLASS="COMPUTEROUTPUT">/bin/ls:     file format elf32-i386 Disassembly of section .init: 080490bc &#60;.init&#62;:  80490bc:       55                      push   %ebp

⌨️ 快捷键说明

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