If in the shell compare

Source: Internet
Author: User
Tags bit set locale sorts

The way to compare two strings for equality is:

If ["$test" x = "Test" x]; Then

Here are a few key points:

1 using a single equals sign

2 Note that there is a space on each side of the equal sign: This is the UNIX shell requirement

3 Notice the last X of "$test" X, which is deliberately arranged, because when $test is empty, the expression above becomes x = Testx, which is obviously not equal. Without this x, the expression would be an error: [: =: unary operator expected

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

Integer comparison

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

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

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

-ge greater than equals, such as: if ["$a"-ge "$b"]

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

-le less than equals, such as: if ["$a"-le "$b"]

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

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

Small data comparisons can be used with awk

string comparison

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

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

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

1 [[$a = = z*]] # if $ A starts with "Z" (pattern match) then 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 $ A equals z* (character match) Then the result is true

A little explanation, about file globbing is a shorthand for the document, such as "*.c" is, again like ~ also.

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

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

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

Greater than, in ASCII alphabetical order. such as:

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

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

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

Refer to Example 26-11 for an example of this operator application.

The-Z string is "null". The length is 0.

-N string not "null"

Attention:

The use of-N in the [] structure must be used to test the variable. Use a string that is not ""! -Z

Or the string itself, which is not referenced by "", is placed in the [] structure. Although it is generally possible to

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

If-judging
If [conditional judgment one] && (| |) [Conditional Judgment II]; Then
elif [Conditional Judgment III] && (| |) [conditional judgment four]; Then
Else
Execute the third paragraph of the volume program
Fi

For example:

[Email protected]:~# a=0[email protected]:~# b=0[email protected]:~# c=5         [email protected]:~# if [$a = 0-a $b = 0]& amp;&[$c! = 0]; then> Echo success> fisuccess

If the expression used

Primary significance
[-A file] if the file exists as true.
[-B file] If the file exists and is a block-the special file is true.
[-C file] is true if the file exists and is a word-special file.

The [-D file] is true if the file exists and is a directory.
The [-e file] is true if the file exists.
The [-F file] is true if the file exists and is a normal file.
[-G file] is true if the file exists and has set his SGID bit.
The [-H file] is true if the file exists and is a symbolic connection.
[-K file] is true if the file exists and his stick bit has been set.
[-P file] is true if the file exists and is a named pipe (F if o).
The [-R File] is true if the file exists and is readable.
[-S file] is true if the file exists and is larger than 0 bytes.
[-T FD] true if the file descriptor is already open and points to a terminal.
The [-u file] is true if the file exists and has his SUID (set user ID) bit set.
[-W file] is true if the file is true if the file exists and is writable.
The [-X file] is true if the file exists and is executable.
The [-o file] is true if the file exists and belongs to a valid user ID.
[-G file] is true if the file exists and belongs to a valid group ID.
[-L file] is true if the file exists and is a symbolic connection.
[-N file] is true if the file exists and has been mod if IED since it is last read.
[-S file] is true if the file exists and is a socket.
[File 1-nt file 2] True if file 1 has been changed more recently than File 2, or if
File 1 exists and
File 2 does not.
[File 1-ot file 2] is true if file 1 is older than file 2, or file 2 exists and file 1 does not exist.
[File 1-ef file 2] True if file 1 and file 2 refer to the same device and Inode
Numbers
The [-o option name] is true if the shell option "option name" is turned on.
[-Z string] is true if the length of "string" is zero.
The length of [-N STRING] or [string] is True "string" is a non-0 value.
[STRING1 = = STRING2] If two strings are equal to true. "=" May is used instead of "= =" for
Strict POSIX compliance.
[STRING1! = STRING2] is true if 22 strings are not equal.
[STRING1 < STRING2] true if "STRING1" sorts before "STRING2" lexicographically in the
Current locale.
[STRING1 > STRING2] True if "STRING1" sorts after "STRING2" lexicographically in the
Current locale.
[ARG1 OP ARG2]
"OP" is one of-eq,-ne,-lt,-le,-gt Or-ge. These arithmetic binary operators
return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater
than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are.
Integers.
Expressions can be combined using the following operators, listed in decreasing order of precedence:
Operator effect
[ ! Expr] true if expr is false.
[(EXPR)] Returns the value of expr. This can be used to ignore the normal operator precedence.

[Expression 1-a expression 2] If expression 1 and expression 2 is true at the same time.
[Expression 1-o expression 2] If either expression 1 or expression 2 is true.

If in the shell compare

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.