📄 extmisc.html
字号:
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 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 # Generate md5 checksum on the script itself. 2 random001=`md5sum $0 | awk '{print $1}'` 3 # 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 #!/bin/bash 2 # tempfile-name.sh: temp filename generator 3 4 BASE_STR=`mcookie` # 32-character magic cookie. 5 POS=11 # Arbitrary position in magic cookie string. 6 LEN=5 # Get $LEN consecutive characters. 7 8 prefix=temp # This is, after all, a "temp" file. 9 # For more "uniqueness," generate the filename prefix 10 #+ using the same method as the suffix, below. 11 12 suffix=${BASE_STR:POS:LEN} 13 # Extract a 5-character string, starting at position 11. 14 15 temp_filename=$prefix.$suffix 16 # Construct the filename. 17 18 echo "Temp filename = "$temp_filename"" 19 20 # sh tempfile-name.sh 21 # Temp filename = temp.e19ea 22 23 # Compare this method of generating "unique" filenames 24 #+ with the 'date' method in ex51.sh. 25 26 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 #!/bin/bash 2 # unit-conversion.sh 3 4 5 convert_units () # Takes as arguments the units to convert. 6 { 7 cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}') 8 # Strip off everything except the actual conversion factor. 9 echo "$cf" 10 } 11 12 Unit1=miles 13 Unit2=meters 14 cfactor=`convert_units $Unit1 $Unit2` 15 quantity=3.73 16 17 result=$(echo $quantity*$cfactor | bc) 18 19 echo "There are $result $Unit2 in $quantity $Unit1." 20 21 # What happens if you pass incompatible units, 22 #+ such as "acres" and "miles" to the function? 23 24 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 #!/bin/bash 2 # m4.sh: Using the m4 macro processor 3 4 # Strings 5 string=abcdA01 6 echo "len($string)" | m4 # 7 7 echo "substr($string,4)" | m4 # A01 8 echo "regexp($string,[0-1][0-1],\&Z)" | m4 # 01Z 9 10 # Arithmetic 11 echo "incr(22)" | m4 # 23 12 echo "eval(99 / 3)" | m4 # 33 13 14 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 case `basename $0` in 2 "name1" ) do_something;; 3 "name2" ) do_something_else;; 4 "name3" ) do_yet_another_thing;; 5 * ) bail_out;; 6 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 cat $file | dd conv=swab,ebcdic > $file_encrypted 2 # Encode (looks like gibberish). 3 # Might as well switch bytes (swab), too, for a little extra obscurity. 4 5 cat $file_encrypted | dd conv=swab,ascii > $file_plaintext 6 # 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 + -