c=$[$c%5] to let c= $c%5 conversion

Source: Internet
Author: User

Just learned the shell don't know how to convert, now understand a little, record under
It's better to be an addition.

  
 
  1. c=$[$c+5]
  2. let c=$c+5 变量c等于C加上5后在赋值给自身
  3. let c+=5 就可以这样表示了
  4. 例如:
  5. 脚本代码如下
  6. declare -i SUM=0
  7. for ((J=2;J<=100;J+=2));do
  8. let SUM+=$J
  9. echo $SUM
  10. done
  11. echo $SUM
  12. 执行结果如下:
  13. + declare -i SUM=0
  14. + (( J=2 )) #首先赋值J=2
  15. + (( J<=100 )) #J小于100就继续执行
  16. + let SUM+=2 #计算SUM的值,SUM+=变量J,J此时等于2,变量SUM等于SUM加上2后在赋值给自身
  17. + echo 2
  18. 2 #此时SUM等于2
  19. + (( J+=2 )) #现在J=2了,然后J+2后在往自身赋值,就变成4了
  20. + (( J<=100 )) #J小于100就继续执行
  21. + let SUM+=4 #SUM经过上轮计算现在等于2,SUM+4后在往自身赋值就变成6了
  22. + echo 6
  23. 6 #此时SUM等于6
  24. + (( J+=2 )) #现在J=4了,然后J+2后在往自身赋值,就变成6了
  25. + (( J<=100 )) #J小于100就继续执行
  26. + let SUM+=6 #SUM经过上轮计算现在等于6,SUM+6后在往自身赋值就变成12了
  27. + echo 12
  28. 12 #最终显示12
Same in +-*/% are common
About the + + value problem
  
 
  1. 代码如下
  2. declare -i SUM=0
  3. for ((J=2;J<=100;J++));do
  4. let SUM+=$J
  5. echo $SUM
  6. done
  7. echo $SUM
  8. 事例如下
  9. + declare -i SUM=0
  10. + (( J=2 )) #首先赋值J =2
  11. + (( J<=100 )) #J小于100就继续执行
  12. + 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
  13. + echo 2
  14. 2 #此时SUM等于2
  15. + (( J++ )) #现在J = 2, and then J + + in the self-assignment, that is, adding 1 to itself, it becomes 3.
  16. + (( J<=100 )) #J小于100就继续执行
  17. + let SUM+=3 #SUM经过上轮计算现在等于2, after sum+3, the assignment to itself becomes 5.
  18. + echo 5
  19. 5 #此时SUM等于5
  20. + (( J++ )) #现在J = 3, and then J + + in the self-assignment, that is, adding 1 to itself, it becomes 4.
  21. + (( J<=100 )) #J小于100就继续执行
  22. + let SUM+=4 #SUM经过上轮计算现在等于2, after sum+4, the assignment to itself becomes 9.
  23. + echo 9
  24. 9 #此时SUM等于5
  25. + (( J++ )) #现在J = 4, and then J + + in the self-assignment, that is, adding 1 to itself, it becomes 5.
  26. + (( J<=100 )) #J小于100就继续执行
  27. + let SUM+=5 #SUM经过上轮计算现在等于9, after sum+5, the assignment to itself becomes 14.
  28. + echo 14
  29. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.