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

📄 assortedtips.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  35&nbsp;#  Parameterize the filters in lines 11 - 13 (as with $FILTER),  36&nbsp;#+ so that they can be specified by passing arguments to a function.  37&nbsp;  38&nbsp;#  For a slightly different approach to anagramming,  39&nbsp;#+ see the agram2.sh script.</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="procref1.html#CONSTAT">Example 27-3</A>, <AHREF="textproc.html#CRYPTOQUOTE">Example 12-22</A>, and <AHREF="contributed-scripts.html#SOUNDEX">Example A-9</A>.</P></LI><LI><P>Use <SPANCLASS="QUOTE">"<AHREF="here-docs.html#ANONHEREDOC0">anonymous here	    documents</A>"</SPAN> to comment out blocks of code,	    to save having to individually comment out each line with	    a <SPANCLASS="TOKEN">#</SPAN>.  See <AHREF="here-docs.html#COMMENTBLOCK">Example 17-11</A>.</P></LI><LI><P>Running a script on a machine that relies on a command	    that might not be installed is dangerous. Use <AHREF="filearchiv.html#WHATISREF">whatis</A> to avoid potential problems	    with this.</P><P>	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;CMD=command1                 # First choice.   2&nbsp;PlanB=command2               # Fallback option.   3&nbsp;   4&nbsp;command_test=$(whatis "$CMD" | grep 'nothing appropriate')   5&nbsp;#  If 'command1' not found on system , 'whatis' will return   6&nbsp;#+ "command1: nothing appropriate."   7&nbsp;#   8&nbsp;#  A safer alternative is:   9&nbsp;#     command_test=$(whereis "$CMD" | grep \/)  10&nbsp;#  But then the sense of the following test would have to be reversed,  11&nbsp;#+ since the $command_test variable holds content only if  12&nbsp;#+ the $CMD exists on the system.  13&nbsp;#     (Thanks, bojster.)  14&nbsp;  15&nbsp;  16&nbsp;if [[ -z "$command_test" ]]  # Check whether command present.  17&nbsp;then  18&nbsp;  $CMD option1 option2       #  Run command1 with options.  19&nbsp;else                         #  Otherwise,  20&nbsp;  $PlanB                     #+ run command2.   21&nbsp;fi</PRE></TD></TR></TABLE>          </P></LI><LI><P>An <AHREF="tests.html#IFGREPREF">if-grep test</A> may not	    return expected results in an error case, when text is output to	    <TTCLASS="FILENAME">stderr</TT>, rather that	    <TTCLASS="FILENAME">stdout</TT>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if ls -l nonexistent_filename | grep -q 'No such file or directory'   2&nbsp;  then echo "File \"nonexistent_filename\" does not exist."   3&nbsp;fi</PRE></TD></TR></TABLE></P><P><AHREF="io-redirection.html#IOREDIRREF">Redirecting</A>	    <TTCLASS="FILENAME">stderr</TT> to <TTCLASS="FILENAME">stdout</TT> fixes	    this.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if ls -l nonexistent_filename 2&#62;&#38;1 | grep -q 'No such file or directory'   2&nbsp;#                             ^^^^   3&nbsp;  then echo "File \"nonexistent_filename\" does not exist."   4&nbsp;fi   5&nbsp;   6&nbsp;# Thanks, Chris Martin, for pointing this out.</PRE></TD></TR></TABLE></P></LI><LI><P>The <AHREF="extmisc.html#RUNPARTSREF">run-parts</A>	    command is handy for running a set of command	    scripts in sequence, particularly in combination	    with <AHREF="system.html#CRONREF">cron</A> or <AHREF="timedate.html#ATREF">at</A>.</P></LI><LI><P>It would be nice to be able to invoke X-Windows widgets	    from a shell script. There happen to exist	    several packages that purport to do so, namely	    <ICLASS="EMPHASIS">Xscript</I>, <ICLASS="EMPHASIS">Xmenu</I>,	    and <ICLASS="EMPHASIS">widtools</I>.	The first two of	    these no longer seem to be maintained.  Fortunately, it is	    still possible to obtain <ICLASS="EMPHASIS">widtools</I> <AHREF="http://www.batse.msfc.nasa.gov/~mallozzi/home/software/xforms/src/widtools-2.0.tgz"TARGET="_top">here</A>.	    </P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <ICLASS="EMPHASIS">widtools</I> (widget tools)	    package requires the <ICLASS="EMPHASIS">XForms</I> library to	    be installed. Additionally, the <AHREF="filearchiv.html#MAKEFILEREF">Makefile</A> needs some judicious	    editing before the package will build on a typical Linux	    system. Finally, three of the six widgets offered do not work	    (and, in fact, segfault).</P></TD></TR></TABLE></DIV><P><ANAME="DIALOGREF"></A></P><P>The <ICLASS="EMPHASIS">dialog</I> family of tools offers a method	    of calling <SPANCLASS="QUOTE">"dialog"</SPAN> widgets from a shell script. The	    original <BCLASS="COMMAND">dialog</B> utility works in a text	    console, but its successors, <BCLASS="COMMAND">gdialog</B>,	    <BCLASS="COMMAND">Xdialog</B>, and <BCLASS="COMMAND">kdialog</B>	    use X-Windows-based widget sets.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DIALOG"></A><P><B>Example 33-19. <BCLASS="COMMAND">Widgets invoked from a shell script</B></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# dialog.sh: Using 'gdialog' widgets.   3&nbsp;# Must have 'gdialog' installed on your system to run this script.   4&nbsp;# Version 1.1 (corrected 04/05/05)   5&nbsp;   6&nbsp;# This script was inspired by the following article.   7&nbsp;#     "Scripting for X Productivity," by Marco Fioretti,   8&nbsp;#      LINUX JOURNAL, Issue 113, September 2003, pp. 86-9.   9&nbsp;# Thank you, all you good people at LJ.  10&nbsp;  11&nbsp;  12&nbsp;# Input error in dialog box.  13&nbsp;E_INPUT=65  14&nbsp;# Dimensions of display, input widgets.  15&nbsp;HEIGHT=50  16&nbsp;WIDTH=60  17&nbsp;  18&nbsp;# Output file name (constructed out of script name).  19&nbsp;OUTFILE=$0.output  20&nbsp;  21&nbsp;# Display this script in a text widget.  22&nbsp;gdialog --title "Displaying: $0" --textbox $0 $HEIGHT $WIDTH  23&nbsp;  24&nbsp;  25&nbsp;  26&nbsp;# Now, we'll try saving input in a file.  27&nbsp;echo -n "VARIABLE=" &#62; $OUTFILE  28&nbsp;gdialog --title "User Input" --inputbox "Enter variable, please:" \  29&nbsp;$HEIGHT $WIDTH 2&#62;&#62; $OUTFILE  30&nbsp;  31&nbsp;  32&nbsp;if [ "$?" -eq 0 ]  33&nbsp;# It's good practice to check exit status.  34&nbsp;then  35&nbsp;  echo "Executed \"dialog box\" without errors."  36&nbsp;else  37&nbsp;  echo "Error(s) in \"dialog box\" execution."  38&nbsp;        # Or, clicked on "Cancel", instead of "OK" button.  39&nbsp;  rm $OUTFILE  40&nbsp;  exit $E_INPUT  41&nbsp;fi  42&nbsp;  43&nbsp;  44&nbsp;  45&nbsp;# Now, we'll retrieve and display the saved variable.  46&nbsp;. $OUTFILE   # 'Source' the saved file.  47&nbsp;echo "The variable input in the \"input box\" was: "$VARIABLE""  48&nbsp;  49&nbsp;  50&nbsp;rm $OUTFILE  # Clean up by removing the temp file.  51&nbsp;             # Some applications may need to retain this file.  52&nbsp;  53&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV><P>For other methods of scripting with widgets, try	    <ICLASS="EMPHASIS">Tk</I> or <ICLASS="EMPHASIS">wish</I>	    (<ICLASS="EMPHASIS">Tcl</I> derivatives),	    <ICLASS="EMPHASIS">PerlTk</I> (Perl with Tk extensions),	    <ICLASS="EMPHASIS">tksh</I> (ksh with Tk extensions),	    <ICLASS="EMPHASIS">XForms4Perl</I> (Perl with XForms	    extensions), <ICLASS="EMPHASIS">Gtk-Perl</I> (Perl with Gtk	    extensions), or <ICLASS="EMPHASIS">PyQt</I> (Python with	    Qt extensions).</P></LI><LI><P>For doing multiple revisions on a complex script, use the	    <ICLASS="EMPHASIS">rcs</I> Revision Control System package.</P><P> Among	    other benefits of this is automatically updated ID	    header tags. The <BCLASS="COMMAND">co</B> command in	    <ICLASS="EMPHASIS">rcs</I> does a parameter replacement of	    certain reserved key words, for example, replacing	    <TTCLASS="PARAMETER"><I>#$Id$</I></TT> in a script with something like:	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#$Id: hello-world.sh,v 1.1 2004/10/16 02:43:05 bozo Exp $</PRE></TD></TR></TABLE></P></LI></UL></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="optimizations.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="securityissues.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Optimizations</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="miscellany.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Security Issues</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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