Shell Scripting Learning notes-integer Two-dimensional comparison operator

Source: Internet
Author: User
1.1 The following table is a common two-dimensional comparison operator

We can also see through the man test

Tips

(1) The "<" symbol is less than the meaning, if[["$a" < "$b"]], if[["$a" \< "$b"]. You need to escape in parentheses because the shell is also redirected with < and >.

(2) the ">" symbol is greater than the meaning, if[["$a" > "$b"]], if[["$a" \> "$b"]. You need to escape in parentheses because the shell is also redirected with < and >.

(3) The "=" symbol equals the meaning, if[["$a" = "$b"]],if[["$a" = "$b"]. Escaping is not required in single [].

Special tips:

"=" and "! = "use in [] does not need to escape, the symbol containing" > "and" < "in the [] number is used to escape, the result of the number is not escaped may not be error, but the result may not be.

Example 1: Two-time numeric comparisons

Actual test results concluded:

(1) integer plus double quotes is also true.

(2) [[]] use-EQ and other writing is also right, [[]] with > may not be correct, only the first, the logical result is not correct.

(3) [] using the > Code syntax error, the logical result is not correct.

Work scenario: recommended []-eq usage.

Summary: integer comparison recommended usage

[$num –eq $num 2] Note the spaces, and compare symbols.

(($num 1> $num 2)) No spaces, general mathematical comparison symbols.

Integer comparisons can be double-quoted or non-additive.
System Script Example Grep–w "-eq"/etc/init.d/nfs

Shell Scripting Learning notes-integer Two-dimensional comparison operator

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.