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

📄 filearchiv.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 4 页
字号:
CLASS="COMMAND">compress</B>.</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">znew</B> command transforms	      <ICLASS="EMPHASIS">compressed</I> files into	      <ICLASS="EMPHASIS">gzipped</I> ones.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">sq</B></DT><DD><P>Yet another compression utility, a filter that works	      only on sorted ASCII word lists. It uses the standard	      invocation syntax for a filter, <BCLASS="COMMAND">sq &#60; input-file &#62;	      output-file</B>.  Fast, but not nearly as efficient	      as <AHREF="filearchiv.html#GZIPREF">gzip</A>.  The corresponding	      uncompression filter is <BCLASS="COMMAND">unsq</B>, invoked	      like <BCLASS="COMMAND">sq</B>.</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The output of <BCLASS="COMMAND">sq</B> may be	        piped to <BCLASS="COMMAND">gzip</B> for further		compression.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">zip</B>, <BCLASS="COMMAND">unzip</B></DT><DD><P>Cross-platform file archiving and compression utility	      compatible with DOS <ICLASS="EMPHASIS">pkzip.exe</I>.	      <SPANCLASS="QUOTE">"Zipped"</SPAN> archives seem to be a more	      acceptable medium of exchange on the Internet than	      <SPANCLASS="QUOTE">"tarballs"</SPAN>.</P></DD><DT><BCLASS="COMMAND">unarc</B>, <BCLASS="COMMAND">unarj</B>, <BCLASS="COMMAND">unrar</B></DT><DD><P>These Linux utilities permit unpacking archives	      compressed with the DOS <ICLASS="EMPHASIS">arc.exe</I>,	      <ICLASS="EMPHASIS">arj.exe</I>, and	      <ICLASS="EMPHASIS">rar.exe</I> programs.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="FAINFORMATION1"></A>File Information</B></P><DL><DT><ANAME="FILEREF"></A><BCLASS="COMMAND">file</B></DT><DD><P>A utility for identifying file types. The command	      <TTCLASS="USERINPUT"><B>file file-name</B></TT> will return a	      file specification for <TTCLASS="FILENAME">file-name</TT>,	      such as <TTCLASS="COMPUTEROUTPUT">ascii text</TT> or	      <TTCLASS="COMPUTEROUTPUT">data</TT>. It references	      the <AHREF="sha-bang.html#MAGNUMREF">magic numbers</A>	      found in <TTCLASS="FILENAME">/usr/share/magic</TT>,	      <TTCLASS="FILENAME">/etc/magic</TT>, or	      <TTCLASS="FILENAME">/usr/lib/magic</TT>, depending on the	      Linux/UNIX distribution.</P><P>The <TTCLASS="OPTION">-f</TT> option causes	      <BCLASS="COMMAND">file</B> to run in batch mode, to read from	      a designated file a list of filenames to analyze. The	      <TTCLASS="OPTION">-z</TT> option, when used on a compressed	      target file, forces an attempt to analyze the uncompressed	      file type.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>file test.tar.gz</B></TT> <TTCLASS="COMPUTEROUTPUT">test.tar.gz: gzip compressed data, deflated, last modified: Sun Sep 16 13:34:51 2001, os: Unix</TT>  <TTCLASS="PROMPT">bash </TT><TTCLASS="USERINPUT"><B>file -z test.tar.gz</B></TT> <TTCLASS="COMPUTEROUTPUT">test.tar.gz: GNU tar archive (gzip compressed data, deflated, last modified: Sun Sep 16 13:34:51 2001, os: Unix)</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Find sh and Bash scripts in a given directory:   2&nbsp;   3&nbsp;DIRECTORY=/usr/local/bin   4&nbsp;KEYWORD=Bourne   5&nbsp;# Bourne and Bourne-Again shell scripts   6&nbsp;   7&nbsp;file $DIRECTORY/* | fgrep $KEYWORD   8&nbsp;   9&nbsp;# Output:  10&nbsp;  11&nbsp;# /usr/local/bin/burn-cd:          Bourne-Again shell script text executable  12&nbsp;# /usr/local/bin/burnit:           Bourne-Again shell script text executable  13&nbsp;# /usr/local/bin/cassette.sh:      Bourne shell script text executable  14&nbsp;# /usr/local/bin/copy-cd:          Bourne-Again shell script text executable  15&nbsp;# . . .</PRE></TD></TR></TABLE>	    </P><DIVCLASS="EXAMPLE"><HR><ANAME="STRIPC"></A><P><B>Example 12-29. Stripping comments from C program files</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# strip-comment.sh: Strips out the comments (/* COMMENT */) in a C program.   3&nbsp;   4&nbsp;E_NOARGS=0   5&nbsp;E_ARGERROR=66   6&nbsp;E_WRONG_FILE_TYPE=67   7&nbsp;   8&nbsp;if [ $# -eq "$E_NOARGS" ]   9&nbsp;then  10&nbsp;  echo "Usage: `basename $0` C-program-file" &#62;&#38;2 # Error message to stderr.  11&nbsp;  exit $E_ARGERROR  12&nbsp;fi    13&nbsp;  14&nbsp;# Test for correct file type.  15&nbsp;type=`file $1 | awk '{ print $2, $3, $4, $5 }'`  16&nbsp;# "file $1" echoes file type . . .  17&nbsp;# Then awk removes the first field of this, the filename . . .  18&nbsp;# Then the result is fed into the variable "type".  19&nbsp;correct_type="ASCII C program text"  20&nbsp;  21&nbsp;if [ "$type" != "$correct_type" ]  22&nbsp;then  23&nbsp;  echo  24&nbsp;  echo "This script works on C program files only."  25&nbsp;  echo  26&nbsp;  exit $E_WRONG_FILE_TYPE  27&nbsp;fi    28&nbsp;  29&nbsp;  30&nbsp;# Rather cryptic sed script:  31&nbsp;#--------  32&nbsp;sed '  33&nbsp;/^\/\*/d  34&nbsp;/.*\*\//d  35&nbsp;' $1  36&nbsp;#--------  37&nbsp;# Easy to understand if you take several hours to learn sed fundamentals.  38&nbsp;  39&nbsp;  40&nbsp;#  Need to add one more line to the sed script to deal with  41&nbsp;#+ case where line of code has a comment following it on same line.  42&nbsp;#  This is left as a non-trivial exercise.  43&nbsp;  44&nbsp;#  Also, the above code deletes non-comment lines with a "*/" --  45&nbsp;#+ not a desirable result.  46&nbsp;  47&nbsp;exit 0  48&nbsp;  49&nbsp;  50&nbsp;# ----------------------------------------------------------------  51&nbsp;# Code below this line will not execute because of 'exit 0' above.  52&nbsp;  53&nbsp;# Stephane Chazelas suggests the following alternative:  54&nbsp;  55&nbsp;usage() {  56&nbsp;  echo "Usage: `basename $0` C-program-file" &#62;&#38;2  57&nbsp;  exit 1  58&nbsp;}  59&nbsp;  60&nbsp;WEIRD=`echo -n -e '\377'`   # or WEIRD=$'\377'  61&nbsp;[[ $# -eq 1 ]] || usage  62&nbsp;case `file "$1"` in  63&nbsp;  *"C program text"*) sed -e "s%/\*%${WEIRD}%g;s%\*/%${WEIRD}%g" "$1" \  64&nbsp;     | tr '\377\n' '\n\377' \  65&nbsp;     | sed -ne 'p;n' \  66&nbsp;     | tr -d '\n' | tr '\377' '\n';;  67&nbsp;  *) usage;;  68&nbsp;esac  69&nbsp;  70&nbsp;#  This is still fooled by things like:  71&nbsp;#  printf("/*");  72&nbsp;#  or  73&nbsp;#  /*  /* buggy embedded comment */  74&nbsp;#  75&nbsp;#  To handle all special cases (comments in strings, comments in string  76&nbsp;#+ where there is a \", \\" ...) the only way is to write a C parser  77&nbsp;#+ (using lex or yacc perhaps?).  78&nbsp;  79&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="WHICHREF"></A><BCLASS="COMMAND">which</B></DT><DD><P><BCLASS="COMMAND">which command-xxx</B> gives the full path	      to <SPANCLASS="QUOTE">"command-xxx"</SPAN>. This is useful for finding	      out whether a particular command or utility is installed	      on the system.</P><P><TTCLASS="USERINPUT"><B>$bash which rm</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">/usr/bin/rm</TT></PRE></TD></TR></TABLE>	  </P></DD><DT><BCLASS="COMMAND">whereis</B></DT><DD><P>Similar to <BCLASS="COMMAND">which</B>, above,	      <BCLASS="COMMAND">whereis command-xxx</B> gives the	      full path to <SPANCLASS="QUOTE">"command-xxx"</SPAN>, but also to its	      <ICLASS="EMPHASIS">manpage</I>.</P><P><TTCLASS="USERINPUT"><B>$bash whereis rm</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">rm: /bin/rm /usr/share/man/man1/rm.1.bz2</TT></PRE></TD></TR></TABLE>	  </P></DD><DT><ANAME="WHATISREF"></A><BCLASS="COMMAND">whatis</B></DT><DD><P><BCLASS="COMMAND">whatis filexxx</B> looks up	      <SPANCLASS="QUOTE">"filexxx"</SPAN> in the	      <TTCLASS="REPLACEABLE"><I>whatis</I></TT> database. This is useful	      for identifying system commands and important configuration	      files. Consider it a simplified <BCLASS="COMMAND">man</B>	      command.</P><P><TTCLASS="USERINPUT"><B>$bash whatis whatis</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">whatis               (1)  - search the whatis database for complete words</TT></PRE></TD></TR></TABLE>	  </P><DIVCLASS="EXAMPLE"><HR><ANAME="WHAT"></A><P><B>Example 12-30. <BCLASS="COMMAND">Exploring <TTCLASS="FILENAME">/usr/X11R6/bin</TT></B></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# What are all those mysterious binaries in /usr/X11R6/bin?   4&nbsp;   5&nbsp;DIRECTORY="/usr/X11R6/bin"   6&nbsp;# Try also "/bin", "/usr/bin", "/usr/local/bin", etc.   7&nbsp;   8&nbsp;for file in $DIRECTORY/*   9&nbsp;do  10&nbsp;  whatis `basename $file`   # Echoes info about the binary.  11&nbsp;done  12&nbsp;  13&nbsp;exit 0  14&nbsp;  15&nbsp;# You may wish to redirect output of this script, like so:  16&nbsp;# ./what.sh &#62;&#62;whatis.db  17&nbsp;# or view it a page at a time on stdout,  18&nbsp;# ./what.sh | less</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="loops.html#FILEINFO">Example 10-3</A>.</P></DD><DT><BCLASS="COMMAND">vdir</B></DT><DD><P>Show a detailed directory listing. The effect is similar to	      <AHREF="external.html#LSREF">ls -l</A>.</P><P>This is one of the GNU <ICLASS="EMPHASIS">fileutils</I>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>vdir</B></TT> <TTCLASS="COMPUTEROUTPUT">total 10 -rw-r--r--    1 bozo  bozo      4034 Jul 18 22:04 data1.xrolo -rw-r--r--    1 bozo  bozo      4602 May 25 13:58 data1.xrolo.bak -rw-r--r--    1 bozo  bozo       877 Dec 17  2000 employment.xrolo</TT>  <TTCLASS="PROMPT">bash </TT><TTCLASS="USERINPUT"><B>ls -l</B></TT> <TTCLASS="COMPUTEROUTPUT">total 10 -rw-r--r--    1 bozo  bozo      4034 Jul 18 22:04 data1.xrolo -rw-r--r--    1 bozo  bozo      4602 May 25 13:58 data1.xrolo.bak -rw-r--r--    1 bozo  bozo       877 Dec 17  2000 employment.xrolo</TT> 	      </PRE></TD></TR></TABLE>	      </P></DD><DT><BCLASS="COMMAND">locate</B>, <BCLASS="COMMAND">slocate</B></DT><DD><P>The <BCLASS="COMMAND">locate</B> command searches for files using a	      database stored for just that purpose. The	      <BCLASS="COMMAND">slocate</B> command is the secure version of	      <BCLASS="COMMAND">locate</B> (which may be aliased to	      <BCLASS="COMMAND">slocate</B>).</P><P><TTCLASS="USERINPUT"><B>$bash locate hickson</B></TT><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">/usr/lib/xephem/catalogs/hickson.edb</TT></PRE></TD></TR></TABLE></P></DD><DT><BCLASS="COMMAND">readlink</B></DT><DD><P>Disclose the file that a symbolic link points to.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>readlink /usr/bin/awk</B></TT> <TTCLASS="COMPUTEROUTPUT">../../bin/gawk</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><BCLASS="COMMAND">strings</B></DT><DD><P>Use the <BCLASS="COMMAND">strings</B> command to find	      printable strings in a binary or data file. It will list	      sequences of printable characters found in the target	      file. This might be handy for a quick 'n dirty examination	      of a core dump or for looking at an unknown graphic image	      file (<TTCLASS="USERINPUT"><B>strings image-file | more</B></TT> might	      show something like <TTCLASS="COMPUTEROUTPUT">JFIF</TT>,	      which would identify the file as a <ICLASS="EMPHASIS">jpeg</I>	      graphic). In a script, you would probably	      parse the output of <BCLASS="COMMAND">strings</B>	      with <AHREF="textproc.html#GREPREF">grep</A> or <AHREF="sedawk.html#SEDREF">sed</A>. See <AHREF="loops.html#BINGREP">Example 10-7</A>	      and <AHREF="loops.html#FINDSTRING">Example 10-9</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="WSTRINGS"></A><P><B>Example 12-31. An <SPANCLASS="QUOTE">"improved"</SPAN> <ICLASS="EMPHASIS">strings</I>	        command</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# wstrings.sh: "word-strings" (enhanced "strings" command)   3&nbsp;#   4&nbsp;#  This script filters the output of "strings" by checking it   5&nbsp;#+ against a standard word list file.   6&nbsp;#  This effectively eliminates gibberish and noise,   7&nbsp;#+ and outputs only recognized words.   8&nbsp;   9&nbsp;# ===========================================================  10&nbsp;#                 Standard Check for Script Argument(s)  11&nbsp;ARGS=1  12&nbsp;E_BADARGS=65  13&nbsp;E_NOFILE=66  14&nbsp;  15&nbsp;if [ $# -ne $ARGS ]  16&nbsp;then  17&nbsp;  echo "Usage: `basename $0` filename"  18&nbsp;  exit $E_BADARGS  19&nbsp;fi  20&nbsp;  21&nbsp;if [ ! -f "$1" ]                      # Check if file exists.  22&nbsp;then  23&nbsp;    echo "File \"$1\" does not exist."  24&nbsp;    exit $E_NOFILE  25&nbsp;fi  26&nbsp;# ===========================================================  27&nbsp;  28&nbsp;  29&nbsp;MINSTRLEN=3                           #  Minimum string length.  30&nbsp;WORDFILE=/usr/share/dict/linux.words  #  Dictionary file.  31&nbsp;                                      #  May specify a different  32&nbsp;                                      #+ word list file  33&nbsp;                                      #+ of one-word-per-line format.  34&nbsp;  35&nbsp;  36&nbsp;wlist=`strings "$1" | tr A-Z a-z | tr '[:space:]' Z | \  37&nbsp;tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`  38&nbsp;  39&nbsp;# Translate output of 'strings' command with multiple passes of 'tr'.  40&nbsp;#  "tr A-Z a-z"  converts to lowercase.  41&nbsp;#  "tr '[:space:]'"  converts whitespace characters to Z's.  42&nbsp;#  "tr -cs '[:alpha:]' Z"  converts non-alphabetic characters to Z's,  43&nbsp;#+ and squeezes multiple consecutive Z's.  44&nbsp;#  "tr -s '\173-\377' Z"  converts all characters past 'z' to Z's  45&nbsp;#+ and squeezes multiple consecutive Z's,  46&nbsp;#+ which gets rid of all the weird characters that the previous  47&nbsp;#+ translation failed to deal with.  48&nbsp;#  Finally, "tr Z ' '" converts all those Z's to whitespace,  49&nbsp;#+ which will be seen as word separators in the loop below.  50&nbsp;  51&nbsp;#  ****************************************************************  52&nbsp;#  Note the technique of feeding the output of 'tr' back to itself,  53&nbsp;#+ but with different arguments and/or options on each pass.  54&nbsp;#  ****************************************************************  55&nbsp;  56&nbsp;  57&nbsp;for word in $wlist                    # Important:  58&nbsp;                                      # $wlist must not be quoted here.  59&nbsp;                                      # "$wlist" does not work.  60&nbsp;                                      # Why not?  61&nbsp;do  62&nbsp;  63&nbsp;  strlen=${#word}                     # String length.  64&nbsp;  if [ "$strlen" -lt "$MINSTRLEN" ]   # Skip over short strings.  65&nbsp;  then  66&nbsp;    continue  67&nbsp;  fi  68&nbsp;  69&nbsp;  grep -Fw $word "$WORDFILE"          #  Match whole words only.  70&nbsp;#      ^^^                            #  "Fixed strings" and  71&nbsp;                                      #+ "whole words" options.   72&nbsp;  73&nbsp;done    74&nbsp;  75&nbsp;  76&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMPARISONN1"></A>Comparison</B></P

⌨️ 快捷键说明

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