Oracle technology: Shell string Comparisons

Source: Internet
Author: User
Tags comparison regular expression requires

The method of comparison of integers in a Shell and the method of comparing strings, such as equals, not equal to, greater than, greater than or equal, less than, etc.

Binary comparison operators, comparing variables or comparing numbers. Notice the difference between the number and the string.

--------------------------------------------------------------------------------

1. Integer comparison

-eq equals, such as: if ["$a"-eq "$b"]

-ne is not equal to, such as: if ["$a"-ne "$b"]

-GT is greater than, such as: if ["$a"-gt "$b"]

-ge is greater than or equal to, such as: if ["$a"-ge "$b"]

-lt is less than, such as: if ["$a"-lt "$b"]

-le is less than or equal, such as: if ["$a"-le "$b"]

< less than (requires double brackets), such as: (("$a" < "$b")

<= is less than or equal (requires double parenthesis), such as: (("$a" <= "$b")

> Greater than (requires double brackets), such as: (("$a" > "$b")

>= is greater than or equal (requires double parenthesis), such as: (("$a" >= "$b")

--------------------------------------------------------------------------------

2, string comparison

= equals, such as: if ["$a" = "$b"]

= = equals, such as: if ["$a" = = "$b"], and = equivalent

Note: the function of = = = is different in [[]] and [], as follows:

1 [[$a = = z*]] # If $a starts with "Z" (pattern match) then it will be true

2 [[$a = = "z*"]] # if $a equals z* (character match), then the result is true

3

4 [$a = = z*] # File globbing and word splitting will occur

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

5 ["$a" = "z*"] # if $a equals z* (character match), then the result is true

A little explanation, about the file globbing is a shorthand for documents, such as "*.c" is, and so on.

But file globbing is not a strict regular expression, although in most cases the structure is more like.

!= is not equal to, such as: if ["$a"!= "$b"]

This operator will use pattern matching in the [[]] structure.

< less than, in ASCII alphabetical order. Such as:

if [["$a" < "$b"]]

If ["$a" \< "$b"]

Note: In the [] structure, "<" needs to be escaped.

> Greater than, in ASCII alphabetical order. Such as:

if [["$a" > "$b"]]

If ["$a" \> "$b"]

Note: In the [] structure, ">" needs to be escaped.

Refer to Example 26-11 to see examples of this operator application.

The-Z string is "null". That is, the length is 0.

-N string is not "null"

Attention:

Using-n tests in the [] structure must be caused by the variable. Use a string that is not ""! -Z

Or the string itself, which is not quoted, is placed in the [] structure. Although in general you can

To work, but it's not safe. It's a good habit to use "" to test strings.

--------------------------------------------------------------------------------

3, Shell number comparison examples

Method One: If [${a}-lt ${b}]; Then ...

This is the most basic comparison method, using LT (less than), GT (greater than), LE (less than equal), GE (greater than equal), advantages: not found; disadvantage: can only compare integers, use lt,gt and so on not intuitive

Method Two: if ((${a} < ${b})) then ...

This is the Cshell style comparison, advantages: Do not use LT,GT and other difficult to remember the string; disadvantage: or only to compare integers

Method Three: if (Echo ${a} ${b} | awk '! ($1>$2) {exit 1} ') then ...

This is the advantage of using awk: you can compare decimals; disadvantages: Expressions are too complex to remember

Method Four: if (Echo ${a}-${b} | Bc-q | Grep-q "^-"); then ...

This is calculated using the BC comparison, the advantages: you can compare decimals; disadvantages: more complex expressions, difficult to remember

--------------------------------------------------------------------------------

4. Shell calculation

Method One: typeset c=$ (expr ${a} + ${b});/typeset c= expr ${a} + ${b};

The basic tools in the shell, advantages: convenient to detect whether the variable is a number; disadvantages: only integers can be computed, and only addition and subtraction can be computed, and the multiplication and division method cannot be computed.

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.