Linux-shell-arithmetic operations {expr, bc, dc, (), and []}

Source: Internet
Author: User
Tags ibase

How do you perform arithmetic operations in Linux? Are you still using expr? You will say that I will also have other bc!

Don't talk about things in real time!

Expr

When using expr, pay attention to writing, such as spaces between the number and operator number in the expression and escape some operator numbers. Remember that expr is only applicable to integer operations!

Expression

The expression section in the help document of expr is as follows:

ARG1 | & ARG2 if both values are not 0 or empty, ARG1 is returned; otherwise, <=! ==>>+-*/% ARG2 calculate the remainder of the division between ARG1 and ARG2

This part is believed to be the most widely used and has a better understanding of it. Here we use an expression to describe it:

$  +  -  \*  /  + \(  -  \) \* 

The results show that you know the calculation rule of expr, which is the same as the mathematical expression we understand in daily life. The priority of parentheses is the highest, followed by "*" and "/". in addition, each number or symbol must be separated by spaces, and the result is also an integer.

String

Expr can also perform operations on strings:

Match string expression equal

1) match

The expr match $ string substring command in expr matches the substring string in the string (the substring string can be a regular expression) and returns the length of the matched substring, if no value is found, 0 is returned.

Here is an example:

┌[--/  ::+][~-LTS- ╰_╯]$str=--/  ::+][~-LTS- ╰_╯]$ match  .*

. * 5 matches 6 characters.

2) substr

In shell, {string: position} and {string: position: length} can be used to extract characters from string strings. The first is to extract from position until the end of the string, and the second is to extract the length of the substring from position. The above functions can also be implemented using expr substr $ string $ position $ length in expr.

Instance:

┌[--/  ::+][~-LTS- ╰_╯]$str=--/  ::+][~-LTS- ╰_╯]$ ${str: --/  ::+][~-LTS- ╰_╯]$ ${str::--/  ::+][~-LTS- ╰_╯]$ substr   

We can see that {string: position} and {string: position: length} start counting from 0, while expr substr $ string $ position $ length starts from 1.

3) index

The expr index $ string substring index Command in expr finds the position of the first occurrence of the character in the substring on the string $ string. If it cannot be found, the expr index returns 0. Note that it matches characters rather than strings.

Instance:

┌[--/  ::+][~-LTS- ╰_╯]$str=--/  ::+][~-LTS- ╰_╯]$ index --/  ::+][~-LTS- ╰_╯]$ index  

4) length

Calculates the length of a string. We can use the length (s) in the awk for calculation. We can also use echo $ {# string} In echo for calculation. Of course, we can also use expr length $ string in expr to find the length of the string.

┌[--/  ::+][~-LTS- ╰_╯]$str=--/  ::+][~-LTS- ╰_╯]$--/  ::+][~-LTS- ╰_╯]$ length 
Bc

The point is coming!

Bc is a computing language of any precision. It is a language that provides some Syntax structures, such as conditional judgment and loops. It can be said to be very powerful, however, I have not found any occasion for this purpose. Another purpose is to perform hexadecimal conversion.

The expr mentioned above supports integer computation, but it is powerless for floating-point computation. In addition, expr cannot perform exponential computation, but there are no more such cases as bc.

Parameters

Let's take a look at several useful parameters:

--- When q enters the bc interaction mode, no additional information such as version is output.
Special Variables

Ibase and obase are used for hexadecimal conversion. ibase is the input base and obase is the output base. The default value is decimal;

Scale decimal reserved digits. The default value is 0.

Interaction Mode

Enter bc in the shell command line and enter the interaction mode of the bc language.

$bc -l -/                           =                   /=                    ^                           *a()                        
Non-Interactive Mode

Bc can also perform non-interactive operations by Using echo.

┌[--/  ::+][~-LTS- ╰_╯]$ |bc          --/  ::+][~-LTS- ╰_╯]$ |bc -l                         --/  ::+][~-LTS- ╰_╯]$ |bc                   --/  ::+][~-LTS- ╰_╯]$ |

For more information, see man!

Dc

You may have known this command, or you may not yet know it. Dc is more complex than bc, but simple operation is still relatively simple. Simply put, dc is always a stack operation. Like bc, dc can also be used in interaction or together with echo.

It also supports floating point operations.

But now I have not thought about this kind of pressure stack-based arithmetic operation.

Interaction Mode
┌[--/  ::+][~/-LTS---*-/-+*

There are other commands such:

 

For more information, see the man document.

Non-Interactive Mode

A formula is similar to what you can see.

┌[--/  ::+][~/-LTS- ╰_╯]$ |

Formula: (4*3 + 2-1 ).

Is it easy!

() & []

These two are common in shell, which are similar to expr commands and are also used for integer calculation.

They support the following operators:

 |+ ||&+ &&<  <= == != >= >  +  -  *  /  % 

The two operators with the plus sign are actually shell-supported operators.

The advantages of these two pairs and expr are that the operators do not need to be escaped.

We use commands that have never been used in shell to do the same thing, but do not forget that () and [] are common in shell and are very practical, but it is not common to you!

Instance:

┌[--/  ::+][~/-LTS- ╰_╯]$ $((  + --/  ::+][~/-LTS- ╰_╯]$ $((  * --/  ::+][~/-LTS- ╰_╯]$ $((  - ---/  ::+][~/-LTS- ╰_╯]$ $((  % --/  ::+][~/-LTS- ╰_╯]$ $[  % --/  ::+][~/-LTS- ╰_╯]$ $[  - ---/  ::+][~/-LTS- ╰_╯]$ $[  * --/  ::+][~/-LTS- ╰_╯]$ $[  + 

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.