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

📄 tests.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
	The <TTCLASS="USERINPUT"><B>if test condition-true</B></TT> construct is the	exact equivalent of <TTCLASS="USERINPUT"><B>if [ condition-true ]</B></TT>.	As it happens, the left bracket, <BCLASS="COMMAND">[</B> , is a token	which invokes the <BCLASS="COMMAND">test</B> command.  The closing	right bracket, <BCLASS="COMMAND">]</B> , in an if/test should not	therefore be strictly necessary, however newer versions of Bash	require it.</P><P><ANAME="TTESTREF"></A></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>The <BCLASS="COMMAND">test</B> command is a Bash <AHREF="internal.html#BUILTINREF">builtin</A> which tests file	  types and compares strings. Therefore, in a Bash script,	  <BCLASS="COMMAND">test</B> does <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> call	  the external <TTCLASS="FILENAME">/usr/bin/test</TT> binary,	  which is part of the <ICLASS="FIRSTTERM">sh-utils</I>	  package. Likewise, <BCLASS="COMMAND">[</B> does not call	  <TTCLASS="FILENAME">/usr/bin/[</TT>, which is linked to	  <TTCLASS="FILENAME">/usr/bin/test</TT>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>type test</B></TT> <TTCLASS="COMPUTEROUTPUT">test is a shell builtin</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>type '['</B></TT> <TTCLASS="COMPUTEROUTPUT">[ is a shell builtin</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>type '[['</B></TT> <TTCLASS="COMPUTEROUTPUT">[[ is a shell keyword</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>type ']]'</B></TT> <TTCLASS="COMPUTEROUTPUT">]] is a shell keyword</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>type ']'</B></TT> <TTCLASS="COMPUTEROUTPUT">bash: type: ]: not found</TT> 	      </PRE></TD></TR></TABLE>	    </P><P><ANAME="USRBINTEST"></A></P><P>If, for some reason, you wish to use	  <TTCLASS="FILENAME">/usr/bin/test</TT> in a Bash script,	  then specify it by full pathname.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX11"></A><P><B>Example 7-2. Equivalence of <ICLASS="FIRSTTERM">test</I>,	  <TTCLASS="FILENAME">/usr/bin/test</TT>, <SPANCLASS="TOKEN">[ ]</SPAN>,	  and <TTCLASS="FILENAME">/usr/bin/[</TT></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo   4&nbsp;   5&nbsp;if test -z "$1"   6&nbsp;then   7&nbsp;  echo "No command-line arguments."   8&nbsp;else   9&nbsp;  echo "First command-line argument is $1."  10&nbsp;fi  11&nbsp;  12&nbsp;echo  13&nbsp;  14&nbsp;if /usr/bin/test -z "$1"      # Equivalent to "test" builtin.  15&nbsp;#  ^^^^^^^^^^^^^              # Specifying full pathname.  16&nbsp;then  17&nbsp;  echo "No command-line arguments."  18&nbsp;else  19&nbsp;  echo "First command-line argument is $1."  20&nbsp;fi  21&nbsp;  22&nbsp;echo  23&nbsp;  24&nbsp;if [ -z "$1" ]                # Functionally identical to above code blocks.  25&nbsp;#   if [ -z "$1"                should work, but...  26&nbsp;#+  Bash responds to a missing close-bracket with an error message.  27&nbsp;then  28&nbsp;  echo "No command-line arguments."  29&nbsp;else  30&nbsp;  echo "First command-line argument is $1."  31&nbsp;fi  32&nbsp;  33&nbsp;echo  34&nbsp;  35&nbsp;  36&nbsp;if /usr/bin/[ -z "$1" ]       # Again, functionally identical to above.  37&nbsp;# if /usr/bin/[ -z "$1"       # Works, but gives an error message.  38&nbsp;#                             # Note:  39&nbsp;#                               This has been fixed in Bash, version 3.x.  40&nbsp;then  41&nbsp;  echo "No command-line arguments."  42&nbsp;else  43&nbsp;  echo "First command-line argument is $1."  44&nbsp;fi  45&nbsp;  46&nbsp;echo  47&nbsp;  48&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="DBLBRACKETS"></A>The <SPANCLASS="TOKEN">[[  ]]</SPAN> construct	is the more versatile Bash version of <SPANCLASS="TOKEN">[	]</SPAN>. This	is the <ICLASS="FIRSTTERM">extended test command</I>, adopted from	<ICLASS="FIRSTTERM">ksh88</I>.</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>No filename expansion or word splitting takes place	between <SPANCLASS="TOKEN">[[</SPAN> and <SPANCLASS="TOKEN">]]</SPAN>, but there is	parameter expansion and command substitution.</P></TD></TR></TABLE></DIV><P>	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;file=/etc/passwd   2&nbsp;   3&nbsp;if [[ -e $file ]]   4&nbsp;then   5&nbsp;  echo "Password file exists."   6&nbsp;fi</PRE></TD></TR></TABLE>      </P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Using the <BCLASS="COMMAND">[[ ... ]]</B> test construct,	rather than <BCLASS="COMMAND">[ ... ]</B> can prevent many	logic errors in scripts. For example, the <SPANCLASS="TOKEN">&#38;&#38;</SPAN>,	<SPANCLASS="TOKEN">||</SPAN>, <SPANCLASS="TOKEN">&#60;</SPAN>, and <SPANCLASS="TOKEN">&#62;</SPAN>	operators work within a <SPANCLASS="TOKEN">[[  ]]</SPAN> test, despite	giving an error within a <SPANCLASS="TOKEN">[  ]</SPAN> construct.</P></TD></TR></TABLE></DIV><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>Following an <BCLASS="COMMAND">if</B>, neither the        <BCLASS="COMMAND">test</B> command nor the test brackets ( [ ] or [[ ]] )	are strictly necessary.	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;dir=/home/bozo   2&nbsp;   3&nbsp;if cd "$dir" 2&#62;/dev/null; then   # "2&#62;/dev/null" hides error message.   4&nbsp;  echo "Now in $dir."   5&nbsp;else   6&nbsp;  echo "Can't change to $dir."   7&nbsp;fi</PRE></TD></TR></TABLE>        The "if COMMAND" construct returns the exit status of COMMAND.</P><P>Similarly, a condition within test brackets may stand alone	  without an <BCLASS="COMMAND">if</B>, when used in combination	  with a <AHREF="list-cons.html#LISTCONSREF">list construct</A>.	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;var1=20   2&nbsp;var2=22   3&nbsp;[ "$var1" -ne "$var2" ] &#38;&#38; echo "$var1 is not equal to $var2"   4&nbsp;   5&nbsp;home=/home/bozo   6&nbsp;[ -d "$home" ] || echo "$home directory does not exist."</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV><P>The <AHREF="dblparens.html">(( )) construct</A> expands	and evaluates an arithmetic expression. If	the expression evaluates as zero, it returns an	<AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of	<SPANCLASS="RETURNVALUE">1</SPAN>, or <SPANCLASS="QUOTE">"false"</SPAN>. A non-zero	expression returns an exit status of <SPANCLASS="RETURNVALUE">0</SPAN>,	or <SPANCLASS="QUOTE">"true"</SPAN>. This is in marked contrast to using	the <BCLASS="COMMAND">test</B> and <SPANCLASS="TOKEN">[ ]</SPAN> constructs	previously discussed.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ARITHTESTS"></A><P><B>Example 7-3. Arithmetic Tests using <SPANCLASS="TOKEN">(( ))</SPAN></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Arithmetic tests.   3&nbsp;   4&nbsp;# The (( ... )) construct evaluates and tests numerical expressions.   5&nbsp;# Exit status opposite from [ ... ] construct!   6&nbsp;   7&nbsp;(( 0 ))   8&nbsp;echo "Exit status of \"(( 0 ))\" is $?."         # 1   9&nbsp;  10&nbsp;(( 1 ))  11&nbsp;echo "Exit status of \"(( 1 ))\" is $?."         # 0  12&nbsp;  13&nbsp;(( 5 &#62; 4 ))                                      # true  14&nbsp;echo "Exit status of \"(( 5 &#62; 4 ))\" is $?."     # 0  15&nbsp;  16&nbsp;(( 5 &#62; 9 ))                                      # false  17&nbsp;echo "Exit status of \"(( 5 &#62; 9 ))\" is $?."     # 1  18&nbsp;  19&nbsp;(( 5 - 5 ))                                      # 0  20&nbsp;echo "Exit status of \"(( 5 - 5 ))\" is $?."     # 1  21&nbsp;  22&nbsp;(( 5 / 4 ))                                      # Division o.k.  23&nbsp;echo "Exit status of \"(( 5 / 4 ))\" is $?."     # 0  24&nbsp;  25&nbsp;(( 1 / 2 ))                                      # Division result &#60; 1.  26&nbsp;echo "Exit status of \"(( 1 / 2 ))\" is $?."     # Rounded off to 0.  27&nbsp;                                                 # 1  28&nbsp;  29&nbsp;(( 1 / 0 )) 2&#62;/dev/null                          # Illegal division by 0.  30&nbsp;#           ^^^^^^^^^^^  31&nbsp;echo "Exit status of \"(( 1 / 0 ))\" is $?."     # 1  32&nbsp;  33&nbsp;# What effect does the "2&#62;/dev/null" have?  34&nbsp;# What would happen if it were removed?  35&nbsp;# Try removing it, then rerunning the script.  36&nbsp;  37&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></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="exit-status.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="fto.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Exit and Exit Status</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">File test operators</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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