Shell Script Raiders (learning notes)--1.5 to perform mathematical operations

Source: Internet
Author: User

Use Let, (()), or [] for basic integer operations, using expr and BC for advanced operations, including decimal operations.

[Email protected] tmp]# str= [[email protected] tmp]# let str=str+6[email protected] tmp]# let str-=5echo $str

11

You can also self-increment, subtract from the operation.

[[email protected] tmp]# let str++; Echo $str

12

[[email protected] tmp]# let ++str; Echo

13

[[email protected] tmp]# let str--; Echo

12

[[email protected] tmp]# let--str; Echo

11

You can use $ (()) If you want the operation to be in the command, i.e. no additional assignment behavior is required.

Echo $ ((str++))

12

Echo $ ((str--))  #从这个结果中是否发现了问题?

13

Echo $ ((str--))

12

Echo $ ((str++))  #还有这个结果

11

Echo $ ((str++))

12

I do not know whether it is my calculation, or the question of $ (()), if it is $ (()) suffix-plus (i++) and suffix-decrement (i--), then after the suffix self-increment, then the suffix will again perform a self-increment behavior, in contrast, the self-increment is also.

However, using $ (()) to calculate prefix self-increment and self-subtraction transformations is no problem, and let self-decrement is no problem.

Echo $ ((--STR))

12

Echo $ ((--STR))

11

Echo $ ((++str))  #结果正常

12

Echo $ ((++STR))

13

Echo $ ((--STR))

12

You can also use [].

echo $[str= $str +6]  #在 [] Use the variable symbol $

18

Echo $[str=str+6]  #[] does not use the $ symbol

24

However, if $[$str =str+6] or $[$str = $str +6] An error will occur.

echo $[$str =str+6]

-bash: 24=str+6: Attempted assignment to Non-variable (Error token is "=str+6")

echo $[$str = $str +6]

-bash: 24=24+6: Attempted assignment to Non-variable (Error token is "=24+6")

Summarize the operation method of the variable:

[Email protected] tmp]# i= [[email protected] tmp]# Let I=i-1[[email protected] tmp]# Let i-=1[[email protected] tmp]# I=$ ((-1)) [[email protected] tmp]# I1  ]  #中括号隔壁必须写空格 [[email protected] tmp]# i1 ]

Shell Script Raiders (learning notes)--1.5 to perform mathematical operations

Related Article

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.