Summary of mathematical calculations in shell _linux Shell

Source: Internet
Author: User
Tags sin
1. Examples of error methods
A
Var=1+1
Echo $var
The result of the output is 1+1, tragedy, hehe

b
Var=1
var= $var +1
Echo $var
The output result is 1+1, still tragic, hehe

2. The correct method
1) using Let
Var=1
Let "Var+=1"
Echo $var
The output is 2, no tragedy this time.
Attention:
A I tested let almost support all operators, see an article on the internet that "let does not support + + 、--and commas, (,)", but I tested the self, the self reduction, and the parentheses are well supported by the priority
b) The square power operation should use "* *"
c) parameter is accessed directly in an expression without having to add $
d) in general, arithmetic expressions can be without double quotes, but if you have a keyword in bash in the expression, you need to add
E-let expression can only be integer operation

2) use (())
Var=1
((var+=1))
Echo $var
Output is 2
Attention:
(()) is used in exactly the same way as let

3) using $[]
Var=1
var=$[$var +1]
Echo $var
Output bit 2
Attention:
A) $[] the expression within the brackets as a mathematical operation to calculate the results before outputting
b) To access the variables in $[] before you need to add $
c) $[] supports the same operators as let, but only integer operations are supported

4) Using expr
Var=1
Var= ' expr $var + 1 '
Echo $var
Output is 2
Attention:
A the expression symbol after expr needs to be separated by a space
b The operators supported by expr are: |, &, <, <=, =,!=, >=, >, + 、-、 *,/,%
c) The operations supported by expr are used with \ to escape: |, &, <, <=, >=, >, *
e) expr also supports only integer operations

5 use BC (can be used for floating-point calculation)
Var=1
Var= ' echo ' $var +1 "|BC"
Echo $var
Output is 2
Introduced:
BC is a simple calculator under Linux, support floating-point calculation, the command line to enter the BC is entered into the calculator program, and we want to directly in the program floating-point calculation, the use of a simple pipe can solve the problem.
Attention:
1 by my test BC supports all operators except the bitwise operator.
2 BC to use scale for precision setting
3 example of floating point count
var=3.14
Var= ' echo ' scale=2; $var *3 "|BC"
Echo $var
Output is 9.42

6 use awk (floating point count is available)
Var=1
Var= ' echo ' $var 1 "|awk ' {printf ("%g ", $1*$2)} '
Echo $var
Output is 2
Introduced:
Awk is a text processing tool as well as a programming language, and as a programming language, AWK supports multiple operations, and we can use awk to compute floating-point numbers, just like the BC above, through a simple pipe, where we can call awk directly in the program for floating-point counting.
Attention:
1 awk supports all operators except the micro-operation operator
2 awk built-in log, sqr, cos, sin, and so on functions
3 example of floating point count
var=3.14
Var= ' echo ' $var 2 "|awk ' {printf ("%g ", Sin ($1/$2))} '
Echo $var
Output is 1
3, Summary
Finally put all the data can be collected to read, and finally summed up a set of their own things, and then encountered similar problems will have seen, haha ~

Yorking Alan
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.