PowerShell 中的逻辑运算符
逻辑运算符可以将各种条件转换为单个条件。
本文将讨论真实世界的示例,并使用 PowerShell 在脚本中应用逻辑运算符。
PowerShell 逻辑运算符
逻辑运算符有 and
、or
、xor
和 not
或!
。
PowerShell 中的 -and
运算符
如果 $a
和 $b
为 true
,则输出为 true
;否则,false
。
真值表:
A | B | 输出 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
$a = 0
$b = 0
$a -and $b # false (if both variables are false)
$a = 1
$b = 0
$a -and $b # false (if any of the variables are false)
$a = 1
$b = 1
$a -and $b # true (if both variables are true)
-and
运算符仅在两者都为真时才返回 true
。一般来说,-and
运算符用于我们希望检查和满足所有条件的地方。
这是需要满足的两个条件的示例。
$attendance = 102
$paid = "Y"
if($attendance -gt 100 -and $paid -eq "Y"){
Write-Output "Allow for examination."
}
输出:
Allow for examination.
PowerShell 中的 -or
运算符
与 -and
运算符相比,如果 $a
和 $b
为 false
,则输出为 false
。
-or
运算符只需要一个变量为 true
即可输出 true
。
真值表:
A | B | 输出 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
$a = 0
$b= 0
$a -or $b # false (if both conditions are false)
$a = 1
$b = 0
$a -or $b # true (if any of the variables are true)
$a = 1
$b = 1
$a -or $b # true (if both of the variables are true)
-or
运算符仅在两个条件都为 false
时才返回 false
。通常,当将任何条件视为 true
时,会使用 -or
运算符。
$attendance = 99
$marks = 201
if($attendance -gt 100 -or $marks -gt 200){
Write-Output "Give five extra marks."
}
输出:
Give five extra marks.
PowerShell 中的 -xor
运算符
如果 $a
或 $b
中只有一个是 true
,则异或 or
或 -xor
是 true
的结果。如果两个条件都是 true
,-xor
会产生 false
的结果。
真值表:
A | B | 输出 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 0 |
('a' -eq 'A') -xor ('a' -eq 'z') # true as one of them is true
('a' -eq 'A') -xor ('Z' -eq 'z') # false as both of them is true
('a' -eq 's') -xor ('Z' -eq 'p') # false as both of them are false
PowerShell 中的 -not
运算符
-not
运算符返回与表达式输出相反的结果。如果表达式的输出为 true
,则运算符将其返回为 false
,反之亦然。
-not ('a' -eq 'a') # false as the output of expression is true
-not ('v' -eq 'a') # true as output expression is false
-not ('v' -eq 'V') # false as output expression is true
-not ('V' -eq 'V1') # true as output expression is false
感叹号!
与 -not
运算符相同。
!('a' -eq 'a') # false as the output of expression is true
!('v' -eq 'a') # true as output expression is false
!('v' -eq 'V') # false as output expression is true
!('V' -eq 'V1') # true as output expression is false
相关文章
在 Windows PowerShell 中获取命令行参数
发布时间:2024/03/04 浏览次数:358 分类:编程语言
-
本文将解释我们如何使用 PowerShell 的参数函数处理命令行参数,参数如何工作,我们如何使用 PowerShell 参数将值传递给参数,以及定义参数的基本方法是什么。
Windows PowerShell 中的 Base64 编码
发布时间:2024/03/04 浏览次数:332 分类:编程语言
-
本文将展示如何编码和解码 base64 字符串。Windows PowerShell 当前版本没有本机命令,因此我们将向你展示如何执行此操作的替代方法。
在 Windows PowerShell 中写入输出
发布时间:2024/03/04 浏览次数:259 分类:编程语言
-
本文将向你展示如何在 Windows PowerShell 中编写或打印输出。本文还将区分多个 write cmdlet 并解释它们的意义。
在 Windows PowerShell 中设置 PATH 环境变量
发布时间:2024/03/04 浏览次数:350 分类:编程语言
-
本文将展示在 Windows PowerShell 中设置路径环境变量的正确方法。本文还展示了如何在持久性和非持久性方法中设置变量。
在 Windows PowerShell 中打印环境变量
发布时间:2024/03/04 浏览次数:869 分类:编程语言
-
本文将讨论什么是环境变量以及如何使用多个 Windows PowerShell 命令打印它们。
在 PowerShell 中注释代码
发布时间:2024/03/04 浏览次数:363 分类:编程语言
-
本文将展示在 Windows PowerShell 中注释代码的多种方法。本文还介绍了注释代码时的一些最佳实践。
在 PowerShell 中执行 LDAP 查询
发布时间:2024/03/04 浏览次数:304 分类:编程语言
-
本文将深入了解如何使用 Active Directory 过滤器和 LDAP 过滤器。