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

📄 comparison-ops.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Other Comparison Operators</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Tests"HREF="tests.html"><LINKREL="PREVIOUS"TITLE="File test operators"HREF="fto.html"><LINKREL="NEXT"TITLE="Nested if/then Condition Tests"HREF="nestedifthen.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="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="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="fto.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 7. Tests</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="nestedifthen.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="COMPARISON-OPS">7.3. Other Comparison Operators</A></H1><P>A <ICLASS="EMPHASIS">binary</I> comparison operator compares two	  variables or quantities. Note the separation between integer and	  string comparison.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="ICOMPARISON1"></A>integer comparison</B></P><DL><DT><SPANCLASS="TOKEN">-eq</SPAN></DT><DD><P><ANAME="EQUALREF"></A></P><P>is equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" -eq "$b" ]</B></TT></P></DD><DT><SPANCLASS="TOKEN">-ne</SPAN></DT><DD><P>is not equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" -ne "$b" ]</B></TT></P></DD><DT><SPANCLASS="TOKEN">-gt</SPAN></DT><DD><P>is greater than</P><P><TTCLASS="USERINPUT"><B>if [ "$a" -gt "$b" ]</B></TT></P></DD><DT><SPANCLASS="TOKEN">-ge</SPAN></DT><DD><P>is greater than or equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" -ge "$b" ]</B></TT></P></DD><DT><SPANCLASS="TOKEN">-lt</SPAN></DT><DD><P>is less than</P><P><TTCLASS="USERINPUT"><B>if [ "$a" -lt "$b" ]</B></TT></P></DD><DT><SPANCLASS="TOKEN">-le</SPAN></DT><DD><P>is less than or equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" -le "$b" ]</B></TT></P></DD><DT><ANAME="INTLT"></A><SPANCLASS="TOKEN">&#60;</SPAN></DT><DD><P>is less than (within <AHREF="dblparens.html">double	      parentheses</A>)</P><P><TTCLASS="USERINPUT"><B>(("$a" &#60; "$b"))</B></TT></P></DD><DT><SPANCLASS="TOKEN">&#60;=</SPAN></DT><DD><P>is less than or equal to (within double parentheses)</P><P><TTCLASS="USERINPUT"><B>(("$a" &#60;= "$b"))</B></TT></P></DD><DT><SPANCLASS="TOKEN">&#62;</SPAN></DT><DD><P>is greater than (within double parentheses)</P><P><TTCLASS="USERINPUT"><B>(("$a" &#62; "$b"))</B></TT></P></DD><DT><SPANCLASS="TOKEN">&#62;=</SPAN></DT><DD><P>is greater than or equal to (within double parentheses)</P><P><TTCLASS="USERINPUT"><B>(("$a" &#62;= "$b"))</B></TT></P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="SCOMPARISON1"></A>string comparison</B></P><DL><DT><SPANCLASS="TOKEN">=</SPAN></DT><DD><P><ANAME="EQUALSIGNREF"></A></P><P>is equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" = "$b" ]</B></TT></P></DD><DT><SPANCLASS="TOKEN">==</SPAN></DT><DD><P>is equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" == "$b" ]</B></TT></P><P>This is a synonym for <SPANCLASS="TOKEN">=</SPAN>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>	      The <SPANCLASS="TOKEN">==</SPAN> comparison operator behaves differently	      within a <AHREF="tests.html#DBLBRACKETS">double-brackets</A>	      test than within single brackets.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;[[ $a == z* ]]    # True if $a starts with an "z" (pattern matching).   2&nbsp;[[ $a == "z*" ]]  # True if $a is equal to z* (literal matching).   3&nbsp;   4&nbsp;[ $a == z* ]      # File globbing and word splitting take place.   5&nbsp;[ "$a" == "z*" ]  # True if $a is equal to z* (literal matching).   6&nbsp;   7&nbsp;# Thanks, St閜hane Chazelas</PRE></TD></TR></TABLE>              </P></TD></TR></TABLE></DIV></DD><DT><SPANCLASS="TOKEN">!=</SPAN></DT><DD><P>is not equal to</P><P><TTCLASS="USERINPUT"><B>if [ "$a" != "$b" ]</B></TT></P><P>This operator uses pattern matching within a <AHREF="tests.html#DBLBRACKETS">[[ ... ]]</A> construct.</P></DD><DT><ANAME="LTREF"></A><SPANCLASS="TOKEN">&#60;</SPAN></DT><DD><P>is less than, in ASCII alphabetical order</P><P><TTCLASS="USERINPUT"><B>if [[ "$a" &#60; "$b" ]]</B></TT></P><P><TTCLASS="USERINPUT"><B>if [ "$a" \&#60; "$b" ]</B></TT></P><P>Note that the <SPANCLASS="QUOTE">"&#60;"</SPAN> needs to be	      escaped within a <TTCLASS="USERINPUT"><B>[  ]</B></TT>	      construct.</P></DD><DT><ANAME="GTREF"></A><SPANCLASS="TOKEN">&#62;</SPAN></DT><DD><P>is greater than, in ASCII alphabetical order</P><P><TTCLASS="USERINPUT"><B>if [[ "$a" &#62; "$b" ]]</B></TT></P><P><TTCLASS="USERINPUT"><B>if [ "$a" \&#62; "$b" ]</B></TT></P><P>Note that the <SPANCLASS="QUOTE">"&#62;"</SPAN> needs to be	      escaped within a <TTCLASS="USERINPUT"><B>[  ]</B></TT> construct.</P><P>See <AHREF="arrays.html#BUBBLE">Example 26-11</A> for an application of this	      comparison operator.</P></DD><DT><SPANCLASS="TOKEN">-z</SPAN></DT><DD><P>string is <SPANCLASS="QUOTE">"null"</SPAN>, that is, has zero length</P></DD><DT><SPANCLASS="TOKEN">-n</SPAN></DT><DD><P>string is not <SPANCLASS="QUOTE">"null"</SPAN>.</P

⌨️ 快捷键说明

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