Shell BASICS (4) arithmetic operations

Source: Internet
Author: User
Tags bitwise operators

--- Content from http://www.jb51.net/article/31232.htm

By default, values assignment and operations in shell are processed by strings,
1. Example of an error method
A)
Var = 1 + 1
Echo $ VaR
The output result is 1 + 1. Tragedy.

B)
Var = 1
Var = $ var + 1
Echo $ VaR
The output result is 1 + 1, which is still tragic.

2. Correct Method
1) Use let
Var = 1
Let "Var + = 1"
Echo $ VaR
The output result is 2. There is no tragedy this time.
Note:
A) I tested that let supports almost all operators,
B) The power operation should use "**"
C) directly access parameters in expressions without adding $
D) In general, the arithmetic expression can be enclosed with no double quotation marks. However, if the expression contains a bash keyword, you must add
E) The let expression can only perform integer operations.

2) Use (())
Var = 1
(VAR + = 1 ))
Echo $ VaR
The output result is 2.
Note:
() Is used in the same way as let

3) use $ []
Var = 1
Var = $ [$ var + 1]
Echo $ VaR
Output result 2
Note:
A) $ [] use the expression in brackets as a mathematical operation to calculate the result and then output it.
B) before accessing the variables in $ [], add $
C) $ [] supports the same operators as let, but only integer operations.

4) use expr
Var = 1
Var = 'expr $ var + 1'
Echo $ VaR
The output result is 2.
Note:
A) expressions after expr must be separated by spaces.
B) expr supports the following operators: |, &, <, <=, =, and ,! =,> =,>, +,-, *,/, %
C) when using the operators supported by Expr, the following escape characters must be used: |, &, <, <=, >=,> ,*
E) expr only supports integer operations.

5) Use BC (floating point numbers can be calculated)
Var = 1
Var = 'echo "$ var + 1" | BC'
Echo $ VaR
The output result is 2.
Introduction:
BC is a simple calculator in Linux. It supports floating point calculation. Input BC in the command line to enter the calculator program. When we want to perform floating point calculation directly in the program, use a simple pipeline to solve the problem.
Note:
1) I tested that BC supports all operators except bitwise operators.
2) scale should be used in BC for precision setting
3) floating point computing instance
Var = 3.14.
Var = 'echo "scale = 2; $ Var * 3" | bc'
Echo $ VaR
The output result is 9.42.

6) Use awk (floating point numbers can be calculated)
Var = 2
Var = 'echo "$ var 1" | awk '{printf ("% G", $1*$2 )}''
Echo $ VaR
The output result is 2.
Introduction:
Awk is a text processing tool and a programming language. As a programming language, awk supports multiple computations, and we can use awk for floating point computing, like the above BC, through a simple pipeline, we can directly call awk in the program for floating point calculation.
Note:
1) awk supports all operators except micro-operation Operators
2) awk has built-in functions such as log, sqr, cos, and sin.
3) floating point computing instance

Shell BASICS (4) arithmetic 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.