Introduction to Shell comparison operations _linux Shell

Source: Internet
Author: User
Shell string Comparisons, judging whether it's a number

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

Integer Comparisons

-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")

String Comparisons

= 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
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. For example:
if [["$a" < "$b"]]
If ["$a" \< "$b"]
Note: In the [] structure, "<" needs to be escaped.
> Greater than, in ASCII alphabetical order. For example:
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 is a good habit to use "" to test strings.

awk ' {print $} ' class.txt | grep ' ^[0-9.] ' > Res
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.