Shell numeric and string comparison

Source: Internet
Author: User

Comparison of strings and values in Shell

Binary comparison operator, which compares variables or numbers. Note the difference between numbers and strings.

Integer comparison

-EQ equals to, for example, if ["$ A"-EQ "$ B"]
-Ne is not equal to, for example, if ["$ A"-ne "$ B"]
-GT is greater than, for example, if ["$ A"-GT "$ B"]
-Ge is greater than or equal to, for example, if ["$ A"-ge "$ B"]
-Lt is less than, for example, if ["$ A"-lt "$ B"]
-Le is less than or equal to, for example, if ["$ A"-Le "$ B"]
<Less than (double parentheses are required), such as: ("$ A" <"$ B "))
<= Less than or equal to (double parentheses are required), for example: ("$ A" <= "$ B "))
> Greater than (double parentheses are required), such as: ("$ A"> "$ B "))
> = Equal to or greater than (double parentheses required), for example: ("$ A"> = "$ B "))

String comparison
= Equals, for example, if ["$ A" = "$ B"]
= Equal to, such as: If ["$ A" = "$ B"], equivalent to =
Note: The = function has different behaviors in [[] and [], as shown below:
1 [[$ A = z *] # True if $ A starts with "Z" (pattern matching)
2 [[$ A = "z *"] # If $ A is equal to z * (character matching), the result is true.
3
4 [$ A = z *] # file globbing and word splitting will occur
5 ["$ A" = "z *"] # If $ A is equal to z * (character matching), the result is true.
For a bit of explanation, file globbing is a stenographer of files, for example, "*. c" is, and then ~ Yes.
But file globbing is not a strict regular expression, although the structure is similar in most cases.
! = Not equal to, such as: If ["$ "! = "$ B"]
This operator uses pattern matching in the [[] structure.
<Less than, in the ASCII alphabetic order. For example:
If [["$ A" <"$ B"]
If ["$ A" \ <"$ B"]
Note: "<" must be escaped in the [] structure.
> Greater than, in the ASCII alphabetic order. For example:
If [["$ A"> "$ B"]
If ["$ A" \> "$ B"]
Note: ">" needs to be escaped in the [] structure.
For more information, see Example 26-11.
-Z string is "null". The length is 0.
-N string is not "null"
Note:
To use-N in the [] structure, you must use "" To cause variables. Use a string that is not! -Z
Or the strings that are not referenced by "" are put in the [] structure. In general
Work, but this is not safe. It is a good habit to use "" To test strings.

Shell numeric comparison and calculation example

Comparison:
Method 1: If [$ {A}-lt $ {B}]; then...
This is the most basic comparison method. It uses LT (less than), GT (greater than), Le (less than or equal to), Ge (greater than or equal to). Advantages: Not found yet; disadvantages: only integers can be compared. It is not intuitive to use lt, GT, etc.
Method 2: If ($ {A} <$ {B}) then...
This is a comparison of the cshell style. Advantages: you do not need to use strings that are hard to remember, such as Lt, gt; disadvantages: You can only compare integers.
Method 3: If (echo $ {A }$ {B} | awk '! ($1> $2) {Exit 1} ') then...
This is a comparison using awk. Advantages: decimals can be compared; disadvantages: The expression is too complex and hard to remember.
Method 4: If (echo $ {A}-$ {B} | BC-q | grep-Q "^-"); then...
This is a comparison using BC. Advantages: decimals can be compared. Disadvantages: The expression is more complex and hard to remember.

Computing:
Method 1: typeset C =$ (expr $ {A} + $ {B });
The basic tool in shell. advantages: it is convenient to check whether the variable is a number. Disadvantages: Only integers can be calculated, and only addition and subtraction can be calculated, but not multiplication and division can be calculated.
Method 2: Let "C =$ {A} + $ {B}"; or let "c = a + B"
Embedded command calculation, advantages: can calculate multiplication and division, bit operations, etc.; disadvantage: Can only calculate Integers
Method 3: typeset C =$ (a + B ))
The cshell-style computation has the following advantages: it can calculate multiplication and division, bit operations, and so on. It is easy to compile. Disadvantages: it cannot calculate decimals.
Method 4: typeset C =$ {echo $ {A }$ {B} | awk '{print $1 + $2 }')
Awk computing has the following advantages: it can calculate decimal places and implement multiple calculation methods, which is flexible. Disadvantages: The expression is too complex.
Method 5: typeset C =$ {echo $ {A} + $ {B} | BC-q)
The advantage of awk computing is that it can calculate decimal places, which is more flexible than awk. disadvantage: The expression is too complex and the number of digits after the decimal point must be set by scale = n, otherwise, the result may be truncated to an integer.

From: http://apps.hi.baidu.com/share/detail/31263915

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.