Shell string comparison to determine whether it is a number

Source: Internet
Author: User

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.

Awk '{print $2}' class.txt | grep '^ [0-9.]'> res

-B. returns true if the file exists and is a block file.
-C: returns true if the file exists and is a character file.
-D. If pathname exists and is a directory, true is returned.
-E: returns true if the file or directory specified by pathname exists.
-F returns true if the file exists and is a regular file
-G returns true if the file or directory specified by pathname exists and the SGID bit is set.
-H returns true if the file exists and is a symbolic link file. This option is invalid in some old systems.
-K. If a file or directory specified by pathname exists and a "Sticky" bit is set, the system returns the true value.
-P: returns true if the file exists and is a command pipeline.
-R: returns true if the file or directory specified by pathname exists and is readable.
-S: returns true if the file size is greater than 0.
-U returns true if a file or directory specified by pathname exists and SUID is set.
-W returns true if the file or directory specified by pathname exists and is executable. A directory must be executable for its content access.
-O returns true if a file or directory specified by pathname exists and the user specified by the valid user ID of the current process is owned by it.

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.