Just learned the shell don't know how to convert, now understand a little, record under
It's better to be an addition.
c=$[$c+5]
let c=$c+5 变量c等于C加上5后在赋值给自身
let c+=5 就可以这样表示了
例如:
脚本代码如下
declare -i SUM=0
for ((J=2;J<=100;J+=2));do
let SUM+=$J
echo $SUM
done
echo $SUM
执行结果如下:
+ declare -i SUM=0
+ (( J=2 )) #首先赋值J=2
+ (( J<=100 )) #J小于100就继续执行
+ let SUM+=2 #计算SUM的值,SUM+=变量J,J此时等于2,变量SUM等于SUM加上2后在赋值给自身
+ echo 2
2 #此时SUM等于2
+ (( J+=2 )) #现在J=2了,然后J+2后在往自身赋值,就变成4了
+ (( J<=100 )) #J小于100就继续执行
+ let SUM+=4 #SUM经过上轮计算现在等于2,SUM+4后在往自身赋值就变成6了
+ echo 6
6 #此时SUM等于6
+ (( J+=2 )) #现在J=4了,然后J+2后在往自身赋值,就变成6了
+ (( J<=100 )) #J小于100就继续执行
+ let SUM+=6 #SUM经过上轮计算现在等于6,SUM+6后在往自身赋值就变成12了
+ echo 12
12 #最终显示12
Same in +-*/% are common
About the + + value problem
代码如下
declare -i SUM=0
for ((J=2;J<=100;J++));do
let SUM+=$J
echo $SUM
done
echo $SUM
事例如下
+ declare -i SUM=0
+ (( J=2 ))
#首先赋值J =2
+ (( J<=100 ))
#J小于100就继续执行
+ let SUM+=2
#计算SUM的值, the sum+= variable j,j is equal to 2 at this time, the variable sum equals sum plus 2 is assigned to itself
+ echo 2
2
#此时SUM等于2
+ (( J++ ))
#现在J = 2, and then J + + in the self-assignment, that is, adding 1 to itself, it becomes 3.
+ (( J<=100 ))
#J小于100就继续执行
+ let SUM+=3
#SUM经过上轮计算现在等于2, after sum+3, the assignment to itself becomes 5.
+ echo 5
5
#此时SUM等于5
+ (( J++ ))
#现在J = 3, and then J + + in the self-assignment, that is, adding 1 to itself, it becomes 4.
+ (( J<=100 ))
#J小于100就继续执行
+ let SUM+=4
#SUM经过上轮计算现在等于2, after sum+4, the assignment to itself becomes 9.
+ echo 9
9
#此时SUM等于5
+ (( J++ ))
#现在J = 4, and then J + + in the self-assignment, that is, adding 1 to itself, it becomes 5.
+ (( J<=100 ))
#J小于100就继续执行
+ let SUM+=5
#SUM经过上轮计算现在等于9, after sum+5, the assignment to itself becomes 14.
+ echo 14
14
#此时SUM等于14
This method can only be applied to--the rest cannot be executed.
From for notes (Wiz)
c=$[$c%5] to let c= $c%5 conversion