Shell implementation Arithmetic Simple method

Source: Internet
Author: User
Tags arithmetic

When you just learned to write shell batches, there is no need to base your logic operations on the basics: arithmetic, here's a simple implementation method in the Linux shell.
1. Simple method
$ b=$ ((5*5+5-3/2))
$ echo $b
29
In the Linux shell, we can use $ (()) to put the expression in parentheses to achieve the function of the operation.
2. Other methods:
Using: Expr to implement operations
$ expr 5-4
1
Note: Write an expression that requires an operation after expr, guaranteeing that there are spaces between the arguments and the operation symbol.

Category Grammar Description
Conditional judgment Expr1 \| Expr2 Returns EXPR1 if EXPR1 is not 0 or null, otherwise returns EXPR2.
Expr1 \& EXPR2 If both EXPR1 and expr2 are nonzero or null, return to EXPR1, or return 0.
Arithmetic Expr1 + EXPR2 Returns the value after Expr1 plus expr2.
Expr1-expr2 Returns the value after Expr1 minus expr2.
expr1\* EXPR2 Returns the value of Expr1 multiplied by EXPR2.
Expr1/expr2 Returns the value of Expr1 except EXPR2.
Expr1% EXPR2 Returns the remainder of the EXPR1 except EXPR2.
Size judgment Expr1 \> EXPR2 Returns 1 if EXPR1 is greater than EXPR2, otherwise returns 0. If Expr1 and expr2 are numbers, they are judged by the size of numbers, otherwise they are judged by words. The following are the same.
Expr1 \< EXPR2 If EXPR1 is less than EXPR2, return 1, or return 0.
EXPR1 = Expr2 Returns 1 if Expr1 equals EXPR2, otherwise returns 0.
Expr1! = Expr2 If EXPR1 is not equal to EXPR2, it returns 1, otherwise returns 0.
Expr1 \>= EXPR2 Returns 1 if EXPR1 is greater than or equal to EXPR2, otherwise returns 0.
Expr1 \<= EXPR2 Returns 1 if EXPR1 is less than or equal to EXPR2, otherwise returns 0.
word processing Expr1:expr2 Compares a fixed string, that is, regular expression. You can use the following characters to assist:

. Matches one character.

$ to find the end of the string.

[List] to find any string that matches the list.

* Search for 0 or more words before *.

\ (\) returns the string that matches the parentheses.

3. Floating-point arithmetic

$ expr 5.0-4
Expr:non-integer argument

$ echo $ ((5.0-4))

-BASH:5.0-4: syntax error in expression (Error token is ". 0-4")

From the above results, it appears that the above expression is not sufficient to support floating-point arithmetic. The data found: Bash does not support floating-point arithmetic, if you need to do floating-point arithmetic, need to use Bc,awk processing.

Method One:
[Email protected] ~]$ c=$ (echo "5.01-4*2.0" |BC)
[Email protected] ~]$ echo $c
-2.99
Method Two:
[[email protected] ~]$ c=$ (awk ' begin{print 7.01*5-4.01} ')
[Email protected] ~]$ echo $c
31.04

Note: $ () in the shell is equivalent to '. The middle contains the command statement execution, which returns the execution result.

Shell implementation Arithmetic Simple method

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.