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

📄 list-cons.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>List Constructs</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="Aliases"HREF="aliases.html"><LINKREL="NEXT"TITLE="Arrays"HREF="arrays.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="aliases.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="arrays.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="LIST-CONS"></A>Chapter 25. List Constructs</H1><P><ANAME="LISTCONSREF"></A></P><P>The <SPANCLASS="QUOTE">"and list"</SPAN> and <SPANCLASS="QUOTE">"or list"</SPAN>	constructs provide a means of processing a number of commands	consecutively. These can effectively replace complex 	nested <BCLASS="COMMAND">if</B>/<BCLASS="COMMAND">then</B> or even	<BCLASS="COMMAND">case</B> statements.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="LCONS1"></A>Chaining together commands</B></P><DL><DT>and list</DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;command-1 &#38;&#38; command-2 &#38;&#38; command-3 &#38;&#38; ... command-n</PRE></TD></TR></TABLE>	      Each command executes in turn provided that	      the previous command has given a return value of	      <SPANCLASS="RETURNVALUE">true</SPAN> (zero). At the first	      <SPANCLASS="RETURNVALUE">false</SPAN> (non-zero) return, the	      command chain terminates (the first command returning	      <SPANCLASS="RETURNVALUE">false</SPAN> is the last one to	      execute).</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX64"></A><P><B>Example 25-1. Using an <ICLASS="FIRSTTERM">and list</I> to test	      for command-line arguments</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# "and list"   3&nbsp;   4&nbsp;if [ ! -z "$1" ] &#38;&#38; echo "Argument #1 = $1" &#38;&#38; [ ! -z "$2" ] \   5&nbsp;&#38;&#38; echo "Argument #2 = $2"   6&nbsp;then   7&nbsp;  echo "At least 2 arguments passed to script."   8&nbsp;  # All the chained commands return true.   9&nbsp;else  10&nbsp;  echo "Less than 2 arguments passed to script."  11&nbsp;  # At least one of the chained commands returns false.  12&nbsp;fi    13&nbsp;# Note that "if [ ! -z $1 ]" works, but its supposed equivalent,  14&nbsp;#   if [ -n $1 ] does not.  15&nbsp;#     However, quoting fixes this.  16&nbsp;#  if [ -n "$1" ] works.  17&nbsp;#     Careful!  18&nbsp;# It is always best to QUOTE tested variables.  19&nbsp;  20&nbsp;  21&nbsp;# This accomplishes the same thing, using "pure" if/then statements.  22&nbsp;if [ ! -z "$1" ]  23&nbsp;then  24&nbsp;  echo "Argument #1 = $1"  25&nbsp;fi  26&nbsp;if [ ! -z "$2" ]  27&nbsp;then  28&nbsp;  echo "Argument #2 = $2"  29&nbsp;  echo "At least 2 arguments passed to script."  30&nbsp;else  31&nbsp;  echo "Less than 2 arguments passed to script."  32&nbsp;fi  33&nbsp;# It's longer and less elegant than using an "and list".  34&nbsp;  35&nbsp;  36&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ANDLIST2"></A><P><B>Example 25-2. Another command-line arg test using an <ICLASS="FIRSTTERM">and	      list</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;ARGS=1        # Number of arguments expected.   4&nbsp;E_BADARGS=65  # Exit value if incorrect number of args passed.   5&nbsp;   6&nbsp;test $# -ne $ARGS &#38;&#38; \   7&nbsp;echo "Usage: `basename $0` $ARGS argument(s)" &#38;&#38; exit $E_BADARGS   8&nbsp;#  If condition 1 tests true (wrong number of args passed to script),   9&nbsp;#+ then the rest of the line executes, and script terminates.  10&nbsp;  11&nbsp;# Line below executes only if the above test fails.  12&nbsp;echo "Correct number of arguments passed to this script."  13&nbsp;  14&nbsp;exit 0  15&nbsp;  16&nbsp;# To check exit value, do a "echo $?" after script termination.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="ANDDEFAULT"></A></P><P>	      Of course, an <ICLASS="FIRSTTERM">and list</I> can also	      <ICLASS="FIRSTTERM">set</I> variables to a default value.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;arg1=$@ &#38;&#38; [ -z "$arg1" ] &#38;&#38; arg1=DEFAULT   2&nbsp;		   3&nbsp;              # Set $arg1 to command line arguments, if any.   4&nbsp;              # But . . . set to DEFAULT if not specified on command line.</PRE></TD></TR></TABLE>            </P></DD><DT><ANAME="ORLISTREF"></A>or list</DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;command-1 || command-2 || command-3 || ... command-n</PRE></TD></TR></TABLE>	      Each command executes in turn for as long as the previous	      command returns <SPANCLASS="RETURNVALUE">false</SPAN>. At	      the first <SPANCLASS="RETURNVALUE">true</SPAN> return, the	      command chain terminates (the first command returning	      <SPANCLASS="RETURNVALUE">true</SPAN> is the last one to	      execute). This is obviously the inverse of the <SPANCLASS="QUOTE">"and	      list"</SPAN>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX65"></A><P><B>Example 25-3. Using <ICLASS="FIRSTTERM">or lists</I> in combination	      with an <ICLASS="FIRSTTERM">and list</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  delete.sh, not-so-cunning file deletion utility.   4&nbsp;#  Usage: delete filename   5&nbsp;   6&nbsp;E_BADARGS=65   7&nbsp;   8&nbsp;if [ -z "$1" ]   9&nbsp;then  10&nbsp;  echo "Usage: `basename $0` filename"  11&nbsp;  exit $E_BADARGS  # No arg? Bail out.  12&nbsp;else    13&nbsp;  file=$1          # Set filename.  14&nbsp;fi    15&nbsp;  16&nbsp;  17&nbsp;[ ! -f "$file" ] &#38;&#38; echo "File \"$file\" not found. \  18&nbsp;Cowardly refusing to delete a nonexistent file."  19&nbsp;# AND LIST, to give error message if file not present.  20&nbsp;# Note echo message continued on to a second line with an escape.  21&nbsp;  22&nbsp;[ ! -f "$file" ] || (rm -f $file; echo "File \"$file\" deleted.")  23&nbsp;# OR LIST, to delete file if present.  24&nbsp;  25&nbsp;# Note logic inversion above.  26&nbsp;# AND LIST executes on true, OR LIST on false.  27&nbsp;  28&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><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>If the first command in an <SPANCLASS="QUOTE">"or list"</SPAN>	      returns <SPANCLASS="RETURNVALUE">true</SPAN>, it	      <TTCLASS="REPLACEABLE"><I>will</I></TT> execute.</P></TD></TR></TABLE></DIV></DD></DL></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# ==&#62; The following snippets from the /etc/rc.d/init.d/single   2&nbsp;#+==&#62; script by Miquel van Smoorenburg   3&nbsp;#+==&#62; illustrate use of "and" and "or" lists.   4&nbsp;# ==&#62; "Arrowed" comments added by document author.   5&nbsp;   6&nbsp;[ -x /usr/bin/clear ] &#38;&#38; /usr/bin/clear   7&nbsp;  # ==&#62; If /usr/bin/clear exists, then invoke it.   8&nbsp;  # ==&#62; Checking for the existence of a command before calling it   9&nbsp;  #+==&#62; avoids error messages and other awkward consequences.  10&nbsp;  11&nbsp;  # ==&#62; . . .  12&nbsp;  13&nbsp;# If they want to run something in single user mode, might as well run it...  14&nbsp;for i in /etc/rc1.d/S[0-9][0-9]* ; do  15&nbsp;        # Check if the script is there.  16&nbsp;        [ -x "$i" ] || continue  17&nbsp;  # ==&#62; If corresponding file in $PWD *not* found,  18&nbsp;  #+==&#62; then "continue" by jumping to the top of the loop.  19&nbsp;  20&nbsp;        # Reject backup files and files generated by rpm.  21&nbsp;        case "$1" in  22&nbsp;                *.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)  23&nbsp;                        continue;;  24&nbsp;        esac  25&nbsp;        [ "$i" = "/etc/rc1.d/S00single" ] &#38;&#38; continue  26&nbsp;  # ==&#62; Set script name, but don't execute it yet.  27&nbsp;        $i start  28&nbsp;done  29&nbsp;  30&nbsp;  # ==&#62; . . .</PRE></TD></TR></TABLE></P><DIVCLASS="IMPORTANT"><TABLECLASS="IMPORTANT"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/important.png"HSPACE="5"ALT="Important"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <AHREF="exit-status.html#EXITSTATUSREF">exit	  status</A> of an <TTCLASS="USERINPUT"><B>and list</B></TT> or an	  <TTCLASS="USERINPUT"><B>or list</B></TT> is the exit status of the last	  command executed.</P></TD></TR></TABLE></DIV><P>Clever combinations of <SPANCLASS="QUOTE">"and"</SPAN> and <SPANCLASS="QUOTE">"or"</SPAN>	lists are possible, but the logic may easily become convoluted and	require close attention to <AHREF="opprecedence.html#OPPRECEDENCE1">operator	precedence rules</A>, and possibly extensive debugging.	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;false &#38;&#38; true || echo false         # false   2&nbsp;   3&nbsp;# Same result as   4&nbsp;( false &#38;&#38; true ) || echo false     # false   5&nbsp;# But *not*   6&nbsp;false &#38;&#38; ( true || echo false )     # (nothing echoed)   7&nbsp;   8&nbsp;#  Note left-to-right grouping and evaluation of statements,   9&nbsp;#+ since the logic operators "&#38;&#38;" and "||" have equal precedence.  10&nbsp;  11&nbsp;#  It's best to avoid such complexities, unless you know what you're doing.  12&nbsp;  13&nbsp;#  Thanks, S.C.</PRE></TD></TR></TABLE>	</P><P>See <AHREF="contributed-scripts.html#DAYSBETWEEN">Example A-7</A> and <AHREF="fto.html#BROKENLINK">Example 7-4</A> for illustrations of using an <TTCLASS="USERINPUT"><B>and	/ or list</B></TT> to test variables.</P></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="aliases.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="arrays.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Aliases</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Arrays</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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