259 lines
6.8 KiB
HTML
259 lines
6.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<META name="generator" content="HTML Tidy, see www.w3.org">
|
|
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<META http-equiv="Content-Style-Type" content="text/css">
|
|
<title>Arithmetic expansions</title>
|
|
<LINK rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
</HEAD>
|
|
<body bgcolor="#FFFFFF">
|
|
<!-- Java sync-link -->
|
|
<SCRIPT LANGUAGE="Javascript" SRC="../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
|
|
|
|
|
|
|
<h2>Arithmetic expansions</h2>
|
|
|
|
<p>Arithmetic expansion provides a mechanism for evaluating an
|
|
arithmetic expression and substituting its value. The format for
|
|
arithmetic expansion is:</p>
|
|
|
|
<p>$((<em>expression</em>))</p>
|
|
|
|
<p>The <em>expression</em> is treated as if it were in
|
|
double quotation marks, except that a double quotation mark inside <em>
|
|
expression</em> is not treated specially. The shell expands all
|
|
tokens in <em>expression</em> for parameter expansion, command
|
|
substitution, and quote removal. <strong>qsh</strong> treats the
|
|
result as an arithmetic expression and
|
|
substitutes the value of the expression.</p>
|
|
|
|
<p><strong>Arithmetic expressions</strong></p>
|
|
|
|
<p>An arithmetic expression can be specified in the following
|
|
situations:</p>
|
|
|
|
<ul>
|
|
<li>in an arithmetic expansion</li>
|
|
|
|
<li>for each argument of the <a href="let.htm">let</a> utility</li>
|
|
|
|
<li>for the argument of the <a href="shift.htm">shift</a>
|
|
utility</li>
|
|
|
|
<li>for the operands of the arithmetic formats of the <a href=
|
|
"printf.htm">printf</a> utility</li>
|
|
|
|
<li>for the operands to the arithmetic comparison operators of the
|
|
<a href="test.htm">test</a> utility</li>
|
|
|
|
<li>for the argument of the
|
|
<a href="ulimit.htm">ulimit</a> utility</li>
|
|
|
|
<li>in the "Substring Starting at Offset" parameter expansion
|
|
</li>
|
|
</ul>
|
|
|
|
<p><strong>qsh</strong> performs either integer or floating point
|
|
arithmetic based on the setting of the float option. When the float
|
|
option is set on, <strong>qsh</strong> performs floating point
|
|
arithmetic.</p>
|
|
|
|
<p>An integer number has the format [<em>base</em>#]<em>number</em>
|
|
where:</p>
|
|
|
|
<ul>
|
|
<li><em>base</em> is a decimal integer between 2 and 36 that
|
|
specifies the arithmetic base. The default is base 10.</li>
|
|
|
|
<li><em>number</em> is a non-negative number. For a base greater
|
|
than 10, numbers greater than 9 or represented using a letter of
|
|
the alphabet. For example, when using base 16, the decimal number
|
|
10 is represented using A.</li>
|
|
</ul>
|
|
|
|
<p>A floating point number has the format [<em>+|-</em>] <em>
|
|
number</em>[.<em>number</em>] [<em>exponent</em>] where:</p>
|
|
|
|
<ul>
|
|
<li><em>number</em> is a non-negative decimal number.</li>
|
|
|
|
<li><em>exponent</em> is E or e followed by + or - and a
|
|
non-negative decimal number.</li>
|
|
</ul>
|
|
|
|
<p>Arithmetic expressions use the following ANSI C language
|
|
operators and precedence.</p>
|
|
|
|
<dl>
|
|
<dt>(<em>expression</em>)</dt>
|
|
|
|
<dd>Parenthesis overrides precedence rules</dd>
|
|
|
|
<dt>Unary operators</dt>
|
|
|
|
<dd>+<em>expression</em> Unary +</dd>
|
|
|
|
<dd>-<em>expression</em> Unary -</dd>
|
|
|
|
<dd>~<em>expression</em> Bitwise negation</dd>
|
|
|
|
<dd>!<em>expression</em> Logical negation</dd>
|
|
|
|
<dt>Multiplicative operators</dt>
|
|
|
|
<dd><em>expression</em> * <em>expression</em> Multiplication</dd>
|
|
|
|
<dd><em>expression</em> / <em>expression</em> Division</dd>
|
|
|
|
<dd><em>expression</em> % <em>expression</em> Remainder</dd>
|
|
|
|
<dt>Additive operators</dt>
|
|
|
|
<dd><em>expression</em> + <em>expression</em> Addition</dd>
|
|
|
|
<dd><em>expression</em> - <em>expression</em> Subtraction</dd>
|
|
|
|
<dt>Bitwise shift operators</dt>
|
|
|
|
<dd><em>expression</em> << <em>expression</em> Left shift the
|
|
first expression by the number of bits given in the second
|
|
expression</dd>
|
|
|
|
<dd><em>expression</em> >> <em>expression</em> Right shift
|
|
the first expression by the number of bits given in the second
|
|
expression</dd>
|
|
|
|
<dt>Relational operators</dt>
|
|
|
|
<dd><em>expression</em> < <em>expression</em> Less than</dd>
|
|
|
|
<dd><em>expression</em> <= <em>expression</em> Less than or
|
|
equal to</dd>
|
|
|
|
<dd><em>expression</em> > <em>expression</em> Greater than</dd>
|
|
|
|
<dd><em>expression</em> >= <em>expression</em> Greater than or
|
|
equal to</dd>
|
|
|
|
<dt>Bitwise AND operator</dt>
|
|
|
|
<dd><em>expression</em> & <em>expression</em> Bitwise and where
|
|
the result contains a 1 in each bit position where there is a 1 in
|
|
both expressions and a 0 in all other bit positions.</dd>
|
|
|
|
<dt>Bitwise Exclusive OR operator</dt>
|
|
|
|
<dd><em>expression</em> ^ <em>expression</em> Bitwise exclusive or
|
|
where the result contains a 1 in each bit position where there is a
|
|
1 in only one of the expressions and a 0 in all other bit
|
|
positions.</dd>
|
|
|
|
<dt>Bitwise OR operator</dt>
|
|
|
|
<dd><em>expression</em> | <em>expression</em> Bitwise or where the
|
|
result contains a 1 in each bit position where there is a 1 in
|
|
either expression and a 0 in all other bit positions.</dd>
|
|
|
|
<dt>Logical AND operator</dt>
|
|
|
|
<dd><em>expression</em> && <em>expression</em> Logical and
|
|
where the result is true if both expressions are true</dd>
|
|
|
|
<dt>Logical OR operator</dt>
|
|
|
|
<dd><em>expression</em> || <em>expression</em> Logical or where the
|
|
result is true if one of the expressions is true</dd>
|
|
|
|
<dt>Conditional operator</dt>
|
|
|
|
<dd><em>expression</em> ? <em>expression</em> : <em>expression</em>
|
|
Conditional operator where when the first expression is true, the
|
|
second expression is evaluated. Otherwise the third expression is
|
|
evaluated.</dd>
|
|
|
|
<dt>Assignment operators</dt>
|
|
|
|
<dd><em>expression</em> = <em>expression</em> Simple
|
|
assignment</dd>
|
|
|
|
<dd><em>expression</em> *= <em>expression</em> Assign and
|
|
multiply</dd>
|
|
|
|
<dd><em>expression</em> /= <em>expression</em> Assign and
|
|
divide</dd>
|
|
|
|
<dd><em>expression</em> %= <em>expression</em> Assign and
|
|
remainder</dd>
|
|
|
|
<dd><em>expression</em> += <em>expression</em> Assign and add</dd>
|
|
|
|
<dd><em>expression</em> -= <em>expression</em> Assign and
|
|
subtract</dd>
|
|
|
|
<dd><em>expression</em> <<= <em>expression</em> Assign and
|
|
shift left</dd>
|
|
|
|
<dd><em>expression</em> >>= <em>expression</em> Assign and
|
|
shift right</dd>
|
|
|
|
<dd><em>expression</em> &= <em>expression</em> Assign and
|
|
bitwise AND</dd>
|
|
|
|
<dd><em>expression</em> ^= <em>expression</em> Assign and bitwise
|
|
exclusive OR</dd>
|
|
|
|
<dd><em>expression</em> |= <em>expression</em> Assign and bitwise
|
|
OR</dd>
|
|
</dl>
|
|
|
|
<table>
|
|
<tr>
|
|
<td valign="top"><strong>Note:</strong></td>
|
|
<td>When using floating point arithmetic the remainder, left shift,
|
|
right shift, bitwise AND, bitwise exclusive OR, and bitwise OR
|
|
operators are not supported.</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p><strong>Examples</strong></p>
|
|
|
|
<ol>
|
|
<li>Add two decimal numbers:
|
|
<pre>
|
|
echo $((2+2))
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Add two hexadecimal numbers:
|
|
<pre>
|
|
echo $((16#A + 16#20))
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Increment the variable index by one:
|
|
<pre>
|
|
let index+=1
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Evaluate a complex expression:
|
|
<pre>
|
|
echo $((5+9-2*3/2))
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Add two floating point numbers:
|
|
<pre>
|
|
set -F
|
|
echo $((5.75+9.157))
|
|
set +F
|
|
</pre>
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
</body>
|
|
</html>
|