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

📄 extmisc.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  80490bd:       89 e5                   mov    %esp,%ebp  . . .</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><BCLASS="COMMAND">mcookie</B></DT><DD><P>This command generates a <SPANCLASS="QUOTE">"magic cookie"</SPAN>, a	    128-bit (32-character) pseudorandom hexadecimal number,	    normally used as an authorization <SPANCLASS="QUOTE">"signature"</SPAN>	    by the X server. This also available for use in a script as	    a <SPANCLASS="QUOTE">"quick 'n dirty"</SPAN> random number.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;random000=$(mcookie)</PRE></TD></TR></TABLE>	    </P><P>Of course, a script could use <AHREF="filearchiv.html#MD5SUMREF">md5</A> for the same purpose.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Generate md5 checksum on the script itself.   2&nbsp;random001=`md5sum $0 | awk '{print $1}'`   3&nbsp;# Uses 'awk' to strip off the filename.</PRE></TD></TR></TABLE>	    </P><P>The <BCLASS="COMMAND">mcookie</B> command gives yet another way	      to generate a <SPANCLASS="QUOTE">"unique"</SPAN> filename.</P><DIVCLASS="EXAMPLE"><HR><ANAME="TEMPFILENAME"></A><P><B>Example 12-56. Filename generator</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# tempfile-name.sh:  temp filename generator   3&nbsp;   4&nbsp;BASE_STR=`mcookie`   # 32-character magic cookie.   5&nbsp;POS=11               # Arbitrary position in magic cookie string.   6&nbsp;LEN=5                # Get $LEN consecutive characters.   7&nbsp;   8&nbsp;prefix=temp          #  This is, after all, a "temp" file.   9&nbsp;                     #  For more "uniqueness," generate the filename prefix  10&nbsp;                     #+ using the same method as the suffix, below.  11&nbsp;  12&nbsp;suffix=${BASE_STR:POS:LEN}  13&nbsp;                     # Extract a 5-character string, starting at position 11.  14&nbsp;  15&nbsp;temp_filename=$prefix.$suffix  16&nbsp;                     # Construct the filename.  17&nbsp;  18&nbsp;echo "Temp filename = "$temp_filename""  19&nbsp;  20&nbsp;# sh tempfile-name.sh  21&nbsp;# Temp filename = temp.e19ea  22&nbsp;  23&nbsp;#  Compare this method of generating "unique" filenames  24&nbsp;#+ with the 'date' method in ex51.sh.  25&nbsp;  26&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">units</B></DT><DD><P>This utility converts between different units of measure.	      While normally invoked in interactive mode,	      <BCLASS="COMMAND">units</B> may find use in a script.</P><DIVCLASS="EXAMPLE"><HR><ANAME="UNITCONVERSION"></A><P><B>Example 12-57. Converting meters to miles</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# unit-conversion.sh   3&nbsp;   4&nbsp;   5&nbsp;convert_units ()  # Takes as arguments the units to convert.   6&nbsp;{   7&nbsp;  cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')   8&nbsp;  # Strip off everything except the actual conversion factor.   9&nbsp;  echo "$cf"  10&nbsp;}    11&nbsp;  12&nbsp;Unit1=miles  13&nbsp;Unit2=meters  14&nbsp;cfactor=`convert_units $Unit1 $Unit2`  15&nbsp;quantity=3.73  16&nbsp;  17&nbsp;result=$(echo $quantity*$cfactor | bc)  18&nbsp;  19&nbsp;echo "There are $result $Unit2 in $quantity $Unit1."  20&nbsp;  21&nbsp;#  What happens if you pass incompatible units,  22&nbsp;#+ such as "acres" and "miles" to the function?  23&nbsp;  24&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">m4</B></DT><DD><P>A hidden treasure, <BCLASS="COMMAND">m4</B> is a	      powerful macro processing filter,		 <ANAME="AEN10683"HREF="#FTN.AEN10683">[5]</A>	      virtually a complete language. Although	      originally written as a pre-processor for	      <ICLASS="EMPHASIS">RatFor</I>, <BCLASS="COMMAND">m4</B>	      turned out to be useful as a stand-alone utility. In	      fact, <BCLASS="COMMAND">m4</B> combines some of the	      functionality of <AHREF="internal.html#EVALREF">eval</A>,	      <AHREF="textproc.html#TRREF">tr</A>, and <AHREF="awk.html#AWKREF">awk</A>, in addition to its extensive	      macro expansion facilities.</P><P>The April, 2002 issue of <AHREF="http://www.linuxjournal.com"TARGET="_top"><ICLASS="CITETITLE">Linux Journal</I></A>	      has a very nice article on <BCLASS="COMMAND">m4</B> and	      its uses.</P><DIVCLASS="EXAMPLE"><HR><ANAME="M4"></A><P><B>Example 12-58. Using m4</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# m4.sh: Using the m4 macro processor   3&nbsp;   4&nbsp;# Strings   5&nbsp;string=abcdA01   6&nbsp;echo "len($string)" | m4                           # 7   7&nbsp;echo "substr($string,4)" | m4                      # A01   8&nbsp;echo "regexp($string,[0-1][0-1],\&#38;Z)" | m4         # 01Z   9&nbsp;  10&nbsp;# Arithmetic  11&nbsp;echo "incr(22)" | m4                               # 23  12&nbsp;echo "eval(99 / 3)" | m4                           # 33  13&nbsp;  14&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">doexec</B></DT><DD><P>The <BCLASS="COMMAND">doexec</B> command enables passing	      an arbitrary list of arguments to a <ICLASS="EMPHASIS">binary	      executable</I>. In particular, passing	      <TTCLASS="VARNAME">argv[0]</TT> (which corresponds to <AHREF="othertypesv.html#POSPARAMREF1">$0</A> in a script) lets the	      executable be invoked by various names, and it can then	      carry out different sets of actions, according to the name	      by which it was called. What this amounts to is roundabout	      way of passing options to an executable.</P><P>For example, the <TTCLASS="FILENAME">/usr/local/bin</TT> directory might	      contain a binary called <SPANCLASS="QUOTE">"aaa"</SPAN>.  Invoking	      <BCLASS="COMMAND">doexec /usr/local/bin/aaa list</B>	      would <ICLASS="EMPHASIS">list</I> all those files	      in the current working directory beginning with an	      <SPANCLASS="QUOTE">"a"</SPAN>, while invoking (the same executable	      with) <BCLASS="COMMAND">doexec /usr/local/bin/aaa delete </B>	      would <ICLASS="EMPHASIS">delete</I> those files.</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 various behaviors of the executable              must be defined within the code of the executable itself,              analogous to something like the following in a shell script:                <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;case `basename $0` in   2&nbsp;"name1" ) do_something;;   3&nbsp;"name2" ) do_something_else;;   4&nbsp;"name3" ) do_yet_another_thing;;   5&nbsp;*       ) bail_out;;   6&nbsp;esac</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">dialog</B></DT><DD><P>The <AHREF="assortedtips.html#DIALOGREF">dialog</A> family of tools	      provide a method of calling interactive	      <SPANCLASS="QUOTE">"dialog"</SPAN> boxes from a script. The more	      elaborate variations of <BCLASS="COMMAND">dialog</B> --	      <BCLASS="COMMAND">gdialog</B>, <BCLASS="COMMAND">Xdialog</B>,	      and <BCLASS="COMMAND">kdialog</B> -- actually invoke X-Windows	      widgets. See <AHREF="assortedtips.html#DIALOG">Example 33-19</A>.</P></DD><DT><BCLASS="COMMAND">sox</B></DT><DD><P>The <BCLASS="COMMAND">sox</B>, or	      <SPANCLASS="QUOTE">"<ICLASS="EMPHASIS">so</I>und	      e<ICLASS="EMPHASIS">x</I>change"</SPAN> command plays and	      performs transformations on sound files. In fact,	      the <TTCLASS="FILENAME">/usr/bin/play</TT> executable	      (now deprecated) is nothing but a shell wrapper for	      <ICLASS="EMPHASIS">sox</I>.</P><P>For example, <BCLASS="COMMAND">sox soundfile.wav	      soundfile.au</B> changes a WAV sound file into a	      (Sun audio format) AU sound file.</P><P>Shell scripts are ideally suited for batch processing	      <BCLASS="COMMAND">sox</B> operations on	      sound files. For examples, see the <AHREF="http://osl.iu.edu/~tveldhui/radio/"TARGET="_top"> Linux Radio	      Timeshift HOWTO</A> and the <AHREF="http://savannah.nongnu.org/projects/audiodo"TARGET="_top">MP3do	      Project</A>.</P></DD></DL></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN10375"HREF="extmisc.html#AEN10375">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>This is actually a script adapted from		the Debian Linux distribution.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN10453"HREF="extmisc.html#AEN10453">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The <ICLASS="EMPHASIS">print queue</I> is	      the group of jobs <SPANCLASS="QUOTE">"waiting in line"</SPAN> to be	      printed.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN10515"HREF="extmisc.html#AEN10515">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>For an excellent overview of this		  topic, see Andy Vaught's article, <AHREF="http://www2.linuxjournal.com/lj-issues/issue41/2156.html"TARGET="_top">Introduction		  to Named Pipes</A>, in the September, 1997 issue of		  <AHREF="http://www.linuxjournal.com"TARGET="_top"><ICLASS="CITETITLE">Linux		  Journal</I></A>.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN10548"HREF="extmisc.html#AEN10548">[4]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><SPANCLASS="ACRONYM">EBCDIC</SPAN> (pronounced		  <SPANCLASS="QUOTE">"ebb-sid-ick"</SPAN>) is an acronym for Extended		  Binary Coded Decimal Interchange Code. This is an IBM		  data format no longer in much use. A bizarre		  application of the <TTCLASS="OPTION">conv=ebcdic</TT> option		  of <BCLASS="COMMAND">dd</B> is as a quick 'n easy, but		  not very secure text file encoder.		    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat $file | dd conv=swab,ebcdic &#62; $file_encrypted   2&nbsp;# Encode (looks like gibberish).		       3&nbsp;# Might as well switch bytes (swab), too, for a little extra obscurity.   4&nbsp;   5&nbsp;cat $file_encrypted | dd conv=swab,ascii &#62; $file_plaintext   6&nbsp;# Decode.</PRE></TD></TR></TABLE>                </P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN10683"HREF="extmisc.html#AEN10683">[5]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>A <ICLASS="EMPHASIS">macro</I> is a		 symbolic constant that expands into a command string		 or a set of operations on parameters.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="mathc.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="system.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Math Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="external.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">System and Administrative Commands</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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