[Personal notes] Shell compares whether two strings are equal

The way to compare two strings for equality is:

if [ "$test"x = "test"x ]; then

There are a few key points here:

1 Use a single equal sign

2 Note that there is a space on both sides of the equal sign: this is a requirement of the unix shell

3 Notice the x at the end of "test" x, which is by design, because when the x at the end of test"x, this is by design, because whent e s t " The x at the end of x is deliberately arranged , because when test is empty , the above expression becomes x = testx, which is obviously not equal. And if there is no x, the expression will report an error: [: =: unary operator expected

Binary comparison operators, compare variables or compare numbers. Note the difference between numbers and strings.

integer comparison

-eq is equal to, such as: if [ " a " − eq " a" -eq "a"eq"b” ]

-ne is not equal, such as: if [ " a " − ne " a" -ne "a"ne"b” ]

-gt greater than, such as: if [ " a " − gt " a" -gt "a"gt"b” ]

-ge is greater than or equal to, such as: if [ " a " − ge " a" -ge "a"ge"b” ]

-lt is less than, such as: if [ " a " − lt " a" -lt "a"lt"b” ]

-le is less than or equal to, such as: if [ " a " − le " a" -le "a"le"b” ]

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

AWK can be used for small data comparison

string comparison

= is equal to, such as: if [ " a " = " a" = "a"="b” ]

== is equal to, such as: if [ " a " = = " a" == "a"==" b"], equivalent to =

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

1 [[ KaTeX parse error: Expected 'EOF', got '#' at position 12: a == z* ]] #̲ will be true if a starts with "z" (pattern match)

2 [[ KaTeX parse error: Expected 'EOF', got '#' at position 14: a == "z*" ]] #̲ If a is equal to z* (character match), then the result is true

3

4 [ $a == z* ] # File globbing and word splitting will happen

5 [ " KaTeX parse error: Expected 'EOF', got '#' at position 14: a" == "z*" ] #̲ If a is equal to z* (character match), then the result is true

A little explanation, about File globbing is a shorthand method for files, such as "*.c", or ~.

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

!= Not equal, such as: if [ " a " ! = " a" != "a"!="b” ]

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

Greater than, in ASCII alphabetical order. For example:

if [[ “ a " > " a" > " a">"b” ]]

if [ “ a "   " a" \> " a""b” ]

Note: ">" needs to be escaped in the [] structure.
Refer to Example 26-11 to see an example of the application of this operator.
-z The string is "null". That is, the length is 0.
-n The string is not "null"
Note:
When using -n to test in the [] structure, you must use "" to quote the variable. Use a string that is not "" to use! -z or the string itself that is not quoted with "", Put it in the [] structure. While this works in general
, it is not safe. It is good practice to use "" to test strings.

Original related information: https://blog.csdn.net/weixin_35408656/article/details/113682373

Guess you like

Origin blog.csdn.net/m0_49303490/article/details/128160377