Linux Shell mathematical operations
In Linux, directly using mathematical operators for mathematical computation often does not get the expected computation results. To perform mathematical operations in Shell, we need to use small points. Currently, there are three main mathematical methods in Linux Shell: bc, expr, And let.
1 bc1.1 command line
On the bash interface, directly enter bc or bc-q to go to the bc command line and use mathematical operators to get the expected results:
[scott@centos1 ~]$ bc - + - - * / % ^ scale=;/ . % scale=;/ %
Enter the Operation Number and operator number, and press enter to obtain the operation result. By setting scale, you can define the current decimal precision, which is effective for division, remainder, and power operations.
This method can only be performed in the command line of bc. Of course, it cannot be done in the code.
1.2 MPs queue Mode
[scott@centos1 ~]$ +| [scott@centos1 ~]$ -| - [scott@centos1 ~]$ *| [scott@centos1 ~]$ /| [scott@centos1 ~]$ %| [scott@centos1 ~]$ | . [scott@centos1 ~]$ | . [scott@centos1 ~]$ | [scott@centos1 ~]$ | [scott@centos1 ~]$ ^|
This pipeline method applies more in shell, and can also add precision restrictions during computation.
1.3 hexadecimal conversion
[scott@centos1 ~]$ | [scott@centos1 ~]$ | [scott@centos1 ~]$ |
In the preceding example, we convert all of the preceding operations into a decimal system.
1.4 expression operations
[scott@centos1 ~]$ vim bc- [scott@centos1 ~]$ bc -q bc- - .
Where, the content of the bc-test.bc is:
+-*/=;/=;/
Is a set of expressions.
2 expr
Expr is a powerful command that can perform mathematical operations or string operations. Let's take a look at the mathematical functions.
[scott@centos1 ~]$ + + [scott@centos1 ~]$ + [scott@centos1 ~]$ + [scott@centos1 ~]$ * [scott@centos1 ~]$ \* [scott@centos1 ~]$ / [scott@centos1 ~]$ %
Expr does not support floating-point operations and power multiplication operations. When performing operations, you must note the separation of operators and arithmetic operations. Writing them together is not recognizable. In addition, multiplication is special and needs to be escaped.
Next let's take a look at the string operation of expr.
[scott@centos1 ~]$ = [scott@centos1 ~]$ length $ [scott@centos1 ~]$ index $ [scott@centos1 ~]$ substr $ [scott@centos1 ~]$ substr $ 789asdfg
In the preceding example, the expr command is used to calculate the length of a string, obtain the first occurrence position of a string or character, and obtain a character string with a limited length starting from a specified position, note that the subscript in expr starts from 1.
3 let
[scott@centos1 ~]$ let a=+ [scott@centos1 ~]$ [scott@centos1 ~]$ let a=* [scott@centos1 ~]$ [scott@centos1 ~]$ let a=/ [scott@centos1 ~]$ [scott@centos1 ~]$ let a=% [scott@centos1 ~]$ [scott@centos1 ~]$ let a=^ [scott@centos1 ~]$ [scott@centos1 ~]$ let a=** [scott@centos1 ~]$
Note that the Power Multiplication operation in the let command is not ^, **.
4. Other Methods
[scott@centos1 ~]$ $((+ [scott@centos1 ~]$ $((* [scott@centos1 ~]$ $((** [scott@centos1 ~]$ $((((+))* [scott@centos1 ~]$ ` Fri Aug :: PDT [scott@centos1 ~]$ ` +%Y%m%