The Linux shell implements arithmetic (integer and floating-point) simple methods __linux

Source: Internet
Author: User
Tags arithmetic


In just learning to write shell batch processing, the logical operation, there is no need to base: arithmetic, here to say in the Linux shell simple implementation method.

1. Simple Method

[Chengmo@centos5 ~]$ b=$ ((5*5+5-3/2))
[Chengmo@centos5 ~]$ Echo $b
29

In the Linux shell, we can use $ (()) to put an expression in parentheses to achieve the function of the operation.

2. Other methods:

Using: Expr to implement the operation

[Chengmo@centos5 ~]$ expr 5-4
1

Note: Write an expression that requires an operation to be followed by expr to ensure that the argument is separated by a space in the middle of the operation symbol.

Category Grammar Description
Conditional judgment Expr1 \| Expr2 If EXPR1 is not 0 or null, return EXPR1, or return EXPR2.
Expr1 \& EXPR2 If both Expr1 and EXPR2 are Non-zero or null, then the EXPR1 is returned, or 0 is returned.
Arithmetic Expr1 + EXPR2 Returns the value after Expr1 plus expr2.
Expr1-expr2 Returns the value after Expr1 minus expr2.
expr1\* EXPR2 Returns the value after the Expr1 multiplication expr2.
Expr1/expr2 Returns the value after Expr1 except EXPR2.
Expr1% EXPR2 Returns the remainder of the EXPR1 except EXPR2.
Size judgment Expr1 \> EXPR2 If EXPR1 is greater than EXPR2, it returns 1, otherwise it returns 0. If Expr1 and expr2 are numbers, they are judged by the size of the figures, otherwise they are judged by words. The following are all the same.
Expr1 \< EXPR2 If EXPR1 is less than EXPR2, it returns 1, otherwise it returns 0.
EXPR1 = Expr2 If Expr1 equals EXPR2, it returns 1, otherwise it returns 0.
Expr1!= EXPR2 If EXPR1 is not equal to EXPR2, it returns 1, otherwise it returns 0.
Expr1 \>= EXPR2 If EXPR1 is greater than or equal to EXPR2, it returns 1, otherwise it returns 0.
Expr1 \<= EXPR2 If the expr1 is less than or equal to EXPR2, it returns 1, otherwise it 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] Find any strings that match the list.

* Search for 0 or more words before *.

\ (\) Returns a string that matches in parentheses.

3. Floating-point operations:

[Chengmo@centos5 ~]$ expr 5.0-4
Expr: illegal argument

[Chengmo@centos5 ~]$ Echo $ ((5.0-4))
-BASH:5.0-4: syntax error in expression (Error token is ". 0-4")

From the above, it seems that the above expression is not enough to support floating-point operations. Look up the data to find: Bash does not support floating-point operations, if you need to do floating-point operations, the use of Bc,awk processing.

Method One:

[Chengmo@centos5 ~]$ c=$ (echo "5.01-4*2.0" |BC)
[Chengmo@centos5 ~]$ Echo $c
-2.99

Method Two:

[Chengmo@centos5 ~]$ c=$ (awk ' begin{print 7.01*5-4.01} ')
[Chengmo@centos5 ~]$ Echo $c
31.04

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


Http://www.cnblogs.com/chengmo/archive/2010/09/30/1839556.html

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.