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

📄 tests.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Tests</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Basics"HREF="part2.html"><LINKREL="PREVIOUS"TITLE="Exit and Exit Status"HREF="exit-status.html"><LINKREL="NEXT"TITLE="File test operators"HREF="fto.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="exit-status.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="fto.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="TESTS"></A>Chapter 7. Tests</H1><P><ANAME="IFTHEN"></A></P><P>Every reasonably complete programming language can test	  for a condition, then act according to the result of the	  test. Bash has the <AHREF="tests.html#TTESTREF">test</A>	  command, various bracket and parenthesis operators, and the	  <BCLASS="COMMAND">if/then</B> construct.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="TESTCONSTRUCTS"></A>7.1. Test Constructs</H1><P><ANAME="TESTCONSTRUCTS1"></A></P><UL><LI><P>An <BCLASS="COMMAND">if/then</B> construct tests whether the	  <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of a list	  of commands is <SPANCLASS="RETURNVALUE">0</SPAN> (since 0 means	  <SPANCLASS="QUOTE">"success"</SPAN> by UNIX convention), and if so, executes	  one or more commands.</P></LI><LI><P>There exists a dedicated command called <BCLASS="COMMAND">	[</B> (<AHREF="special-chars.html#LEFTBRACKET">left bracket</A>	special character). It is a synonym for <BCLASS="COMMAND">test</B>,	and a <AHREF="internal.html#BUILTINREF">builtin</A> for efficiency	reasons. This command considers its arguments as comparison	expressions or file tests and returns an exit status corresponding	to the result of the comparison (0 for true, 1 for false).</P></LI><LI><P>With version 2.02, Bash introduced the <AHREF="tests.html#DBLBRACKETS">[[ ... ]]</A> <ICLASS="FIRSTTERM">extended	  test command</I>, which performs comparisons	  in a manner more familiar to programmers from other	  languages. Note that <BCLASS="COMMAND">[[</B> is a <AHREF="internal.html#KEYWORDREF">keyword</A>, not a command.</P><P>Bash sees <TTCLASS="USERINPUT"><B>[[ $a -lt $b ]]</B></TT> as a	  single element, which returns an exit status.</P><P>The <AHREF="dblparens.html">(( ... ))</A> and <AHREF="internal.html#LETREF">let ...</A> constructs also return an	  exit status of <SPANCLASS="RETURNVALUE">0</SPAN> if the arithmetic	  expressions they evaluate expand to a non-zero value. These	  <AHREF="arithexp.html#ARITHEXPREF">arithmetic expansion</A>	  constructs may therefore be used to perform arithmetic	  comparisons.	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;let "1&#60;2" returns 0 (as "1&#60;2" expands to "1")   2&nbsp;(( 0 &#38;&#38; 1 )) returns 1 (as "0 &#38;&#38; 1" expands to "0")</PRE></TD></TR></TABLE>	    	  </P></LI><LI><P><ANAME="IFGREPREF"></A></P><P>An <BCLASS="COMMAND">if</B> can test any command, not just	    conditions enclosed within brackets.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if cmp a b &#38;&#62; /dev/null  # Suppress output.   2&nbsp;then echo "Files a and b are identical."   3&nbsp;else echo "Files a and b differ."   4&nbsp;fi   5&nbsp;   6&nbsp;# The very useful "if-grep" construct:   7&nbsp;# -----------------------------------    8&nbsp;if grep -q Bash file   9&nbsp;then echo "File contains at least one occurrence of Bash."  10&nbsp;fi  11&nbsp;  12&nbsp;word=Linux  13&nbsp;letter_sequence=inu  14&nbsp;if echo "$word" | grep -q "$letter_sequence"  15&nbsp;# The "-q" option to grep suppresses output.  16&nbsp;then  17&nbsp;  echo "$letter_sequence found in $word"  18&nbsp;else  19&nbsp;  echo "$letter_sequence not found in $word"  20&nbsp;fi  21&nbsp;  22&nbsp;  23&nbsp;if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED  24&nbsp;then echo "Command succeeded."  25&nbsp;else echo "Command failed."  26&nbsp;fi</PRE></TD></TR></TABLE>          </P></LI><LI><P>An <BCLASS="COMMAND">if/then</B> construct can contain nested	    comparisons and tests.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if echo "Next *if* is part of the comparison for the first *if*."   2&nbsp;   3&nbsp;  if [[ $comparison = "integer" ]]   4&nbsp;    then (( a &#60; b ))   5&nbsp;  else   6&nbsp;    [[ $a &#60; $b ]]   7&nbsp;  fi   8&nbsp;   9&nbsp;then  10&nbsp;  echo '$a is less than $b'  11&nbsp;fi</PRE></TD></TR></TABLE>          </P><P><SPANCLASS="emphasis"><ICLASS="EMPHASIS">This detailed <SPANCLASS="QUOTE">"if-test"</SPAN> explanation	  courtesy of St閜hane Chazelas.</I></SPAN></P></LI></UL><DIVCLASS="EXAMPLE"><HR><ANAME="EX10"></A><P><B>Example 7-1. What is truth?</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;#  Tip:   4&nbsp;#  If you're unsure of how a certain condition would evaluate,   5&nbsp;#+ test it in an if-test.   6&nbsp;   7&nbsp;echo   8&nbsp;   9&nbsp;echo "Testing \"0\""  10&nbsp;if [ 0 ]      # zero  11&nbsp;then  12&nbsp;  echo "0 is true."  13&nbsp;else  14&nbsp;  echo "0 is false."  15&nbsp;fi            # 0 is true.  16&nbsp;  17&nbsp;echo  18&nbsp;  19&nbsp;echo "Testing \"1\""  20&nbsp;if [ 1 ]      # one  21&nbsp;then  22&nbsp;  echo "1 is true."  23&nbsp;else  24&nbsp;  echo "1 is false."  25&nbsp;fi            # 1 is true.  26&nbsp;  27&nbsp;echo  28&nbsp;  29&nbsp;echo "Testing \"-1\""  30&nbsp;if [ -1 ]     # minus one  31&nbsp;then  32&nbsp;  echo "-1 is true."  33&nbsp;else  34&nbsp;  echo "-1 is false."  35&nbsp;fi            # -1 is true.  36&nbsp;  37&nbsp;echo  38&nbsp;  39&nbsp;echo "Testing \"NULL\""  40&nbsp;if [ ]        # NULL (empty condition)  41&nbsp;then  42&nbsp;  echo "NULL is true."  43&nbsp;else  44&nbsp;  echo "NULL is false."  45&nbsp;fi            # NULL is false.  46&nbsp;  47&nbsp;echo  48&nbsp;  49&nbsp;echo "Testing \"xyz\""  50&nbsp;if [ xyz ]    # string  51&nbsp;then  52&nbsp;  echo "Random string is true."  53&nbsp;else  54&nbsp;  echo "Random string is false."  55&nbsp;fi            # Random string is true.  56&nbsp;  57&nbsp;echo  58&nbsp;  59&nbsp;echo "Testing \"\$xyz\""  60&nbsp;if [ $xyz ]   # Tests if $xyz is null, but...  61&nbsp;              # it's only an uninitialized variable.  62&nbsp;then  63&nbsp;  echo "Uninitialized variable is true."  64&nbsp;else  65&nbsp;  echo "Uninitialized variable is false."  66&nbsp;fi            # Uninitialized variable is false.  67&nbsp;  68&nbsp;echo  69&nbsp;  70&nbsp;echo "Testing \"-n \$xyz\""  71&nbsp;if [ -n "$xyz" ]            # More pedantically correct.  72&nbsp;then  73&nbsp;  echo "Uninitialized variable is true."  74&nbsp;else  75&nbsp;  echo "Uninitialized variable is false."  76&nbsp;fi            # Uninitialized variable is false.  77&nbsp;  78&nbsp;echo  79&nbsp;  80&nbsp;  81&nbsp;xyz=          # Initialized, but set to null value.  82&nbsp;  83&nbsp;echo "Testing \"-n \$xyz\""  84&nbsp;if [ -n "$xyz" ]  85&nbsp;then  86&nbsp;  echo "Null variable is true."  87&nbsp;else  88&nbsp;  echo "Null variable is false."  89&nbsp;fi            # Null variable is false.  90&nbsp;  91&nbsp;  92&nbsp;echo  93&nbsp;  94&nbsp;  95&nbsp;# When is "false" true?  96&nbsp;  97&nbsp;echo "Testing \"false\""  98&nbsp;if [ "false" ]              #  It seems that "false" is just a string.  99&nbsp;then 100&nbsp;  echo "\"false\" is true." #+ and it tests true. 101&nbsp;else 102&nbsp;  echo "\"false\" is false." 103&nbsp;fi            # "false" is true. 104&nbsp; 105&nbsp;echo 106&nbsp; 107&nbsp;echo "Testing \"\$false\""  # Again, uninitialized variable. 108&nbsp;if [ "$false" ] 109&nbsp;then 110&nbsp;  echo "\"\$false\" is true." 111&nbsp;else 112&nbsp;  echo "\"\$false\" is false." 113&nbsp;fi            # "$false" is false. 114&nbsp;              # Now, we get the expected result. 115&nbsp; 116&nbsp;#  What would happen if we tested the uninitialized variable "$true"? 117&nbsp; 118&nbsp;echo 119&nbsp; 120&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="FORMALPARA"><P><B>Exercise. </B>Explain the behavior of <AHREF="tests.html#EX10">Example 7-1</A>, above.</P></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ condition-true ]   2&nbsp;then   3&nbsp;   command 1   4&nbsp;   command 2   5&nbsp;   ...   6&nbsp;else   7&nbsp;   # Optional (may be left out if not needed).   8&nbsp;   # Adds default code block executing if original condition tests false.   9&nbsp;   command 3  10&nbsp;   command 4  11&nbsp;   ...  12&nbsp;fi</PRE></TD></TR></TABLE>      </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>When <ICLASS="FIRSTTERM">if</I> and <ICLASS="FIRSTTERM">then</I>	are on same line in a condition test, a semicolon must	terminate the <ICLASS="FIRSTTERM">if</I> statement.  Both	<ICLASS="FIRSTTERM">if</I> and <ICLASS="FIRSTTERM">then</I>	are <AHREF="internal.html#KEYWORDREF">keywords</A>.  Keywords (or	commands) begin statements, and before a new statement on the	same line begins, the old one must terminate.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ -x "$filename" ]; then</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="ELIFREF1"></A>Else if and elif</B></P><DL><DT><SPANCLASS="TOKEN">elif</SPAN></DT><DD><P><TTCLASS="USERINPUT"><B>elif</B></TT> is a contraction	      for <SPANCLASS="TOKEN">else if</SPAN>.  The effect is to nest an	      inner <SPANCLASS="TOKEN">if/then</SPAN> construct within an outer	      one.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ condition1 ]   2&nbsp;then   3&nbsp;   command1   4&nbsp;   command2   5&nbsp;   command3   6&nbsp;elif [ condition2 ]   7&nbsp;# Same as else if   8&nbsp;then   9&nbsp;   command4  10&nbsp;   command5  11&nbsp;else  12&nbsp;   default-command  13&nbsp;fi</PRE></TD></TR></TABLE>	      </P></DD></DL></DIV><P>                                    	

⌨️ 快捷键说明

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