📄 extmisc.html
字号:
>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 <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) |----> to file | ==========================|==================== command ---> command ---> |tee ---> command ---> ---> output of pipe =============================================== </PRE></TD></TR></TABLE><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 cat listfile* | sort | tee check.file | uniq > 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 # Converting a file to all uppercase: 2 3 dd if=$filename conv=ucase > $filename.uppercase 4 # 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 #!/bin/bash 2 # self-copy.sh 3 4 # This script copies itself. 5 6 file_subscript=copy 7 8 dd if=$0 of=$0.$file_subscript 2>/dev/null 9 # Suppress messages from dd: ^^^^^^^^^^^ 10 11 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 #!/bin/bash 2 # exercising-dd.sh 3 4 # Script by Stephane Chazelas. 5 # Somewhat modified by document author. 6 7 input_file=$0 # This script. 8 output_file=log.txt 9 n=3 10 p=5 11 12 dd if=$input_file of=$output_file bs=1 skip=$((n-1)) count=$((p-n+1)) 2> /dev/null 13 # Extracts characters n to p from this script. 14 15 # ------------------------------------------------------- 16 17 echo -n "hello world" | dd cbs=1 conv=unblock 2> /dev/null 18 # Echoes "hello world" vertically. 19 20 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 #!/bin/bash 2 # dd-keypress.sh: Capture keystrokes without needing to press ENTER. 3 4 5 keypresses=4 # Number of keypresses to capture. 6 7 8 old_tty_setting=$(stty -g) # Save old terminal settings. 9 10 echo "Press $keypresses keys." 11 stty -icanon -echo # Disable canonical mode. 12 # Disable local echo. 13 keys=$(dd bs=1 count=$keypresses 2> /dev/null) 14 # 'dd' uses stdin, if "if" (input file) not specified. 15 16 stty "$old_tty_setting" # Restore old terminal settings. 17 18 echo "You pressed the \"$keys\" keys." 19 20 # Thanks, Stephane Chazelas, for showing the way. 21 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 echo -n . | dd bs=1 seek=4 of=file conv=notrunc 2 # The "conv=notrunc" option means that the output file will not be truncated. 3 4 # 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 #!/bin/bash 2 # blot-out.sh: Erase "all" traces of a file. 3 4 # This script overwrites a target file alternately 5 #+ with random bytes, then zeros before finally deleting it. 6 # After that, even examining the raw disk sectors by conventional methods 7 #+ will not reveal the original file data. 8 9 PASSES=7 # Number of file-shredding passes. 10 # Increasing this slows script execution, 11 #+ especially on large target files. 12 BLOCKSIZE=1 # I/O with /dev/urandom requires unit block size, 13 #+ otherwise you get weird results. 14 E_BADARGS=70 # Various error exit codes. 15 E_NOT_FOUND=71 16 E_CHANGED_MIND=72 17 18 if [ -z "$1" ] # No filename specified. 19 then 20 echo "Usage: `basename $0` filename" 21 exit $E_BADARGS 22 fi 23 24 file=$1 25 26 if [ ! -e "$file" ] 27 then 28 echo "File \"$file\" not found." 29 exit $E_NOT_FOUND 30 fi 31 32 echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? " 33 read answer 34 case "$answer" in 35 [nN]) echo "Changed your mind, huh?" 36 exit $E_CHANGED_MIND 37 ;; 38 *) echo "Blotting out file \"$file\".";; 39 esac 40 41 42 flength=$(ls -l "$file" | awk '{print $5}') # Field 5 is file length. 43 pass_count=1 44 45 chmod u+w "$file" # Allow overwriting/deleting the file. 46 47 echo 48 49 while [ "$pass_count" -le "$PASSES" ] 50 do 51 echo "Pass #$pass_count" 52 sync # Flush buffers. 53 dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength 54 # Fill with random bytes. 55 sync # Flush buffers again. 56 dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength 57 # Fill with zeros. 58 sync # Flush buffers yet again. 59 let "pass_count += 1" 60 echo 61 done 62 63 64 rm -f $file # Finally, delete scrambled and shredded file. 65 sync # Flush buffers a final time. 66 67 echo "File \"$file\" blotted out and deleted."; echo 68 69 70 exit 0 71 72 # This is a fairly secure, if inefficient and slow method 73 #+ of thoroughly "shredding" a file. 74 # The "shred" command, part of the GNU "fileutils" package, 75 #+ does the same thing, although more efficiently. 76 77 # The file cannot not be "undeleted" or retrieved by normal methods. 78 # However . . . 79 #+ this simple method would *not* likely withstand 80 #+ sophisticated forensic analysis. 81 82 # This script may not play well with a journaled file system. 83 # Exercise (difficult): Fix it so it does. 84 85 86 87 # Tom Vier's "wipe" file-deletion package does a much more thorough job 88 #+ of file shredding than this simple script. 89 # http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2 90 91 # For an in-depth analysis on the topic of file deletion and security, 92 #+ see Peter Gutmann's paper, 93 #+ "Secure Deletion of Data From Magnetic and Solid-State Memory". 94 # 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 <.init>: 80490bc: 55 push %ebp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -