2008-06-26
在BASH中进行数学运算
作者:花开 发布时间: 2008-06-26 | 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处、作者信息及本声名。
本文链接: http://www.bsdmap.com/2008/06/26/%e5%9c%a8bash%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e5%ad%a6%e8%bf%90%e7%ae%97/
以前只知道expr,今天发现了BASH内置的let:
let: let arg [arg ...]
Each ARG is an arithmetic expression to be evaluated. Evaluation
is done in fixed-width integers with no check for overflow, though
division by 0 is trapped and flagged as an error. The following
list of operators is grouped into levels of equal-precedence operators.
The levels are listed in order of decreasing precedence.
id++, id– variable post-increment, post-decrement
++id, –id variable pre-increment, pre-decrement
-, + unary minus, plus
!, ~ logical and bitwise negation
** exponentiation
*, /, % multiplication, division, remainder
+, - addition, subtraction
<<, >> left and right bitwise shifts
<=, >=, <, > comparison
==, != equality, inequality
& bitwise AND
^ bitwise XOR
| bitwise OR
&& logical AND
|| logical OR
expr ? expr : expr
conditional operator
=, *=, /=, %=,
+=, -=, <<=, >>=,
&=, ^=, |= assignment
Shell variables are allowed as operands. The name of the variable
is replaced by its value (coerced to a fixed-width integer) within
an expression. The variable need not have its integer attribute
turned on to be used in an expression.
Operators are evaluated in order of precedence. Sub-expressions in
parentheses are evaluated first and may override the precedence
rules above.
If the last ARG evaluates to 0, let returns 1; 0 is returned
otherwise.
注:是“脚本”,不是“角本”。
2008-07-02
今天又有新的发现:
(( … )): (( expression ))
The EXPRESSION is evaluated according to the rules for arithmetic
evaluation. Equivalent to “let EXPRESSION”.
$a=1
$(( a++ ))
$echo $a
2
On this day..
- 对软链接的认识(bash中) - 2008