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

📄 operations.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
>As of version 2.05b, Bash supports 64-bit integers.</P></TD></TR></TABLE></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><ANAME="NOFLOATINGPOINT"></A></P><P>Bash does not understand floating point arithmetic. It        treats numbers containing a decimal point as strings.</P><P>        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;a=1.5   2&nbsp;   3&nbsp;let "b = $a + 1.3"  # Error.   4&nbsp;# t2.sh: let: b = 1.5 + 1.3: syntax error in expression   5&nbsp;#                            (error token is ".5 + 1.3")   6&nbsp;   7&nbsp;echo "b = $b"       # b=1</PRE></TD></TR></TABLE>      </P><P>Use <AHREF="mathc.html#BCREF">bc</A> in scripts that that need floating	point calculations or math library functions.</P></TD></TR></TABLE></DIV><DIVCLASS="FORMALPARA"><P><B>bitwise operators. </B>The bitwise operators seldom make an appearance in shell scripts.	Their chief use seems to be manipulating and testing values read	from ports or <AHREF="devproc.html#SOCKETREF">sockets</A>. <SPANCLASS="QUOTE">"Bit	flipping"</SPAN> is more relevant to compiled languages, such	as C and C++, which provide direct access to system	hardware.</P></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="BITWSOPS1"></A>bitwise operators</B></P><DL><DT><SPANCLASS="TOKEN">&#60;&#60;</SPAN></DT><DD><P>bitwise left shift (multiplies by <TTCLASS="LITERAL">2</TT>	      for each shift position)</P></DD><DT><SPANCLASS="TOKEN">&#60;&#60;=</SPAN></DT><DD><P><ICLASS="FIRSTTERM">left-shift-equal</I></P><P><TTCLASS="USERINPUT"><B>let "var &#60;&#60;= 2"</B></TT> results in <TTCLASS="PARAMETER"><I>var</I></TT>	    left-shifted <TTCLASS="LITERAL">2</TT> bits (multiplied by <TTCLASS="LITERAL">4</TT>)</P></DD><DT><SPANCLASS="TOKEN">&#62;&#62;</SPAN></DT><DD><P>bitwise right shift (divides by <TTCLASS="LITERAL">2</TT>	      for each shift position)</P></DD><DT><SPANCLASS="TOKEN">&#62;&#62;=</SPAN></DT><DD><P><ICLASS="FIRSTTERM">right-shift-equal</I>	    (inverse of <SPANCLASS="TOKEN">&#60;&#60;=</SPAN>)</P></DD><DT><SPANCLASS="TOKEN">&#38;</SPAN></DT><DD><P>bitwise AND</P></DD><DT><SPANCLASS="TOKEN">&#38;=</SPAN></DT><DD><P>bitwise <ICLASS="FIRSTTERM">AND-equal</I></P></DD><DT><SPANCLASS="TOKEN">|</SPAN></DT><DD><P>bitwise OR</P></DD><DT><SPANCLASS="TOKEN">|=</SPAN></DT><DD><P>bitwise <ICLASS="FIRSTTERM">OR-equal</I></P></DD><DT><SPANCLASS="TOKEN">~</SPAN></DT><DD><P>bitwise NOT</P></DD><DT><SPANCLASS="TOKEN">^</SPAN></DT><DD><P>bitwise XOR</P></DD><DT><SPANCLASS="TOKEN">^=</SPAN></DT><DD><P>bitwise <ICLASS="FIRSTTERM">XOR-equal</I></P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="LOGOPS1"></A>logical (boolean) operators</B></P><DL><DT><SPANCLASS="TOKEN">!</SPAN></DT><DD><P>NOT</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ ! -f $FILENAME ]   2&nbsp;then   3&nbsp;  ...</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">&#38;&#38;</SPAN></DT><DD><P>AND</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ $condition1 ] &#38;&#38; [ $condition2 ]   2&nbsp;# Same as:  if [ $condition1 -a $condition2 ]   3&nbsp;# Returns true if both condition1 and condition2 hold true...   4&nbsp;   5&nbsp;if [[ $condition1 &#38;&#38; $condition2 ]]    # Also works.   6&nbsp;# Note that &#38;&#38; operator not permitted within [ ... ] construct.</PRE></TD></TR></TABLE></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><SPANCLASS="TOKEN">&#38;&#38;</SPAN> may also, depending on context, be	    used in an <AHREF="list-cons.html#LISTCONSREF">and list</A>	    to concatenate commands.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="ORREF"></A><SPANCLASS="TOKEN">||</SPAN></DT><DD><P>OR</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ $condition1 ] || [ $condition2 ]   2&nbsp;# Same as:  if [ $condition1 -o $condition2 ]   3&nbsp;# Returns true if either condition1 or condition2 holds true...   4&nbsp;   5&nbsp;if [[ $condition1 || $condition2 ]]    # Also works.   6&nbsp;# Note that || operator not permitted within [ ... ] construct.</PRE></TD></TR></TABLE></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>Bash tests the <AHREF="exit-status.html#EXITSTATUSREF">exit	    status</A> of each statement linked with a logical	    operator.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ANDOR"></A><P><B>Example 8-3. Compound Condition Tests Using &#38;&#38; and ||</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;a=24   4&nbsp;b=47   5&nbsp;   6&nbsp;if [ "$a" -eq 24 ] &#38;&#38; [ "$b" -eq 47 ]   7&nbsp;then   8&nbsp;  echo "Test #1 succeeds."   9&nbsp;else  10&nbsp;  echo "Test #1 fails."  11&nbsp;fi  12&nbsp;  13&nbsp;# ERROR:   if [ "$a" -eq 24 &#38;&#38; "$b" -eq 47 ]  14&nbsp;#+         attempts to execute  ' [ "$a" -eq 24 '  15&nbsp;#+         and fails to finding matching ']'.  16&nbsp;#  17&nbsp;#  Note:  if [[ $a -eq 24 &#38;&#38; $b -eq 24 ]]  works.  18&nbsp;#  The double-bracket if-test is more flexible  19&nbsp;#+ than the single-bracket version.         20&nbsp;#    (The "&#38;&#38;" has a different meaning in line 17 than in line 6.)  21&nbsp;#    Thanks, Stephane Chazelas, for pointing this out.  22&nbsp;  23&nbsp;  24&nbsp;if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]  25&nbsp;then  26&nbsp;  echo "Test #2 succeeds."  27&nbsp;else  28&nbsp;  echo "Test #2 fails."  29&nbsp;fi  30&nbsp;  31&nbsp;  32&nbsp;#  The -a and -o options provide  33&nbsp;#+ an alternative compound condition test.  34&nbsp;#  Thanks to Patrick Callahan for pointing this out.  35&nbsp;  36&nbsp;  37&nbsp;if [ "$a" -eq 24 -a "$b" -eq 47 ]  38&nbsp;then  39&nbsp;  echo "Test #3 succeeds."  40&nbsp;else  41&nbsp;  echo "Test #3 fails."  42&nbsp;fi  43&nbsp;  44&nbsp;  45&nbsp;if [ "$a" -eq 98 -o "$b" -eq 47 ]  46&nbsp;then  47&nbsp;  echo "Test #4 succeeds."  48&nbsp;else  49&nbsp;  echo "Test #4 fails."  50&nbsp;fi  51&nbsp;  52&nbsp;  53&nbsp;a=rhino  54&nbsp;b=crocodile  55&nbsp;if [ "$a" = rhino ] &#38;&#38; [ "$b" = crocodile ]  56&nbsp;then  57&nbsp;  echo "Test #5 succeeds."  58&nbsp;else  59&nbsp;  echo "Test #5 fails."  60&nbsp;fi  61&nbsp;  62&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <SPANCLASS="TOKEN">&#38;&#38;</SPAN> and <SPANCLASS="TOKEN">||</SPAN> operators also	      find use in an arithmetic context.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $(( 1 &#38;&#38; 2 )) $((3 &#38;&#38; 0)) $((4 || 0)) $((0 || 0))</B></TT> <TTCLASS="COMPUTEROUTPUT">1 0 1 0</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="MISCOP1"></A>miscellaneous operators</B></P><DL><DT><ANAME="COMMAOP"></A><SPANCLASS="TOKEN">,</SPAN></DT><DD><P>Comma operator</P><P>The <BCLASS="COMMAND">comma operator</B> chains together	      two or more arithmetic operations. All the operations are	      evaluated (with possible <ICLASS="FIRSTTERM">side effects</I>	        <ANAME="AEN3825"HREF="#FTN.AEN3825">[1]</A>	      ), but only the last operation is returned.</P><P>	     <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;let "t1 = ((5 + 3, 7 - 1, 15 - 4))"   2&nbsp;echo "t1 = $t1"               # t1 = 11   3&nbsp;   4&nbsp;let "t2 = ((a = 9, 15 / 3))"  # Set "a" and calculate "t2".   5&nbsp;echo "t2 = $t2    a = $a"     # t2 = 5    a = 9</PRE></TD></TR></TABLE>	     </P><P>The comma operator finds use mainly in <AHREF="loops.html#FORLOOPREF1">for loops</A>. See <AHREF="loops.html#FORLOOPC">Example 10-12</A>.</P></DD></DL></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN3825"HREF="operations.html#AEN3825">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ICLASS="FIRSTTERM">Side effects</I>		are, of course, unintended and usually undesirable		consequences.</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="testtest.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="numerical-constants.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Testing Your Knowledge of Tests</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Numerical Constants</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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