Linux shell bash comparison

Source: Internet
Author: User

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 actions in [[] and [] are different in some cases:
 
[[$ A = z *] # true if $ a starts with "z" (pattern matching)
[[$ A = "z *"] # If $ a is equal to z * (character matching), the result is true.
 
[$ A = z *] # File globbing and word splitting will occur
["$ A" = "z *"] # If $ a is equal to z * (character matching), the result is true.
 
File globbing is a stenographer for 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.
(I don't quite understand. If there is an a.txt file under the/tmpdirectory, then ["a.txt" =. *] will return the true logic, but if/tmphas a.txt and. out two files, so Shell writing will report an error)
 
! = Not equal to, such as: if ["$ "! = "$ B"]
Note :! = Use 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.
 
-The z string is null. The length is 0.
-N string is not empty
Note: Double quotation marks ("") are good for comparing strings or numbers and can avoid unnecessary troubles (see, however, the mode and regular expression cannot contain double quotation marks.
 
 
Hybrid comparison
-A logic and
Exp1-a exp2 if both exp1 and exp2 are true, this expression returns true
-O logic or
Exp1-o exp2 if either exp1 or exp2 is true, the expression returns true.
 
However, in [[], use & | instead of-a and-o,-o and-a are generally used together with the test command or []
 
Let's look at some examples:
01
#! /Bin/bash
02
# Comparison of numbers and strings
03
 
04
# The variables a and B can be both integer and string.
05
# Bash variables are not strongly typed.
06
A = 4
07
B = 5
08
# Compare a and B as integers
09
If ["$ a"-ne "$ B"]
10
Then
11
Echo "$ a is not equal to $ B"
12
Echo "(arithmetic comparison )"
13
Fi
14
Echo
15
# Comparison of a and B as strings
16
If ["$ "! = "$ B"]
17
Then
18
Echo "$ a is not equal to $ B ."
19
Echo "(string comparison )"
20
Fi
21
 
22
# In this particular example, "-ne" and "! = "Yes.
23
 
24
Exit 0
 
 
01
#! /Bin/bash
02
# Test whether the string is null
03
# $ String1 is not declared and initialized
04
If [-n $ string1]
05
Then
06
Echo "String \" string1 \ "is not null ."
07
Else
08
Echo "String \" string1 \ "is null ."
09
Fi
10
# Incorrect results.
11
# Show $ string1 is not empty, although it is not initialized.
12
 
13
Echo
14
 
15
# Add double quotation marks to $ string1 and try again.
16
If [-n "$ string1"]
17
Then
18
Echo "String \" string1 \ "is not null ."
19
Else
20
Echo "String \" string1 \ "is null ."
21
Fi
22
# Output our expected results this time
23
 
24
Echo
25
 
26
If [$ string1] # This time, $ string1 becomes "naked", which is equivalent to [-n $ string1].
27
Then
28
Echo "String \" string1 \ "is not null ."
29
Else
30
Echo "String \" string1 \ "is null ."
31
Fi
32
 
33
String1 = initialized
34
If [$ string1] # Come back
35
Then
36
Echo "String \" string1 \ "is not null ."
37
Else
38
Echo "String \" string1 \ "is null ."
39
Fi
40
# Output the expected results this time
41
 
42
# But it is best to add double quotation marks "" to the string, because this can avoid unnecessary troubles and is also a good habit.
 

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.