Shell string comparison to determine whether a digital _linux shell
Source: Internet
Author: User
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.
–b returns True when file exists and is a block file
-C Returns True when file exists and is a character file
-D returns True when pathname exists and is a directory
-E Returns True when pathname specified file or directory exists
-F Returns True when file exists and is a regular file
-G returns True when the file or directory specified by pathname exists and the Sgid bit is set
-H Returns True when file exists and is a symbolic link file, which is not valid on some old systems
-K returns True when a file or directory specified by pathname exists and a "sticky" bit is set
-P Returns True when file exists and is a command pipe
-R when the file or directory specified by pathname is present and returned as true when it is readable
-S returns True when file size is greater than 0 o'clock
-U returns True when the file or directory specified by pathname exists and the SUID bit is set
-W Returns True when the file or directory specified by pathname exists and is executable. A directory that is accessed for its content is necessarily executable.
-O Returns True if the file or directory specified by pathname exists and the user specified by the active user ID of the current process is owned by the person.
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.