📄 assortedtips.html
字号:
35 # Parameterize the filters in lines 11 - 13 (as with $FILTER), 36 #+ so that they can be specified by passing arguments to a function. 37 38 # For a slightly different approach to anagramming, 39 #+ 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 CMD=command1 # First choice. 2 PlanB=command2 # Fallback option. 3 4 command_test=$(whatis "$CMD" | grep 'nothing appropriate') 5 # If 'command1' not found on system , 'whatis' will return 6 #+ "command1: nothing appropriate." 7 # 8 # A safer alternative is: 9 # command_test=$(whereis "$CMD" | grep \/) 10 # But then the sense of the following test would have to be reversed, 11 #+ since the $command_test variable holds content only if 12 #+ the $CMD exists on the system. 13 # (Thanks, bojster.) 14 15 16 if [[ -z "$command_test" ]] # Check whether command present. 17 then 18 $CMD option1 option2 # Run command1 with options. 19 else # Otherwise, 20 $PlanB #+ run command2. 21 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 if ls -l nonexistent_filename | grep -q 'No such file or directory' 2 then echo "File \"nonexistent_filename\" does not exist." 3 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 if ls -l nonexistent_filename 2>&1 | grep -q 'No such file or directory' 2 # ^^^^ 3 then echo "File \"nonexistent_filename\" does not exist." 4 fi 5 6 # 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 #!/bin/bash 2 # dialog.sh: Using 'gdialog' widgets. 3 # Must have 'gdialog' installed on your system to run this script. 4 # Version 1.1 (corrected 04/05/05) 5 6 # This script was inspired by the following article. 7 # "Scripting for X Productivity," by Marco Fioretti, 8 # LINUX JOURNAL, Issue 113, September 2003, pp. 86-9. 9 # Thank you, all you good people at LJ. 10 11 12 # Input error in dialog box. 13 E_INPUT=65 14 # Dimensions of display, input widgets. 15 HEIGHT=50 16 WIDTH=60 17 18 # Output file name (constructed out of script name). 19 OUTFILE=$0.output 20 21 # Display this script in a text widget. 22 gdialog --title "Displaying: $0" --textbox $0 $HEIGHT $WIDTH 23 24 25 26 # Now, we'll try saving input in a file. 27 echo -n "VARIABLE=" > $OUTFILE 28 gdialog --title "User Input" --inputbox "Enter variable, please:" \ 29 $HEIGHT $WIDTH 2>> $OUTFILE 30 31 32 if [ "$?" -eq 0 ] 33 # It's good practice to check exit status. 34 then 35 echo "Executed \"dialog box\" without errors." 36 else 37 echo "Error(s) in \"dialog box\" execution." 38 # Or, clicked on "Cancel", instead of "OK" button. 39 rm $OUTFILE 40 exit $E_INPUT 41 fi 42 43 44 45 # Now, we'll retrieve and display the saved variable. 46 . $OUTFILE # 'Source' the saved file. 47 echo "The variable input in the \"input box\" was: "$VARIABLE"" 48 49 50 rm $OUTFILE # Clean up by removing the temp file. 51 # Some applications may need to retain this file. 52 53 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 #$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 + -