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

📄 assortedtips.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
   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><ANAME="IFGREPFIX"></A></P><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><ANAME="SUBSHTMP"></A>        If you absolutely must access a subshell variable outside the	subshell, here's a way to do it.	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;TMPFILE=tmpfile                  # Create a temp file to store the variable.   2&nbsp;   3&nbsp;(   # Inside the subshell ...   4&nbsp;inner_variable=Inner   5&nbsp;echo $inner_variable   6&nbsp;echo $inner_variable &#62;&#62;$TMPFILE  # Append to temp file.   7&nbsp;)   8&nbsp;   9&nbsp;    # Outside the subshell ...  10&nbsp;  11&nbsp;echo; echo "-----"; echo  12&nbsp;echo $inner_variable             # Null, as expected.  13&nbsp;echo "-----"; echo  14&nbsp;  15&nbsp;# Now ...  16&nbsp;read inner_variable &#60;$TMPFILE    # Read back shell variable.  17&nbsp;rm -f "$TMPFILE"                 # Get rid of temp file.  18&nbsp;echo "$inner_variable"           # It's an ugly kludge, but it works.</PRE></TD></TR></TABLE>	</P></LI><LI><P><ANAME="RUNPARTSREF2"></A></P><P>The <AHREF="extmisc.html#RUNPARTSREF">run-parts</A>	    command is handy for running a set of command	    scripts in a particular sequence, especially in	    combination with <AHREF="system.html#CRONREF">cron</A> or	    <AHREF="timedate.html#ATREF">at</A>.</P></LI><LI><P><ANAME="RCSREF"></A></P><P>For doing multiple revisions on a complex script, use the	    <ICLASS="FIRSTTERM">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="FIRSTTERM">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="SECT2"><H2CLASS="SECT2"><ANAME="AEN19071"></A>33.8.2. Widgets</H2><P><ANAME="WIDGETREF"></A></P><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="FIRSTTERM">Xscript</I>,	    <ICLASS="FIRSTTERM">Xmenu</I>, and <ICLASS="FIRSTTERM">widtools</I>.	    The first two of these no longer seem	    to be maintained.  Fortunately, it is still	    possible to obtain <ICLASS="FIRSTTERM">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="100%"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="FIRSTTERM">widtools</I> (widget tools)	    package requires the <ICLASS="FIRSTTERM">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="FIRSTTERM">dialog</I> family of tools offers a method	    of calling <SPANCLASS="QUOTE">"dialog"</SPAN> widgets from a shell script. The	    original <ICLASS="FIRSTTERM">dialog</I> utility works in a text	    console, but its successors, <ICLASS="FIRSTTERM">gdialog</I>,	    <ICLASS="FIRSTTERM">Xdialog</I>, and <ICLASS="FIRSTTERM">kdialog</I>	    use X-Windows-based widget sets.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DIALOG"></A><P><B>Example 33-19. Widgets invoked from a shell script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# dialog.sh: Using 'gdialog' widgets.   3&nbsp;   4&nbsp;# Must have 'gdialog' installed on your system to run this script.   5&nbsp;# Or, you can replace all instance of 'gdialog' below with 'kdialog' ...   6&nbsp;# Version 1.1 (corrected 04/05/05)   7&nbsp;   8&nbsp;# This script was inspired by the following article.   9&nbsp;#     "Scripting for X Productivity," by Marco Fioretti,  10&nbsp;#      LINUX JOURNAL, Issue 113, September 2003, pp. 86-9.  11&nbsp;# Thank you, all you good people at LJ.  12&nbsp;  13&nbsp;  14&nbsp;# Input error in dialog box.  15&nbsp;E_INPUT=65  16&nbsp;# Dimensions of display, input widgets.  17&nbsp;HEIGHT=50  18&nbsp;WIDTH=60  19&nbsp;  20&nbsp;# Output file name (constructed out of script name).  21&nbsp;OUTFILE=$0.output  22&nbsp;  23&nbsp;# Display this script in a text widget.  24&nbsp;gdialog --title "Displaying: $0" --textbox $0 $HEIGHT $WIDTH  25&nbsp;  26&nbsp;  27&nbsp;  28&nbsp;# Now, we'll try saving input in a file.  29&nbsp;echo -n "VARIABLE=" &#62; $OUTFILE  30&nbsp;gdialog --title "User Input" --inputbox "Enter variable, please:" \  31&nbsp;$HEIGHT $WIDTH 2&#62;&#62; $OUTFILE  32&nbsp;  33&nbsp;  34&nbsp;if [ "$?" -eq 0 ]  35&nbsp;# It's good practice to check exit status.  36&nbsp;then  37&nbsp;  echo "Executed \"dialog box\" without errors."  38&nbsp;else  39&nbsp;  echo "Error(s) in \"dialog box\" execution."  40&nbsp;        # Or, clicked on "Cancel", instead of "OK" button.  41&nbsp;  rm $OUTFILE  42&nbsp;  exit $E_INPUT  43&nbsp;fi  44&nbsp;  45&nbsp;  46&nbsp;  47&nbsp;# Now, we'll retrieve and display the saved variable.  48&nbsp;. $OUTFILE   # 'Source' the saved file.  49&nbsp;echo "The variable input in the \"input box\" was: "$VARIABLE""  50&nbsp;  51&nbsp;  52&nbsp;rm $OUTFILE  # Clean up by removing the temp file.  53&nbsp;             # Some applications may need to retain this file.  54&nbsp;  55&nbsp;exit $?  56&nbsp;  57&nbsp;# Exercise: Rewrite this script using the 'zenity' widget set.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="XMESSAGEREF2"></A>	    The <AHREF="extmisc.html#XMESSAGEREF">xmessage</A> command is	    a simple method of popping up a message/query window. For	    example:	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;xmessage Fatal error in script! -button exit</PRE></TD></TR></TABLE>	  </P><P><ANAME="ZENITYREF2"></A>	    The latest entry in the widget sweepstakes is	    <AHREF="extmisc.html#ZENITYREF">zenity</A>.	    This utility pops up	    <ICLASS="FIRSTTERM">GTK+</I> dialog widgets-and-windows,	    and it works very nicely within a script.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;get_info ()   2&nbsp;{   3&nbsp;  zenity --entry       #  Pops up query window . . .   4&nbsp;                       #+ and prints user entry to stdout.   5&nbsp;   6&nbsp;                       #  Also try the --calendar and --scale options.   7&nbsp;}   8&nbsp;   9&nbsp;answer=$( get_info )   #  Capture stdout in $answer variable.  10&nbsp;  11&nbsp;echo "User entered: "$answer""</PRE></TD></TR></TABLE>	  </P><P>For other methods of scripting with widgets, try	    <ICLASS="FIRSTTERM">Tk</I> or <ICLASS="FIRSTTERM">wish</I>	    (<ICLASS="FIRSTTERM">Tcl</I> derivatives),	    <ICLASS="FIRSTTERM">PerlTk</I> (<ICLASS="FIRSTTERM">Perl</I>	    with <ICLASS="FIRSTTERM">Tk</I> extensions),	    <ICLASS="FIRSTTERM">tksh</I> (<ICLASS="FIRSTTERM">ksh</I>	    with <ICLASS="FIRSTTERM">Tk</I> extensions),	    <ICLASS="FIRSTTERM">XForms4Perl</I>	    (<ICLASS="FIRSTTERM">Perl</I> with	    <ICLASS="FIRSTTERM">XForms</I> extensions),	    <ICLASS="FIRSTTERM">Gtk-Perl</I> (<ICLASS="FIRSTTERM">Perl</I>	    with <ICLASS="FIRSTTERM">Gtk</I> extensions), or	    <ICLASS="FIRSTTERM">PyQt</I> (<ICLASS="FIRSTTERM">Python</I>	    with <ICLASS="FIRSTTERM">Qt</I> extensions).</P></DIV></DIV><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="optimizations.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="securityissues.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Optimizations</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="miscellany.html"ACCESSKEY="U">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 + -