How to calculate the numbers in the Shell (bc/expr/$ (())/let/awk)

Source: Internet
Author: User
Tags arithmetic

1.bc

Common calculation Tools , and supports floating-point arithmetic:

[Email protected] shell]# echo | Bc

2

floating point number accuracy problem not resolved

[Email protected] shell]# echo "1.2*1.2" | Bc

1.4

[Email protected] shell]# echo "scale=2;1.2*1.2" | Bc

1.44

[Email protected] shell]# echo "5.0/3.0" | Bc

1

[Email protected] shell]# echo "scale=2;5.0/6.0" |BC

.83

2.expr

floating-point arithmetic not supported, note operators have spaces around

[[email protected] shell]# Expr 10 + 10

20
[[email protected] shell]# expr 1500 + 900


[[email  Protected] shell]# expr 30/3

10
[[email protected] shell]# expr 30/3/2

5

[[email protected] shell]# Expr 30 \* 3

90

3.$ (())

with expr, floating-point arithmetic is not supported

[[email protected] shell]# echo $ ((+))

2

[[email protected] shell]# echo $ ((2*3))

6

[[email protected] shell]# echo $ ((6/2))

3

[[email protected] shell]# echo $ ((6/5))

1

4.let

Floating-point arithmetic is not supported, and direct output is not supported, only values can be assigned

[[email protected] shell]# let a=10+10

[Email protected] shell]# echo $a

20

[[email protected] shell]# let B=50/5

[Email protected] shell]# echo $b

10

[[email protected] shell]# let c=6*5

[Email protected] shell]# echo $c

30

[[email protected] shell]# let C=6/5

[Email protected] shell]# echo $c

1

5.awk

Common operations:

[[email protected] shell]# Echo|awk ' {print (+)} '

2

[[email protected] shell]# Echo|awk ' {print (1/2)} '

0.5

[[email protected] shell]# Echo|awk ' {print (1/3)} '

0.333333

[[email protected] shell]# Echo|awk ' {print (3*5)} '

15

Control Accuracy (printf) :

[Email protected] shell]# echo | awk ' {printf ("%.2f \ n", 1/2)} '

0.50

[Email protected] shell]# echo | awk ' {printf ("%.4f \ n", 1/3)} '

0.3333

Pass parameters:

[[email protected] shell]# echo | awk-v a=5-v b=6 ' {printf("%.4f \ n", A/b)} ' Note: The method a,b does not need to add the $ symbol

0.8333

[Email protected] shell]# a=5

[Email protected] shell]# b=6

[[email protected] shell]# Echo|awk "{print ($a/$b)}" Note: This method requires double quotation marks outside the curly braces

0.833333


This article is from the "Feng" blog, make sure to keep this source http://fengxiaoli.blog.51cto.com/12104465/1952187

How to calculate the numbers in the Shell (bc/expr/$ (())/let/awk)

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.