📄 tests.html
字号:
></DIV><P> 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><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 <ICLASS="EMPHASIS">not</I> call the external <TTCLASS="FILENAME">/usr/bin/test</TT> binary, which is part of the <ICLASS="EMPHASIS">sh-utils</I> package. Likewise, <SPANCLASS="TOKEN">[</SPAN> 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></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX11"></A><P><B>Example 7-2. Equivalence of <SPANCLASS="TOKEN">test</SPAN>, <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 #!/bin/bash 2 3 echo 4 5 if test -z "$1" 6 then 7 echo "No command-line arguments." 8 else 9 echo "First command-line argument is $1." 10 fi 11 12 echo 13 14 if /usr/bin/test -z "$1" # Same result as "test" builtin". 15 then 16 echo "No command-line arguments." 17 else 18 echo "First command-line argument is $1." 19 fi 20 21 echo 22 23 if [ -z "$1" ] # Functionally identical to above code blocks. 24 # if [ -z "$1" should work, but... 25 #+ Bash responds to a missing close-bracket with an error message. 26 then 27 echo "No command-line arguments." 28 else 29 echo "First command-line argument is $1." 30 fi 31 32 echo 33 34 35 if /usr/bin/[ -z "$1" ] # Again, functionally identical to above. 36 # if /usr/bin/[ -z "$1" # Works, but gives an error message. 37 # # Note: 38 # This has been fixed in Bash, version 3.x. 39 then 40 echo "No command-line arguments." 41 else 42 echo "First command-line argument is $1." 43 fi 44 45 echo 46 47 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="EMPHASIS">extended test command</I>, adopted from <ICLASS="EMPHASIS">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 file=/etc/passwd 2 3 if [[ -e $file ]] 4 then 5 echo "Password file exists." 6 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">&&</SPAN>, <SPANCLASS="TOKEN">||</SPAN>, <SPANCLASS="TOKEN"><</SPAN>, and <SPANCLASS="TOKEN">></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 dir=/home/bozo 2 3 if cd "$dir" 2>/dev/null; then # "2>/dev/null" hides error message. 4 echo "Now in $dir." 5 else 6 echo "Can't change to $dir." 7 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 var1=20 2 var2=22 3 [ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2" 4 5 home=/home/bozo 6 [ -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 #!/bin/bash 2 # Arithmetic tests. 3 4 # The (( ... )) construct evaluates and tests numerical expressions. 5 # Exit status opposite from [ ... ] construct! 6 7 (( 0 )) 8 echo "Exit status of \"(( 0 ))\" is $?." # 1 9 10 (( 1 )) 11 echo "Exit status of \"(( 1 ))\" is $?." # 0 12 13 (( 5 > 4 )) # true 14 echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0 15 16 (( 5 > 9 )) # false 17 echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1 18 19 (( 5 - 5 )) # 0 20 echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1 21 22 (( 5 / 4 )) # Division o.k. 23 echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0 24 25 (( 1 / 2 )) # Division result < 1. 26 echo "Exit status of \"(( 1 / 2 ))\" is $?." # Rounded off to 0. 27 # 1 28 29 (( 1 / 0 )) 2>/dev/null # Illegal division by 0. 30 # ^^^^^^^^^^^ 31 echo "Exit status of \"(( 1 / 0 ))\" is $?." # 1 32 33 # What effect does the "2>/dev/null" have? 34 # What would happen if it were removed? 35 # Try removing it, then rerunning the script. 36 37 exit 0</PRE></TD></TR></TABLE><HR></DIV></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="exit-status.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="fto.html">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">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 + -