📄 comparison-ops.html
字号:
CLASS="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>The <TTCLASS="USERINPUT"><B>-n</B></TT> test absolutely requires that the string be quoted within the test brackets. Using an unquoted string with <TTCLASS="USERINPUT"><B>! -z</B></TT>, or even just the unquoted string alone within test brackets (see <AHREF="comparison-ops.html#STRTEST">Example 7-6</A>) normally works, however, this is an unsafe practice. <SPANCLASS="emphasis"><ICLASS="EMPHASIS">Always</I></SPAN> quote a tested string. <ANAME="AEN3266"HREF="#FTN.AEN3266">[1]</A> </P></TD></TR></TABLE></DIV></DD><DT><ANAME="STRINGNULL"></A><SPANCLASS="TOKEN">-z</SPAN></DT><DD><P>string is <SPANCLASS="QUOTE">"null, "</SPAN> that is, has zero length</P></DD></DL></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX13"></A><P><B>Example 7-5. Arithmetic and string comparisons</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 a=4 4 b=5 5 6 # Here "a" and "b" can be treated either as integers or strings. 7 # There is some blurring between the arithmetic and string comparisons, 8 #+ since Bash variables are not strongly typed. 9 10 # Bash permits integer operations and comparisons on variables 11 #+ whose value consists of all-integer characters. 12 # Caution advised, however. 13 14 echo 15 16 if [ "$a" -ne "$b" ] 17 then 18 echo "$a is not equal to $b" 19 echo "(arithmetic comparison)" 20 fi 21 22 echo 23 24 if [ "$a" != "$b" ] 25 then 26 echo "$a is not equal to $b." 27 echo "(string comparison)" 28 # "4" != "5" 29 # ASCII 52 != ASCII 53 30 fi 31 32 # In this particular instance, both "-ne" and "!=" work. 33 34 echo 35 36 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="STRTEST"></A><P><B>Example 7-6. Testing whether a string is <ICLASS="FIRSTTERM">null</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # str-test.sh: Testing null strings and unquoted strings, 3 #+ but not strings and sealing wax, not to mention cabbages and kings . . . 4 5 # Using if [ ... ] 6 7 8 # If a string has not been initialized, it has no defined value. 9 # This state is called "null" (not the same as zero). 10 11 if [ -n $string1 ] # $string1 has not been declared or initialized. 12 then 13 echo "String \"string1\" is not null." 14 else 15 echo "String \"string1\" is null." 16 fi 17 # Wrong result. 18 # Shows $string1 as not null, although it was not initialized. 19 20 21 echo 22 23 24 # Lets try it again. 25 26 if [ -n "$string1" ] # This time, $string1 is quoted. 27 then 28 echo "String \"string1\" is not null." 29 else 30 echo "String \"string1\" is null." 31 fi # Quote strings within test brackets! 32 33 34 echo 35 36 37 if [ $string1 ] # This time, $string1 stands naked. 38 then 39 echo "String \"string1\" is not null." 40 else 41 echo "String \"string1\" is null." 42 fi 43 # This works fine. 44 # The [ ] test operator alone detects whether the string is null. 45 # However it is good practice to quote it ("$string1"). 46 # 47 # As Stephane Chazelas points out, 48 # if [ $string1 ] has one argument, "]" 49 # if [ "$string1" ] has two arguments, the empty "$string1" and "]" 50 51 52 53 echo 54 55 56 57 string1=initialized 58 59 if [ $string1 ] # Again, $string1 stands naked. 60 then 61 echo "String \"string1\" is not null." 62 else 63 echo "String \"string1\" is null." 64 fi 65 # Again, gives correct result. 66 # Still, it is better to quote it ("$string1"), because . . . 67 68 69 string1="a = b" 70 71 if [ $string1 ] # Again, $string1 stands naked. 72 then 73 echo "String \"string1\" is not null." 74 else 75 echo "String \"string1\" is null." 76 fi 77 # Not quoting "$string1" now gives wrong result! 78 79 exit 0 80 # Thank you, also, Florian Wisser, for the "heads-up".</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX14"></A><P><B>Example 7-7. <ICLASS="FIRSTTERM">zmore</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # zmore 3 4 #View gzipped files with 'more' 5 6 NOARGS=65 7 NOTFOUND=66 8 NOTGZIP=67 9 10 if [ $# -eq 0 ] # same effect as: if [ -z "$1" ] 11 # $1 can exist, but be empty: zmore "" arg2 arg3 12 then 13 echo "Usage: `basename $0` filename" >&2 14 # Error message to stderr. 15 exit $NOARGS 16 # Returns 65 as exit status of script (error code). 17 fi 18 19 filename=$1 20 21 if [ ! -f "$filename" ] # Quoting $filename allows for possible spaces. 22 then 23 echo "File $filename not found!" >&2 24 # Error message to stderr. 25 exit $NOTFOUND 26 fi 27 28 if [ ${filename##*.} != "gz" ] 29 # Using bracket in variable substitution. 30 then 31 echo "File $1 is not a gzipped file!" 32 exit $NOTGZIP 33 fi 34 35 zcat $1 | more 36 37 # Uses the filter 'more.' 38 # May substitute 'less', if desired. 39 40 41 exit $? # Script returns exit status of pipe. 42 # Actually "exit $?" is unnecessary, as the script will, in any case, 43 # return the exit status of the last command executed.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="CCOMPARISON1"></A>compound comparison</B></P><DL><DT><ANAME="COMPOUNDAND"></A><SPANCLASS="TOKEN">-a</SPAN></DT><DD><P>logical and</P><P><TTCLASS="REPLACEABLE"><I>exp1 -a exp2</I></TT> returns true if <SPANCLASS="emphasis"><ICLASS="EMPHASIS">both</I></SPAN> exp1 and exp2 are true.</P></DD><DT><ANAME="COMPOUNDOR"></A><SPANCLASS="TOKEN">-o</SPAN></DT><DD><P>logical or </P><P><TTCLASS="REPLACEABLE"><I>exp1 -o exp2</I></TT> returns true if either exp1 <SPANCLASS="emphasis"><ICLASS="EMPHASIS">or</I></SPAN> exp2 are true.</P></DD></DL></DIV><P>These are similar to the Bash comparison operators <BCLASS="COMMAND">&&</B> and <BCLASS="COMMAND">||</B>, used within <AHREF="tests.html#DBLBRACKETS">double brackets</A>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 [[ condition1 && condition2 ]]</PRE></TD></TR></TABLE> The <BCLASS="COMMAND">-o</B> and <BCLASS="COMMAND">-a</B> operators work with the <BCLASS="COMMAND">test</B> command or occur within single test brackets. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 if [ "$exp1" -a "$exp2" ]</PRE></TD></TR></TABLE> </P><P>Refer to <AHREF="operations.html#ANDOR">Example 8-3</A>, <AHREF="arrays.html#TWODIM">Example 26-17</A>, and <AHREF="contributed-scripts.html#WHX">Example A-31</A> to see compound comparison operators in action.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN3266"HREF="comparison-ops.html#AEN3266">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>As S.C. points out, in a compound test, even quoting the string variable might not suffice. <TTCLASS="USERINPUT"><B>[ -n "$string" -o "$a" = "$b" ]</B></TT> may cause an error with some versions of Bash if <TTCLASS="VARNAME">$string</TT> is empty. The safe way is to append an extra character to possibly empty variables, <TTCLASS="USERINPUT"><B>[ "x$string" != x -o "x$a" = "x$b" ]</B></TT> (the <SPANCLASS="QUOTE">"x's"</SPAN> cancel out).</P></TD></TR></TABLE><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="fto.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="nestedifthen.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">File test operators</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="tests.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Nested <ICLASS="FIRSTTERM">if/then</I> Condition Tests</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -