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

[玩转系统] PowerShell Switch 声明 | PowerShell Switch 语句指南

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

PowerShell Switch 声明 | PowerShell Switch 语句指南


[玩转系统] PowerShell Switch 声明 | PowerShell Switch 语句指南

PowerShell Switch语句简介

PowerShell 中的 switch 语句与其他编程语言中的 switch 语句相同。 switch 语句用于评估预定义的条件列表,换句话说,它相当于多个 if 语句。 switch 语句具有一系列条件,每个条件都有其自己要执行的操作。每当一个条件匹配时,就会执行相应的操作。 switch 语句内的每个条件都被视为一种情况,并且针对每种情况检查执行 switch 语句的值。本文将详细介绍 switch case 语句。有一个默认块来处理与任何情况都不匹配的值。

句法

switch 语句的基本语法/结构如下

switch(<valueToBeChecked>) {
<condition> {<actionToBePerformed>}
break;
<condition> {< actionToBePerformed >}
break;
<condition> {< actionToBePerformed >}
break;
Default {< actionToBePerformed >}
}

代码:

switch (Jan)
{
Jan  {"It is Jan."}
Feb  {"It is Feb."}
Mar  {"It is Mar."}
Apr  {"It is Apr."}
}

上面将产生“It is Jan”的输出。

语法#1

switch语句的其他详细语法如下

switch [-regex|-wildcard|-exact][-casesensitive] (<value>)
{
"string"|number|variable|{ expression } { statementlist }
default { statementlist }
}

语法#2

switch [-regex|-wildcard|-exact][-casesensitive] -file filename
{
"string"|number|variable|{ expression } { statementlist }
default { statementlist }
}

PowerShell Switch 语句流程图

下面是流程图:

[玩转系统] PowerShell Switch 声明 | PowerShell Switch 语句指南

有关 Switch 语句的关键事项:

  • 对象或对象数组都可以用作开关变量。
  • switch 中可以有任意数量的 case 语句。默认和操作块是可选的。
  • switch 变量和 case 变量必须具有相同的数据类型。它应该是常量或文字。
  • 每当一个 case 匹配时,就会执行后面的语句,直到遇到break语句。
  • 当遇到break语句时,switch语句终止。控制权转移到中断之后的下一个语句。
  • 不需要每个case都有break,如果没有break,则执行后续语句,直到遇到break。

参数

以下是开关的参数。

  • 通配符:这表示正在检查的条件是通配符字符串。如果匹配子句不是字符串类型,则不考虑相应的参数。这是不区分大小写的比较。
  • 精确:表示必须存在精确匹配。 如果匹配子句不是字符串类型,则不考虑相应的参数。它不区分大小写。
  • 区分大小写:它表示区分大小写的匹配。如果匹配子句不是字符串类型,则不考虑相应的参数。
  • 文件:输入取自文件。如果指定了多个文件,则仅使用最后一个文件进行比较。每一行都由 switch 语句进行评估。它不区分大小写。
  • Regex:表示匹配时必须考虑正则表达式。如果匹配子句不是字符串类型,则不考虑相应的参数。

实施 PowerShell Switch 语句的示例

下面是提到的例子:

例子#1

代码:

Write-Host "Demo of switch statement in PowerShell"
$name="vignesh"
switch($name)
{
"ram" {"my name is ram";break}
"raman" {"my name is raman";break}
"ramu" {"my name is ramu";break}
"Vignesh" {"my name is Vignesh";break}
"ram123" {"my name is ram123";break}
"ram45" {"my name is ram454";break}
"ramfdgdf" {"my name is ramdfgfdg";break}
"ramfgdf" {"my name is ramdgfdg";break}
"ramfgdfg" {"my name is ramdgg";break}
}
Write-Host "Demo with default block"
$city="NewYork"
switch($city)
{
"Tn" {"city name is TN";break}
"as" {"city name is as";break}
"sd" {"city name is sd";break}
"gf" {"city name is gf";break}
"gujarat" {"city name is gujarat";break}
"wqe" {"city name is wqe";break}
"ewrewr" {"city name is ewrewr";break}
"wer" {"city name is wer";break}
"ewr" {"city name is ewr";break}
default {"city is not present in the list";break}
}

输出:

[玩转系统] PowerShell Switch 声明 | PowerShell Switch 语句指南

例子#2

代码:

Write-Host "Demo of switch statement with array as input in PowerShell"
$name="vignesh"
$name1="Krishnakumar"
$name2="Nandhini"
$name3="Vyapini"
switch($name,$name1,$name2,$name3)
{
"Krishnakumar" {"my name is Krishnakumar";break}
"Nandhini" {"my name is Nandhini";break}
"ramu" {"my name is ramu";break}
"Vignesh" {"my name is Vignesh";break}
"ram123" {"my name is ram123";break}
"ram45" {"my name is ram454";break}
"ramfdgdf" {"my name is ramdfgfdg";break}
"ramfgdf" {"my name is ramdgfdg";break}
"Vyapini" {"my name is Vyapini";break}
}
Write-Host "Demo with default block"
$city="NewYork"
$city1="Tn"
$city2="gujarat"
$city3="Texas"
$city4="albama"
switch($city,$city1,$city2,$city3,$city4)
{
"Tn" {"city name is TN"}
"as" {"city name is as"}
"sd" {"city name is sd"}
"gf" {"city name is gf"}
"gujarat" {"city name is gujarat"}
"Texas" {"city name is texas"}
"ewrewr" {"city name is ewrewr";}
"wer" {"city name is wer"}
"NewYork" {"city name is NewYork"}
default {"city is not present in the list";break}
}

输出:

[玩转系统] PowerShell Switch 声明 | PowerShell Switch 语句指南

说明:在上面的示例中,如果仔细观察,会发现第一个 switch 语句中只打印了第一个匹配项。这是因为一旦找到匹配,就会执行相应的break语句,并退出switch case。在第二个 switch case 中,找到了所有匹配的值,因为没有为每个 case 编写break语句。

例子#3

代码:

write-host "Demo of execution with and without break statement"
Write-Host "Demo of execution without break statement" -ForegroundColor Yellow
$Pm="Modi"
switch($pm)
{
"Modi" {"PM is Modi"}
"Indira gandhi" {"PM was Indira gandhi"}
"vallu" {"PM was Indira gandhi"}
"test" {"PM was Indira test"}
default {"none"}
"Modi" {"execution is done even after first match"}
}
Write-Host "Execution of switch with break statement" -ForegroundColor Yellow
switch($pm)
{
"Modi" {"PM is Modi";break}
"Indira gandhi" {"PM was Indira gandhi;break"}
"vallu" {"PM was Indira gandhi;break"}
"test" {"PM was Indira test;break"}
default {"none;break"}
"Modi" {"execution is done even after first match;break"}
}

输出:

[玩转系统] PowerShell Switch 声明 | PowerShell Switch 语句指南

解释:在上面的输出中,相同的 switch case 与相同的输入参数一起使用,只是第一个 switch case 没有break语句。从上面可以看出,如果不使用中断,即使在成功匹配后也会继续执行,而使用中断时不会发生这种情况。

结论

因此,本文详细介绍了 PowerShell 中的 switch case 语句。它还通过适当的示例解释了 switch case 语句的执行及其语法。了解更多信息的最佳方法是尝试其他各种方法并在示例脚本中进行练习。

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

取消回复欢迎 发表评论:

关灯