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

📄 comparison-ops.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
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&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;a=4   4&nbsp;b=5   5&nbsp;   6&nbsp;#  Here "a" and "b" can be treated either as integers or strings.   7&nbsp;#  There is some blurring between the arithmetic and string comparisons,   8&nbsp;#+ since Bash variables are not strongly typed.   9&nbsp;  10&nbsp;#  Bash permits integer operations and comparisons on variables  11&nbsp;#+ whose value consists of all-integer characters.  12&nbsp;#  Caution advised, however.  13&nbsp;  14&nbsp;echo  15&nbsp;  16&nbsp;if [ "$a" -ne "$b" ]  17&nbsp;then  18&nbsp;  echo "$a is not equal to $b"  19&nbsp;  echo "(arithmetic comparison)"  20&nbsp;fi  21&nbsp;  22&nbsp;echo  23&nbsp;  24&nbsp;if [ "$a" != "$b" ]  25&nbsp;then  26&nbsp;  echo "$a is not equal to $b."  27&nbsp;  echo "(string comparison)"  28&nbsp;  #     "4"  != "5"  29&nbsp;  # ASCII 52 != ASCII 53  30&nbsp;fi  31&nbsp;  32&nbsp;# In this particular instance, both "-ne" and "!=" work.  33&nbsp;  34&nbsp;echo  35&nbsp;  36&nbsp;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&nbsp;#!/bin/bash   2&nbsp;#  str-test.sh: Testing null strings and unquoted strings,   3&nbsp;#+ but not strings and sealing wax, not to mention cabbages and kings . . .   4&nbsp;   5&nbsp;# Using   if [ ... ]   6&nbsp;   7&nbsp;   8&nbsp;# If a string has not been initialized, it has no defined value.   9&nbsp;# This state is called "null" (not the same as zero).  10&nbsp;  11&nbsp;if [ -n $string1 ]    # $string1 has not been declared or initialized.  12&nbsp;then  13&nbsp;  echo "String \"string1\" is not null."  14&nbsp;else    15&nbsp;  echo "String \"string1\" is null."  16&nbsp;fi    17&nbsp;# Wrong result.  18&nbsp;# Shows $string1 as not null, although it was not initialized.  19&nbsp;  20&nbsp;  21&nbsp;echo  22&nbsp;  23&nbsp;  24&nbsp;# Lets try it again.  25&nbsp;  26&nbsp;if [ -n "$string1" ]  # This time, $string1 is quoted.  27&nbsp;then  28&nbsp;  echo "String \"string1\" is not null."  29&nbsp;else    30&nbsp;  echo "String \"string1\" is null."  31&nbsp;fi                    # Quote strings within test brackets!  32&nbsp;  33&nbsp;  34&nbsp;echo  35&nbsp;  36&nbsp;  37&nbsp;if [ $string1 ]       # This time, $string1 stands naked.  38&nbsp;then  39&nbsp;  echo "String \"string1\" is not null."  40&nbsp;else    41&nbsp;  echo "String \"string1\" is null."  42&nbsp;fi    43&nbsp;# This works fine.  44&nbsp;# The [ ] test operator alone detects whether the string is null.  45&nbsp;# However it is good practice to quote it ("$string1").  46&nbsp;#  47&nbsp;# As Stephane Chazelas points out,  48&nbsp;#    if [ $string1 ]    has one argument, "]"  49&nbsp;#    if [ "$string1" ]  has two arguments, the empty "$string1" and "]"   50&nbsp;  51&nbsp;  52&nbsp;  53&nbsp;echo  54&nbsp;  55&nbsp;  56&nbsp;  57&nbsp;string1=initialized  58&nbsp;  59&nbsp;if [ $string1 ]       # Again, $string1 stands naked.  60&nbsp;then  61&nbsp;  echo "String \"string1\" is not null."  62&nbsp;else    63&nbsp;  echo "String \"string1\" is null."  64&nbsp;fi    65&nbsp;# Again, gives correct result.  66&nbsp;# Still, it is better to quote it ("$string1"), because . . .  67&nbsp;  68&nbsp;  69&nbsp;string1="a = b"  70&nbsp;  71&nbsp;if [ $string1 ]       # Again, $string1 stands naked.  72&nbsp;then  73&nbsp;  echo "String \"string1\" is not null."  74&nbsp;else    75&nbsp;  echo "String \"string1\" is null."  76&nbsp;fi    77&nbsp;# Not quoting "$string1" now gives wrong result!  78&nbsp;  79&nbsp;exit 0  80&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# zmore   3&nbsp;   4&nbsp;#View gzipped files with 'more'   5&nbsp;   6&nbsp;NOARGS=65   7&nbsp;NOTFOUND=66   8&nbsp;NOTGZIP=67   9&nbsp;  10&nbsp;if [ $# -eq 0 ] # same effect as:  if [ -z "$1" ]  11&nbsp;# $1 can exist, but be empty:  zmore "" arg2 arg3  12&nbsp;then  13&nbsp;  echo "Usage: `basename $0` filename" &#62;&#38;2  14&nbsp;  # Error message to stderr.  15&nbsp;  exit $NOARGS  16&nbsp;  # Returns 65 as exit status of script (error code).  17&nbsp;fi    18&nbsp;  19&nbsp;filename=$1  20&nbsp;  21&nbsp;if [ ! -f "$filename" ]   # Quoting $filename allows for possible spaces.  22&nbsp;then  23&nbsp;  echo "File $filename not found!" &#62;&#38;2  24&nbsp;  # Error message to stderr.  25&nbsp;  exit $NOTFOUND  26&nbsp;fi    27&nbsp;  28&nbsp;if [ ${filename##*.} != "gz" ]  29&nbsp;# Using bracket in variable substitution.  30&nbsp;then  31&nbsp;  echo "File $1 is not a gzipped file!"  32&nbsp;  exit $NOTGZIP  33&nbsp;fi    34&nbsp;  35&nbsp;zcat $1 | more  36&nbsp;  37&nbsp;# Uses the filter 'more.'  38&nbsp;# May substitute 'less', if desired.  39&nbsp;  40&nbsp;  41&nbsp;exit $?   # Script returns exit status of pipe.  42&nbsp;# Actually "exit $?" is unnecessary, as the script will, in any case,  43&nbsp;# 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">&#38;&#38;</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&nbsp;[[ condition1 &#38;&#38; 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&nbsp;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 + -