Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use shell to compare whether two strings are equal

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use shell to compare whether two strings are equal. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The way to compare whether two strings are equal is:

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

The key points here are as follows:

1 use a single equal sign

2 notice that there is a space on each side of the equal sign: this is the requirement of unix shell

3 notice the last x of "$test" x, which is deliberately arranged, because when $test is empty, the above expression becomes x = testx, which is obviously not equal. Without this x, the expression will report an error: [: =: unary operator expected

Binary comparison operator, comparison variable or comparison number. Pay attention to the difference between numbers and strings.

Integer comparison

-eq equals, 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, such as: if ["$a"-ge "$b"]

-lt is less than, for example: if ["$a"-lt "$b"]

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

Greater than (double parentheses are required), such as (("$a" > "$b"))

> = greater than or equal to (double parentheses are required), such as: (("$a" > = "$b"))

Small data can be compared with AWK.

String comparison

= equal to, for example: if ["$a" = "$b"]

= equal to, for example: if ["$a" = = "$b"], equivalent to =

Note: the function of = = behaves differently in [[]] and [], as follows:

1 [[$a = = z*]] # if $a starts with "z" (pattern match) then it will be true

2 [[$a = "z*"] # if $an equals z * (character match), then the result is true

three

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

5 ["$a" = "z*"] # if $an equals z * (character match), then the result is true

A little explanation, about File globbing is a kind of shorthand about files, such as "* .c" is, and so is also.

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

! = not equal to, for example: 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: ">" needs to be escaped in the [] structure.

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

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

-n string is not "null"

Note:

Using-n to test in the [] structure must use "" to generate variables. Use a string that is not "" to use!-z or the string itself that is not referenced by "", put it in the [] structure. Although it can work under normal circumstances, it is not safe. It is a good habit to use "" to test strings.

Thank you for reading! This is the end of this article on "how to use shell to compare whether two strings are equal". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report