Using the Switch Statement

powershell 的 swith 语句强大的令人无法相信哈

1. 最基本的语句

$a = 5

switch ($a)    

 {  

         1 {"The color is red."}

         2 {"The color is blue."} 

         3 {"The color is green."}

         4 {"The color is yellow."}

         5 {"The color is orange."}

         6 {"The color is purple."}

         7 {"The color is pink."} 

         8 {"The color is brown."}

         default {"The color could not be determined."}  

  }

========================================================

2. 多值的时候

$a = 5

switch ($a)   

         {($_ -eq 1) -or ($_ -eq 2) -or ($_ -eq 3)}

            {"The color is red."}

         4 {"The color is yellow."}

         5 {"The color is orange."}

         6 {"The color is purple."}

         7 {"The color is pink."}

         8 {"The color is brown."}

         default {"The color could not be determined."} 

  }

Note: case 可以试条件表达式的 {($_ -eq 1) -or ($_ -eq 2) -or ($_ -eq 3)}

在如:$myNumber = 3

$arrA = 1, 3, 5, 7, 9

$arrB = 2, 4, 6, 8, 10

switch ($myNumber)

{ 

    {$arrA -contains $_} { write-host "Odd" } 

    {$arrB -contains $_} { write-host "Even" }

}

=========================================================

3. 可以用wildcard 参数

如:

$a = "d14151"

switch -wildcard ($a) 

    {

         "a*" {"The color is red."}

         "b*" {"The color is blue."}

         "c*" {"The color is green."} 

         "d*" {"The color is yellow."}

           "h*" {"The color is brown."}

         default {"The color could not be determined."}  

  }

在如:

$a = "d14151"

switch -wildcard ($a)    

{

         "?14150" {"The color is red."}

         "?14151" {"The color is blue."} 

        "?14152" {"The color is green."}   

      "?14153" {"The color is yellow."}   

         default {"The color could not be determined."}  

  }

=======================================================================

4. 使用正则表达式

如:一个变量,以字母开头后面跟写数字,开头的a,b,c,d 决定一种颜色,这时就要用正则表达式才行了

$a = "r14151"

switch -regex ($a)    

 { 

        "[a-d]" {"The color is red."}

         "[e-g]" {"The color is blue."}  

       "[h-k]" {"The color is green."} 

        "[l-o]" {"The color is yellow."} 

        "[p-s]" {"The color is orange."} 

        default {"The color could not be determined."}

    }

在如

$a = "14151"

switch -regex ($a)    

{       

         "\d{5}" {"The color is yellow.”; break}  

       "\d{4}" {"The color is orange.”; break}  

       "\d{3}" {"The color is purple.”; break} 

        "\d{2}" {"The color is pink.”; break}    

    "\d{1}" {"The color is brown.”; break}  

       default {"The color could not be determined."}   

 }

#Result PS>The color is yellow

===========================================================

5. 用 Arrays $a = 21, 38, 6

switch ($a)    

        1 {"The color is red."}

         2 {"The color is blue."}

         3 {"The color is green."} 

        4 {"The color is yellow."}

         5 {"The color is orange."}

         6 {"The color is purple."} 

        7 {"The color is pink."} 

       8 {"The color is brown."}  

   }

#Result PS>The color is purple.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值