Chapter 8. Operations and Related Topics
8.1. Operators
1、Example 8-1. Greatest common divisor最大公约数的算法不懂,里面的练习:判断命令行参数是否是整数不懂,记在这里。
2、发现一个问题,bash的算术操作符只能进行整数的运算,要进行其它的复杂点的运算:如开方、小数等怎么办呢?
3、Example 8-2. Using Arithmetic Operations
这个例子有不少地方不明白,记在这里以后进一步解决:
我知道 let "z = 2 + 3" 相当于 (( z = 2 + 3 )),根据:
Similar to the let command, the ((...)) construct permits arithmetic expansion
and evaluation.
|
|
1)、
10 : $((n = $n + 1))
11 # ":" necessary because otherwise Bash attempts
12 #+ to interpret "$((n = $n + 1))" as a command.
|
|
2)、 20 n=$(($n + 1))
3)、
23 : $[ n = $n + 1 ]
24 # ":" necessary because otherwise Bash attempts
25 #+ to interpret "$[ n = $n + 1 ]" as a command.
26 # Works even if "n" was initialized as a string.
4)、
29 n=$[ $n + 1 ]
30 # Works even if "n" was initialized as a string.
31 #* Avoid this type of construct, since it is obsolete and
nonportable.
32 # Thanks, Stephane Chazelas.
5)、 44 : $(( n++ )) # : $(( ++n )) also works.
6)、 47 : $[ n++ ] # : $[ ++n ]] also works
4、bash不能处理浮点数:
Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as strings.
|
|
5、
if [ $condition1 -a $condition2 ] 相当于 if [[ $condition1 && $condition2 ]]
,但是&&不能用在[...]结构中,这些有什么规律吗?真不好掌握啊。
6、Example 8-3. Compound Condition Tests Using && and ||
6 if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
17 # Note: if [[ $a -eq 24 && $b -eq 24 ]] works.
20 # (The "&&" has a different meaning in line 17 than in line 6.)
|
|
我不懂&&在6行和17行有什么区别?
7、不懂 $((...))的用法
bash$ echo $(( 1 && 2 )) $((3 && 0)) $((4 || 0)) $((0 || 0))
1 0 1 0
|
|
英语疑问:
1、Modulo turns up surprisingly often in various numerical recipes.
2、erroneous
An operation that takes a variable outside these limits will give an erroneous result.
3、flip, relevant, fly
"Bit flipping" is more relevant to compiled languages, such
as C and C++, which run fast enough to permit its use on the fly.
4、miscellaneous operators
5、evaluated
All the operations are evaluated (with possible side effects), but only the last operation is returned.