Linux Shell mathematical operations

Source: Internet
Author: User

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%  

 

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.