当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 PowerShell 中的 if 语句中组合多个条件

作者:精品下载站 日期:2024-12-14 05:29:51 浏览:17 分类:玩电脑

在 PowerShell 中的 if 语句中组合多个条件


[玩转系统] 在 PowerShell 中的 if 语句中组合多个条件

我们使用带有逻辑运算符和比较运算符的 if 语句在 PowerShell 中组合多个条件。我们还使用 (()) 表示的括号对条件进行分组并控制计算顺序。因此,了解我们有哪些逻辑运算符、算术运算符和比较运算符、它们的含义以及它们的作用非常重要。请参阅下表以快速理解。
所有比较运算符、逻辑运算符和算术运算符及其优先级都在代码示例后面以表格形式进行了解释。

if 与逻辑和比较运算符结合使用

if 语句与一个逻辑运算符 (-and) 和两个比较运算符(-gt-lt) 在 PowerShell 中组合多个条件。

将 if 与 -and、-gt 和 -lt 一起使用:

$number1 = 3
$number2 = 11
if ($number1 -lt 4 -and $number2 -gt 10){
    echo "'$number1' is less than '4' while '$number2' is greater than '10'"
}

输出 :

'3' is less than '4' while '11' is greater than '10'

上面的 if 语句只有在两个条件都为 True 时才会被执行;我们再举一个两个条件都不满足的例子。

将 if 与 -and、-gt 和 -lt 一起使用:

$number1 = 4
$number2 = 11
if ($number1 -lt 4 -and $number2 -gt 10){
    echo "'$number1' is less than '4' while '$number2' is greater than '10'"
}

这次我们不会得到任何输出,因为这两个条件仍然需要满足。

使用带有一个逻辑运算符 (-or) 和一个比较运算符 (-eq) 的 if 语句在 PowerShell 中组合多个条件.

将 if 与 -or 和 -eq 一起使用:

$firstname = "John"
$lastname = "Williamson"
if ($firstname -eq "John" -or $lastname -eq "Williamson") {
    echo "Condition satisfied."
}
else{
    echo "Condition not satisfied."
}

输出 :

Condition satisfied.

这里,只有当至少一个或所有指定条件为 True 时,if 块才会被执行;否则,else 块将被执行。让我们修改一下没有条件为 True 的示例。

将 if 与 -or 和 -eq 一起使用:

$firstname = "Martin"
$lastname = "Guptill"
if ($firstname -eq "John" -or $lastname -eq "Williamson") {
    echo "At least one or all conditions are satisfied."
}
else{
    echo "No condition is satisfied."
}

输出 :

No condition is satisfied.

使用带有两个逻辑运算符(-not-or)和一个比较运算符(-lt) 在 PowerShell 中组合多个条件。

将 if 与 -not、-or 和 & -eq 一起使用:

if (-not 4 -le 2 -or 4 -lt 2) {
    echo "Do something."
}

输出 :

Do something.

重点!这个例子需要你注意才能理解。首先,必须了解上述代码片段中使用的每个运算符的优先级。因此,-not 具有最高优先级,将首先评估,而 -or 具有最低优先级,将最后考虑。现在,我们有两个具有相同优先级的 -lt 运算符,但紧接着 -not 运算符之后的 -lt 运算符将被求值第一的。

下面是执行流程:

  1. 4 -lt 2=
  2. -not 4 -lt 2=True
  3. 另一个 4 -lt 2=False
  4. 步骤 2 -or 步骤 3 的结果=True -or False=True

现在是引入括号的最佳时机,它可以控制求值顺序并增加代码的可读性。 () 中的表达式将首先被计算。如果我们有嵌套的括号,则首先考虑内部的括号。请参阅以下示例。

将 if 与 -not、-or & -eq 一起使用:

if ((-not (4 -le 2)) -or (4 -lt 2)) {
    echo "Do something."
}

输出 :

Do something.

同样,我们可以使用下表中给出的其他运算符。

比较运算符及其优先级

Operator Description Example Output -eq Equal to 2 -eq 2 True | -ne Not equal to 4 -ne 5 True -gt Greater than 4 -gt 5 False -ge Greater than or equal to 3 -ge 3 True -lt Less than 3 -lt 4 True -le Less than or equal to 1 -le 1 True -like Wildcard match "John" -like "*eh*" True -notlike Does not match wildcard "John" -notlike "*xyz*" True -match Regular expression match "John" -match ".*eh.*" True -notmatch Does not match regular expression "Hi" -notmatch ".*abc.*" True -contains Contains an item (for arrays) @(2,4,3) -contains 3 True -notcontains Does not contains an item (for arrays) @(2,1,3) -notcontains 4 True -in Is contained in the value (for arrays) 2 -in @(2,4,3) True -notin Is not contained in the value (for arrays) 5 -notin @(1,2,3) True

所有比较运算符都返回一个布尔值 (True/False),如果满足条件则返回 True;否则,False。关键是如果我们一次有多个运算符,将首先评估哪个运算符。在这种情况下,将根据下面给出的从最高到最低的优先级对其进行评估。

    -eq -ne -gt -ge -lt -le
    -like -notlike
    -match -notmatch
    -in -notIn
    -contains -notcontains

上面这组比较运算符具有相同的优先级。请注意,它们区分大小写(以 c 为前缀,例如 -cmatch)并显式不区分大小写(以 i 为前缀,例如 -imatch) 变体也具有相同(相同)的优先级。具有最高优先级(优先级)的运算符将首先被评估,而具有相同优先级的运算符将从左到右按照它们出现在给定表达式中的顺序进行评估。

在上表中的示例中,使用 @() 表示的数组运算符来创建数组。

逻辑和算术运算符及其优先级

Operator Description Example Output -and Logical AND 5 -gt 3 -and 5 -gt 4 True -or Logical OR 4 -gt 6 -or 8 -gt 5 True -xor Logical XOR 5 -gt 6 -xor 6 -gt 5 True -not Logical NOT -not 4 -gt 5 False -band Bitwise AND 5 -band 3 1 -bor Bitwise OR 5 -bor 3 7 -bnot Bitwise NOT -bnot 6 -7 -bxor Bitwise XOR 6 -bxor 3 5

所有逻辑运算符返回一个布尔值,如果满足指定条件则返回 True;否则,False。另一方面,算术运算符(按位运算符)返回基于运算符的数值。这些运算符也具有优先级,如下所示从最高优先级到最低优先级。

    -band -bnot -bor -bxor
    -and -or -xor

上述一组运算符具有相同的优先级。因此,优先级最高的运算符将首先被评估。相反,具有相同优先级的运算符将在给定表达式中出现时从左到右进行评估。

-not 在所有比较、算术和逻辑运算符中具有最高优先级。同样重要的是要注意,比较运算符将比算术和逻辑运算符更早进行计算,因为它们具有最高的优先级。您还可以使用括号控制计算顺序。
您可以在此处检查运算符的优先级。

这就是如何在 PowerShell 中的 if 语句中组合多个条件。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯