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

📄 filearchiv.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
></TT> <TTCLASS="COMPUTEROUTPUT">==== 1:1c   This is line 1 of "file-1". 2:1c   This is line 1 of "file-2". 3:1c   This is line 1 of "file-3"</TT> 	      </PRE></TD></TR></TABLE>	      </P></DD><DT><ANAME="SDIFFREF"></A><BCLASS="COMMAND">sdiff</B></DT><DD><P>Compare and/or edit two files in order to merge	      them into an output file. Because of its interactive nature,	      this command would find little use in a script.</P></DD><DT><ANAME="CMPREF"></A><BCLASS="COMMAND">cmp</B></DT><DD><P>The <BCLASS="COMMAND">cmp</B> command is a simpler version of	      <BCLASS="COMMAND">diff</B>, above. Whereas <BCLASS="COMMAND">diff</B>	      reports the differences between two files,	      <BCLASS="COMMAND">cmp</B> merely shows at what point they	      differ.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Like <BCLASS="COMMAND">diff</B>, <BCLASS="COMMAND">cmp</B>	    returns an exit status of 0 if the compared files are	    identical, and 1 if they differ. This permits use in a test	    construct within a shell script.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="FILECOMP"></A><P><B>Example 15-34. Using <ICLASS="FIRSTTERM">cmp</I> to compare two files	        within a script.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;ARGS=2  # Two args to script expected.   4&nbsp;E_BADARGS=65   5&nbsp;E_UNREADABLE=66   6&nbsp;   7&nbsp;if [ $# -ne "$ARGS" ]   8&nbsp;then   9&nbsp;  echo "Usage: `basename $0` file1 file2"  10&nbsp;  exit $E_BADARGS  11&nbsp;fi  12&nbsp;  13&nbsp;if [[ ! -r "$1" || ! -r "$2" ]]  14&nbsp;then  15&nbsp;  echo "Both files to be compared must exist and be readable."  16&nbsp;  exit $E_UNREADABLE  17&nbsp;fi  18&nbsp;  19&nbsp;cmp $1 $2 &#38;&#62; /dev/null  # /dev/null buries the output of the "cmp" command.  20&nbsp;#   cmp -s $1 $2  has same result ("-s" silent flag to "cmp")  21&nbsp;#   Thank you  Anders Gustavsson for pointing this out.  22&nbsp;#  23&nbsp;# Also works with 'diff', i.e.,   diff $1 $2 &#38;&#62; /dev/null  24&nbsp;  25&nbsp;if [ $? -eq 0 ]         # Test exit status of "cmp" command.  26&nbsp;then  27&nbsp;  echo "File \"$1\" is identical to file \"$2\"."  28&nbsp;else    29&nbsp;  echo "File \"$1\" differs from file \"$2\"."  30&nbsp;fi  31&nbsp;  32&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><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>Use <BCLASS="COMMAND">zcmp</B> on	      <ICLASS="FIRSTTERM">gzipped</I> files.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="COMMREF"></A><BCLASS="COMMAND">comm</B></DT><DD><P>Versatile file comparison utility. The files must be	      sorted for this to be useful.</P><P><BCLASS="COMMAND">comm		<TTCLASS="REPLACEABLE"><I>-options</I></TT>		<TTCLASS="REPLACEABLE"><I>first-file</I></TT>		<TTCLASS="REPLACEABLE"><I>second-file</I></TT></B></P><P><TTCLASS="USERINPUT"><B>comm file-1 file-2</B></TT> outputs three columns:	      <UL><LI><P>column 1 = lines unique to <TTCLASS="FILENAME">file-1</TT></P></LI><LI><P>column 2 = lines unique to <TTCLASS="FILENAME">file-2</TT></P></LI><LI><P>column 3 = lines common to both.</P></LI></UL></P><P>The options allow suppressing output of one or more columns.	      <UL><LI><P><TTCLASS="OPTION">-1</TT> suppresses column		    <TTCLASS="LITERAL">1</TT></P></LI><LI><P><TTCLASS="OPTION">-2</TT> suppresses column		    <TTCLASS="LITERAL">2</TT></P></LI><LI><P><TTCLASS="OPTION">-3</TT> suppresses column		    <TTCLASS="LITERAL">3</TT></P></LI><LI><P><TTCLASS="OPTION">-12</TT> suppresses both columns		    <TTCLASS="LITERAL">1</TT> and <TTCLASS="LITERAL">2</TT>, etc.</P></LI></UL>	    </P><P>This command is useful for comparing	      <SPANCLASS="QUOTE">"dictionaries"</SPAN> or <ICLASS="FIRSTTERM">word	      lists</I> -- sorted text files with one word per	      line.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="FAUTILS1"></A>Utilities</B></P><DL><DT><ANAME="BASENAMEREF"></A><BCLASS="COMMAND">basename</B></DT><DD><P>Strips the path information from a file name, printing	      only the file name. The construction  <TTCLASS="USERINPUT"><B>basename		$0</B></TT> lets the script know its name, that is, the name it	      was invoked by. This can be used for <SPANCLASS="QUOTE">"usage"</SPAN> messages if, 	      for example a script is called with missing arguments:              <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "Usage: `basename $0` arg1 arg2 ... argn"</PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="DIRNAMEREF"></A><BCLASS="COMMAND">dirname</B></DT><DD><P>Strips the <BCLASS="COMMAND">basename</B> from	    a filename, printing only the path information.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><BCLASS="COMMAND">basename</B> and <BCLASS="COMMAND">dirname</B>		can operate on any arbitrary string. The argument		does not need to refer to an existing file, or		even be a filename for that matter (see <AHREF="contributed-scripts.html#DAYSBETWEEN">Example A-7</A>).</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX35"></A><P><B>Example 15-35. <ICLASS="FIRSTTERM">basename</I> and	      <ICLASS="FIRSTTERM">dirname</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;a=/home/bozo/daily-journal.txt   4&nbsp;   5&nbsp;echo "Basename of /home/bozo/daily-journal.txt = `basename $a`"   6&nbsp;echo "Dirname of /home/bozo/daily-journal.txt = `dirname $a`"   7&nbsp;echo   8&nbsp;echo "My own home is `basename ~/`."         # `basename ~` also works.   9&nbsp;echo "The home of my home is `dirname ~/`."  # `dirname ~`  also works.  10&nbsp;  11&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="SPLITREF"></A><BCLASS="COMMAND">split</B>, <ANAME="CSPLITREF"></A><BCLASS="COMMAND">csplit</B></DT><DD><P>These are utilities for splitting a file into smaller	      chunks. Their usual use is for splitting up large files	      in order to back them up on floppies or preparatory to	      e-mailing or uploading them.</P><P>The <BCLASS="COMMAND">csplit</B> command splits a file	      according to <ICLASS="FIRSTTERM">context</I>, the split occuring	      where patterns are matched.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SPLITCOPY"></A><P><B>Example 15-36. A script that copies itself in sections</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# splitcopy.sh   3&nbsp;   4&nbsp;#  A script that splits itself into chunks,   5&nbsp;#+ then reassembles the chunks into an exact copy   6&nbsp;#+ of the original script.   7&nbsp;   8&nbsp;CHUNKSIZE=4    #  Size of first chunk of split files.   9&nbsp;OUTPREFIX=xx   #  csplit prefixes, by default,  10&nbsp;               #+ files with "xx" ...  11&nbsp;  12&nbsp;csplit "$0" "$CHUNKSIZE"  13&nbsp;  14&nbsp;# Some comment lines for padding . . .  15&nbsp;# Line 15  16&nbsp;# Line 16  17&nbsp;# Line 17  18&nbsp;# Line 18  19&nbsp;# Line 19  20&nbsp;# Line 20  21&nbsp;  22&nbsp;cat "$OUTPREFIX"* &#62; "$0.copy"  # Concatenate the chunks.  23&nbsp;rm "$OUTPREFIX"*               # Get rid of the chunks.  24&nbsp;  25&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="FAENCENCR1"></A>Encoding and Encryption</B></P><DL><DT><ANAME="SUMREF"></A><BCLASS="COMMAND">sum</B>, <ANAME="CKSUMREF"></A><BCLASS="COMMAND">cksum</B>, <ANAME="MD5SUMREF"></A><BCLASS="COMMAND">md5sum</B>, <ANAME="SHA1SUMREF"></A><BCLASS="COMMAND">sha1sum</B></DT><DD><P>These are utilities for generating checksums. A	      <ICLASS="FIRSTTERM">checksum</I> is a number mathematically	      calculated from the contents of a file, for the purpose	      of checking its integrity. A script might refer to a list	      of checksums for security purposes, such as ensuring	      that the contents of key system files have not been	      altered or corrupted. For security applications, use the	      <BCLASS="COMMAND">md5sum</B> (<BCLASS="COMMAND">m</B>essage	      <BCLASS="COMMAND">d</B>igest <BCLASS="COMMAND">5</B>	      check<BCLASS="COMMAND">sum</B>) command, or better yet,	      the newer <BCLASS="COMMAND">sha1sum</B> (Secure Hash	      Algorithm).</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cksum /boot/vmlinuz</B></TT> <TTCLASS="COMPUTEROUTPUT">1670054224 804083 /boot/vmlinuz</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -n "Top Secret" | cksum</B></TT> <TTCLASS="COMPUTEROUTPUT">3391003827 10</TT>    <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>md5sum /boot/vmlinuz</B></TT> <TTCLASS="COMPUTEROUTPUT">0f43eccea8f09e0a0b2b5cf1dcf333ba  /boot/vmlinuz</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -n "Top Secret" | md5sum</B></TT> <TTCLASS="COMPUTEROUTPUT">8babc97a6f62a4649716f4df8d61728f  -</TT> 	      </PRE></TD></TR></TABLE>	  </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">cksum</B> command shows the size,	    in bytes, of its target, whether file or	    <TTCLASS="FILENAME">stdout</TT>.</P><P>The <BCLASS="COMMAND">md5sum</B> and	    <BCLASS="COMMAND">sha1sum</B> commands display a	    <AHREF="special-chars.html#DASHREF2">dash</A> when they receive their input from	    <TTCLASS="FILENAME">stdout</TT>.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="FILEINTEGRITY"></A><P><B>Example 15-37. Checking file integrity</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# file-integrity.sh: Checking whether files in a given directory   3&nbsp;#                    have been tampered with.   4&nbsp;   5&nbsp;E_DIR_NOMATCH=70   6&nbsp;E_BAD_DBFILE=71   7&nbsp;   8&nbsp;dbfile=File_record.md5   9&nbsp;# Filename for storing records (database file).  10&nbsp;  11&nbsp;  12&nbsp;set_up_database ()  13&nbsp;{  14&nbsp;  echo ""$directory"" &#62; "$dbfile"  15&nbsp;  # Write directory name to first line of file.  16&nbsp;  md5sum "$directory"/* &#62;&#62; "$dbfile"  17&nbsp;  # Append md5 checksums and filenames.  18&nbsp;}  19&nbsp;  20&nbsp;check_database ()  21&nbsp;{  22&nbsp;  local n=0  23&nbsp;  local filename  24&nbsp;  local checksum  25&nbsp;  26&nbsp;  # ------------------------------------------- #  27&nbsp;  #  This file check should be unnecessary,  28&nbsp;  #+ but better safe than sorry.  29&nbsp;  30&nbsp;  if [ ! -r "$dbfile" ]  31&nbsp;  then  32&nbsp;    echo "Unable to read checksum database file!"  33&nbsp;    exit $E_BAD_DBFILE  34&nbsp;  fi  35&nbsp;  # ------------------------------------------- #  36&nbsp;  37&nbsp;  while read record[n]  38&nbsp;  do  39&nbsp;  40&nbsp;    directory_checked="${record[0]}"  41&nbsp;    if [ "$directory_checked" != "$directory" ]  42&nbsp;    then  43&nbsp;      echo "Directories do not match up!"  44&nbsp;      # Tried to use file for a different directory.  45&nbsp;      exit $E_DIR_NOMATCH  46&nbsp;    fi  47&nbsp;  48&nbsp;    if [ "$n" -gt 0 ]   # Not directory name.

⌨️ 快捷键说明

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