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

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

作者:精品下载站 日期:2024-12-14 04:58:00 浏览:14 分类:玩电脑

PowerShell 中的逻辑运算符 |逻辑运算符列表及示例


[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

PowerShell 中的逻辑运算符简介

我们在 if 语句中看到了很多条件,例如 if($j -lt 10),但是如果我们想一次检查多个条件,例如 $j -lt 10 和 $i -lt 15 。所以我们有逻辑运算符来应对这种情况。 PowerShell 中的逻辑运算符将两个或多个表达式和语句组合在一起。简而言之,如果我们想在单个条件中转换多个条件,那么我们可以在 PowerShell 中使用逻辑运算符。让我们将逻辑运算符称为语法名称 LO。

语法:

if(cond1 LO1 cond2  LO2 cond3){
Statement 1
Statement 2
...
}

因此,在上述语法 (cond1 LO1 cond2 LO2 cond3) 中组合起来形成一个条件。

在上述语法中 (cond1 LO1 cond2 LO2 cond3)=单个条件。因为它将返回多个条件的单个值组合。

PowerShell 中逻辑运算符的示例

以下是Powershell中逻辑运算符的示例详细解释。

例子#1

在此示例中,我们将三个条件全部组合起来,($a -gt $b) =一个条件,(($a -lt 20) -or ($b -lt 20))=一个条件通过两个组合和 ($a -gt $b) -and (($a -lt 20) -or ($b -lt 20)) =一个条件,通过这三个条件的组合,我们得到一个条件。因此,如果所有这些条件都为真,则只会显示输出“所有组合条件都为真”。

代码:

$a =14
$b=12
if(($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
){
Write-Output “all combined conditions are true”
}

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

例子#2

在此示例中,我们将三个条件全部组合起来,($a -gt $b) =一个条件,(($a -lt 20) -or ($b -lt 20))=一个条件,($a -gt $b) -and (($a -lt 20) -or ($b -lt 20)) =一个条件,通过这三个条件的组合,我们得到一个条件。在这里我们可以看到我们将所有三个条件组合成一个条件,它们的输出将是单一的。

代码:

$a =20
$b=21
if(($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
){
Write-Output “all combined conditions are true”
}else{
Write-Output “all combined conditions are false”
}

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

PowerShell 中的逻辑运算符列表

PowerShell中有5个主要的逻辑运算符,它们是“and”、“or”、“xor”、“not=(!)”。让我们通过例子来简要讨论每一个。

1) - 和运算符

and 称为逻辑与,如果 $a 和 $b 为 True,则任何逻辑 and 的输出为 True,否则为 False,下面是逻辑与运算符的一些示例。

代码:

$a -and $b //false (if both are false)
$a -and $b  //false (if any one of them is false)
$a -and $b //true (if both of them are true)

所以基本上只有当两者都为真时,逻辑和运算符才为真。下面给出了执行上述示例的屏幕。

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

一般来说,我们希望使用的运算符应满足所有条件。例如,假设在一个班级中,老师决定只允许出勤人数超过 100 人的学生参加考试,并且他们支付了学费。所以这里两个条件都需要满足。

代码:

$attendance =101
$paid =”yes”
if($attendance -gt 100 -and $paid -eq “yes”){
Write-Output “Allow him for examination”
}

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

我们还可以通过传递 $attendance 和 $paid 的不同输入值来测试该程序。

2) -或运算符

逻辑或,如果 $a 和 $b 为 False,则为 False,否则下面给出一些示例:

代码:

$a -or $b //false(if both are false)
$a -or $b //true (if any one of them is true)
$a -or $b  //true (if both of them are true)

所以基本上只有当两者都为假时,逻辑和运算符才为假。下面给出了执行上述示例的屏幕。

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

一般来说,当我们想要考虑任何条件为真时,例如参加超过 100 分的学生将获得额外 5 分或得分超过 200 分的学生,则使用 or 运算符。

代码:

$attendance =101
$marks =201
if($attendance -gt 100 -or $marks -gt 200){
Write-Output “give 5 marks extra”
}

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

我们还可以通过传递 $attendance 和 $paid 的不同输入值来测试该程序。

3) -异或运算符

逻辑异或,如果 $a 或 $b 为 True,则为 True,否则

代码:

(‘a’ -eq ‘A’) -xor (‘a’ -eq ‘z’) //true as one of them is true
(‘a’ -eq ‘A’) -xor (‘Z’ -eq ‘z’)//false as one of them  is false
(‘a’ -eq ‘s’) -xor (‘Z’ -eq ‘p’) //false as both of them are false

下面的屏幕显示了上面示例的输出,

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

4) -非操作员

逻辑非,如果 $a 为 False,则为 True,否则

代码:

-not (‘a’ -eq ‘a’)  //false as 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

上述示例的屏幕如下所示,

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

5)!操作员

这 !运算符与 -not 运算符相同。简单地!运算符将 true 转换为 false,将 false 转换为 true。

代码:

!(‘a’ -eq ‘a’)  //false as 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

上述示例的屏幕如下所示,

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

代码:

$a =false
$b=true
!$a //true
!($b) //true
$a=$false
$b=$true
!($a) //true
!($b) //false

上述示例的屏幕如下所示,

输出:

[玩转系统] PowerShell 中的逻辑运算符 |逻辑运算符列表及示例

一些将所有运算符混合在一起的现实示例,

假设我们的服务器和数据库正在运行,并且我们希望实施某些检查,它将始终检查服务器和数据库是否正在运行。

代码:

if($server -eq “running” -and $database -eq “running”){
Write-Output “server is running and database is running”
}elseif($server -eq “not running” -and $database -eq “running”){
Write-Output “server is not running and database running”
}elseif($server -eq “running” -and $database -eq “not running”){
Write-Output “server is running and database not running”
}else{
Write-Output “server and database both are not running”
}

第一个输入:

$server =”not running”;
$database =”running”

第二个输入:

$server =”running”;
$database =”not running”

结论

总而言之,如果没有逻辑运算符,我们的编程将是一片空白,因为只有逻辑运算符我们才能够编写情景代码,我们才能够处理不同的情况。

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

取消回复欢迎 发表评论:

关灯