📄 extmisc.html
字号:
><DIVCLASS="EXAMPLE"><HR><ANAME="UNITCONVERSION"></A><P><B>Example 15-60. 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><ANAME="M4REF"></A><BCLASS="COMMAND">m4</B></DT><DD><P>A hidden treasure, <BCLASS="COMMAND">m4</B> is a powerful macro processing filter, <ANAME="AEN13268"HREF="#FTN.AEN13268">[5]</A> virtually a complete language. Although originally written as a pre-processor for <ICLASS="FIRSTTERM">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 15-61. Using <ICLASS="FIRSTTERM">m4</I></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><ANAME="XMESSAGEREF"></A><BCLASS="COMMAND">xmessage</B></DT><DD><P>This X-based variant of <AHREF="internal.html#ECHOREF">echo</A> pops up a message/query window on the desktop.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 xmessage Left click to continue -button okay</PRE></TD></TR></TABLE> </P></DD><DT><ANAME="ZENITYREF"></A><BCLASS="COMMAND">zenity</B></DT><DD><P>The <AHREF="http://freshmeat.net/projects/zenity"TARGET="_top">zenity</A> utility is adept at displaying <ICLASS="FIRSTTERM">GTK+</I> dialog <AHREF="assortedtips.html#WIDGETREF">widgets</A> and <AHREF="assortedtips.html#ZENITYREF2">very suitable for scripting purposes</A>.</P></DD><DT><ANAME="DOEXECREF"></A><BCLASS="COMMAND">doexec</B></DT><DD><P>The <BCLASS="COMMAND">doexec</B> command enables passing an arbitrary list of arguments to a <ICLASS="FIRSTTERM">binary executable</I>. In particular, passing <TTCLASS="PARAMETER"><I>argv[0]</I></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 <SPANCLASS="emphasis"><ICLASS="EMPHASIS">list</I></SPAN> 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 <SPANCLASS="emphasis"><ICLASS="EMPHASIS">delete</I></SPAN> 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 <AHREF="assortedtips.html#WIDGETREF">widgets</A>.</P></DD><DT><ANAME="SOXREF"></A><BCLASS="COMMAND">sox</B></DT><DD><P>The <BCLASS="COMMAND">sox</B>, or <SPANCLASS="QUOTE">"<BCLASS="COMMAND">so</B>und e<BCLASS="COMMAND">x</B>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="FIRSTTERM">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.AEN12881"HREF="extmisc.html#AEN12881">[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.AEN12974"HREF="extmisc.html#AEN12974">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The <ICLASS="FIRSTTERM">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.AEN13038"HREF="extmisc.html#AEN13038">[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.AEN13075"HREF="extmisc.html#AEN13075">[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.AEN13268"HREF="extmisc.html#AEN13268">[5]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>A <ICLASS="FIRSTTERM">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%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="mathc.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="system.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Math Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="external.html"ACCESSKEY="U">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 + -