Powershell比较运算符,例如Equal,Greater,Lessor,Contains,Regex

Comparison operator is used to compare given values and return an boolean value like true and false . These operators are generally used by if , while and similar decision making keywords.

比较运算符用于比较给定值并返回布尔值,如truefalse 。 这些运算符通常由ifwhile和类似的决策关键字使用。

All comparison operators are case-insensitive by default. So the uppercase or lowercase expression do not changes the behaviour of these comparison operators.

默认情况下,所有比较运算符都不区分大小写。 因此,大写或小写表达式不会更改这些比较运算符的行为。

等于 (Equal)

One of the most used comparison operator is equal. This operator check whether given values are equal or not if they are equal a true value is returned, if they are not equal false value is returned. If parameters are variables their type must be comparable. This operator is expressed as -eq which is short version of equal word.

最常用的比较运算符之一是相等的。 该运算符检查给定值是否相等,如果相等,则返回true值;如果不相等,则返回false值。 如果参数是变量,则它们的类型必须是可比较的。 该运算符表示为-eq ,它是等价字的缩写。

(10 -eq 10)

(10 -eq 11)

(10 -eq "10")

(10 -eq "a")
Equal
Equal
等于

As we can see from examples we get always a true or false value according to comparison.

从示例中可以看出,根据比较,我们总是得到truefalse值。

不平等 (Not Equal)

The opposite of the equal operator is not equal which will simply give reverse results. If given values are not equal this operator will return a true boolean result. If given values are equal this will be a false boolean result. We will express this operator with -ne which is short form of not equal

等于运算符的对立面不相等,这将简单地给出相反的结果。 如果给定值不相等,则此运算符将返回true布尔结果。 如果给定值相等,则将是false布尔结果。 我们用-ne表示此运算符,它是not equal缩写

(10 -ne 10)

(10 -ne 11)

(10 -ne "10")

(10 -ne "a")
Not Equal
Not Equal
不平等

比...更棒(Greater Than)

We can use greater than operator to find which value is greater than other. This operator is used with similar data types but if given values can be converted each other different type of data can be used too. If the first value is greater than second value the statement will return boolean true if not returns boolean false

我们可以使用大于运算符来查找哪个值大于其他值。 该运算符用于类似的数据类型,但是如果给定值可以互相转换,那么也可以使用不同类型的数据。 如果第一个值大于第二个值,则语句将返回布尔值true ,否则返回布尔值false

(10 -gt 5)

(10 -gt "5")

(10 -gt "a")

(10 -gt 15)
Greater Than
Greater Than
比...更棒

大于或等于(Greater Than or Equal)

In previous part we examined the greater than comparison operator. If we want to check the equality with greater than we will use greater than or equal operator. If the first value is greater than or equal to the second value this will return boolean true result if lesser than it will return boolean false result.

在上一部分中,我们检查了大于比较运算符。 如果我们要检查大于等于的相等性,我们将使用大于或等于运算符。 如果第一个值大于或等于第二个值,这将返回布尔true ,如果较少比它会返回布尔结果false的结果。

(10 -ge 5)

(10 -ge 10)

(10 -ge 11)
Greater Than or Equal
Greater Than or Equal
大于或等于

少于(Less Than)

We can compare for the first value lesser than second value. This is the reverse implementation of greater than operator. If the first value is lesser than second value it will return boolean true if not it will return boolean false result. This operator is expressed as -lt

我们可以比较小于第二个值的第一个值。 这是大于运算符的反向实现。 如果第一个值小于第二个值,则将返回布尔值true否则,将返回布尔值false 。 该运算符表示为-lt

(10 -lt 5)

(10 -lt "5")

(10 -lt 15)
Less Than
Less Than
少于

小于或等于(Less Than or Equal)

This operator is similar to less than operator this operator also checks equality. If first value is lesser than or equal to the second value the result is boolean true if not result is boolean false . We will use -le as operator.

该运算符类似于小于该运算符还检查相等性的运算符。 如果第一个值小于或等于第二个值,则结果为布尔值true否则为boolean false 。 我们将使用-le作为运算符。

(10 -le 10)

(10 -le 15)

(10 -le 5)
Less Than or Equal
Less Than or Equal
小于或等于

通配符比较(Wildcard Comparison)

Another useful comparison operator in Powershell is wildcard comparison. This operator compare first value in a wildcard manner. If the second value exists in the first value this will return boolean true result , if not it will return boolean false result. We will express this with -like operator.

Powershell中另一个有用的比较运算符是通配符比较。 该运算符以通配符方式比较第一个值。 如果第二个值存在于第一个值中,则将返回布尔值true结果,如果不存在,则将返回布尔值false结果。 我们将使用-like运算符表达这一点。

("ismail" -like "*mail")

("ismail" -like "*m*")

("ismail" -like "*mi*")
Wildcard Comparison
Wildcard Comparison
通配符比较

正则表达式比较(Regular Expression Comparison)

Regular expression is used to express structured strings. Regular expression is a powerful language. We can use regular expressions in our comparison operations. We will use -match operator to check given regular expression in the value.

正则表达式用于表达结构化字符串。 正则表达式是一种强大的语言。 我们可以在比较操作中使用正则表达式。 我们将使用-match运算符检查值中给定的正则表达式。

("ismail" -match "[aZ]")

("1" -match "[az]")
Regular Expression Comparison
Regular Expression Comparison
正则表达式比较

遏制(Containment)

This operator is used in types like collection, array or hashtable. Containment operator will check given collection, array or hashtable whether second value exists in these values. If exist this will return boolean true if not return boolean false

该运算符用于集合,数组或哈希表等类型。 包含运算符将检查给定的集合,数组或哈希表,其中是否存在第二个值。 如果存在,则返回布尔值true ,否则返回布尔值false

In this example we have a array named cities and contains city names istanbul , ankara and canakkale . We check this array if it contains given values.

在此示例中,我们有一个名为cities的数组,其中包含城市名称istanbulankaracanakkale 。 我们检查此数组是否包含给定值。

$cities="ankara","istanbul","canakkale"

$cities -contains "canakkale"

$cities -contains "canakkal"
Containment
Containment
遏制
LEARN MORE  What Is System Administrator?
了解更多什么是系统管理员?

翻译自: https://www.poftut.com/powershell-comparison-operators-like-equal-greater-lesser-contains-regex/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值