📄 operations.html
字号:
>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 a=1.5 2 3 let "b = $a + 1.3" # Error. 4 # t2.sh: let: b = 1.5 + 1.3: syntax error in expression 5 # (error token is ".5 + 1.3") 6 7 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"><<</SPAN></DT><DD><P>bitwise left shift (multiplies by <TTCLASS="LITERAL">2</TT> for each shift position)</P></DD><DT><SPANCLASS="TOKEN"><<=</SPAN></DT><DD><P><ICLASS="FIRSTTERM">left-shift-equal</I></P><P><TTCLASS="USERINPUT"><B>let "var <<= 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">>></SPAN></DT><DD><P>bitwise right shift (divides by <TTCLASS="LITERAL">2</TT> for each shift position)</P></DD><DT><SPANCLASS="TOKEN">>>=</SPAN></DT><DD><P><ICLASS="FIRSTTERM">right-shift-equal</I> (inverse of <SPANCLASS="TOKEN"><<=</SPAN>)</P></DD><DT><SPANCLASS="TOKEN">&</SPAN></DT><DD><P>bitwise AND</P></DD><DT><SPANCLASS="TOKEN">&=</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 if [ ! -f $FILENAME ] 2 then 3 ...</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">&&</SPAN></DT><DD><P>AND</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 if [ $condition1 ] && [ $condition2 ] 2 # Same as: if [ $condition1 -a $condition2 ] 3 # Returns true if both condition1 and condition2 hold true... 4 5 if [[ $condition1 && $condition2 ]] # Also works. 6 # 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><SPANCLASS="TOKEN">&&</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 if [ $condition1 ] || [ $condition2 ] 2 # Same as: if [ $condition1 -o $condition2 ] 3 # Returns true if either condition1 or condition2 holds true... 4 5 if [[ $condition1 || $condition2 ]] # Also works. 6 # 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 && and ||</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 a=24 4 b=47 5 6 if [ "$a" -eq 24 ] && [ "$b" -eq 47 ] 7 then 8 echo "Test #1 succeeds." 9 else 10 echo "Test #1 fails." 11 fi 12 13 # ERROR: if [ "$a" -eq 24 && "$b" -eq 47 ] 14 #+ attempts to execute ' [ "$a" -eq 24 ' 15 #+ and fails to finding matching ']'. 16 # 17 # Note: if [[ $a -eq 24 && $b -eq 24 ]] works. 18 # The double-bracket if-test is more flexible 19 #+ than the single-bracket version. 20 # (The "&&" has a different meaning in line 17 than in line 6.) 21 # Thanks, Stephane Chazelas, for pointing this out. 22 23 24 if [ "$a" -eq 98 ] || [ "$b" -eq 47 ] 25 then 26 echo "Test #2 succeeds." 27 else 28 echo "Test #2 fails." 29 fi 30 31 32 # The -a and -o options provide 33 #+ an alternative compound condition test. 34 # Thanks to Patrick Callahan for pointing this out. 35 36 37 if [ "$a" -eq 24 -a "$b" -eq 47 ] 38 then 39 echo "Test #3 succeeds." 40 else 41 echo "Test #3 fails." 42 fi 43 44 45 if [ "$a" -eq 98 -o "$b" -eq 47 ] 46 then 47 echo "Test #4 succeeds." 48 else 49 echo "Test #4 fails." 50 fi 51 52 53 a=rhino 54 b=crocodile 55 if [ "$a" = rhino ] && [ "$b" = crocodile ] 56 then 57 echo "Test #5 succeeds." 58 else 59 echo "Test #5 fails." 60 fi 61 62 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <SPANCLASS="TOKEN">&&</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 && 2 )) $((3 && 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 let "t1 = ((5 + 3, 7 - 1, 15 - 4))" 2 echo "t1 = $t1" # t1 = 11 3 4 let "t2 = ((a = 9, 15 / 3))" # Set "a" and calculate "t2". 5 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 + -