PowerShell 条件判断和循环
2015-10-01 04:28
651 查看
对于多数据处理,条件判断和循环是常用的。下面简单列出基本用法。
比较运算符:
布尔运算符:
布尔运算:
数组运算:
更多参考:Chapter 7. Conditions
IF/ELSE 条件判断语句:
SWITCH 条件判断语句:
ForEach-Object 逐项处理数据:
FOREACH 循环语句 :
Do While 循环语句 / While 循环语句 / For 循环语句 :
比较运算符:
-eq :等于 -ne :不等于 -gt :大于 -ge :大于等于 -lt :小于 -le :小于等于 -contains :包含 -notcontains :不包含
布尔运算符:
-and :和 -or :或 -xor :异或 -not :逆 !($var)
布尔运算:
1 -eq 2 1,2,3,2 -contains 2 ("A","B","C") -notcontains "D"
数组运算:
1,2,3,2 -eq 2 1,2,3,2 -ne 2 1,2,3,2 -ge 2
更多参考:Chapter 7. Conditions
Operator | Conventional | Description | Example | Result |
-eq, -ceq, -ieq | = | equals | 10 -eq 15 | $false |
-ne, -cne, -ine | <> | not equal | 10 -ne 15 | $true |
-gt, -cgt, -igt | > | greater than | 10 -gt 15 | $false |
-ge, -cge, -ige | >= | greater than or equal to | 10 -ge 15 | $false |
-lt, -clt, -ilt | < | less than | 10 -lt 15 | $true |
-le, -cle, -ile | <= | less than or equal to | 10 -le 15 | $true |
-contains, -ccontains, -icontains | contains | 1,2,3 -contains 1 | $true | |
-notcontains, -cnotcontains, -inotcontains | does not contain | 1,2,3 -notcontains 1 | $false |
Operator | Description | Left Value | Right Value | Result |
-and | Both conditions must be met | True False False True | False True False True | False False False True |
-or | At least one of the two conditions must be met | True False False True | False True False True | True True False True |
-xor | One or the other condition must be met, but not both | True False False True | True False True False | False False True True |
-not | Reverses the result | (not applicable) | True False | False True |
$a=2 if($a -gt 1) {"$a 大于 1"} if($a -gt 1) {"$a 大于 1"} else {"$a 小于 1"} if($a -gt 1) {"$a 大于 1"} elseif ($a -lt 1){"$a 小于 1"} else {"1等于1"}
SWITCH 条件判断语句:
#一般执行语法 $a=10 switch($a) { 1 {"return 1"} 2 {"return 2"} 3 {"return 3"} Default {"else return"} } #多个符合的条件都执行 $a=10 switch($a) { 1 {"return 1"} 2 {"return 2"} 10 {"return 10"} {$_ -gt 5 }{ "小于5" } {$_ -lt 100 }{ "小于100" } {($_ % 2) -eq 0}{ "余2为0" } Default {"else return"} }
ForEach-Object 逐项处理数据:
#逐项输出服务项 Get-Service | ForEach-Object{$_} #逐项查看服务中列 Name ,如包含“SQL”字符串,则输出服务中的列 DisplayName Get-Service | ForEach-Object{if ($_.Name -like "*SQL*"){$_.DisplayName}} #输出当前目录下最近一天更改过的文件 Get-ChildItem | ForEach-Object{if ($_.LastWriteTime -gt (Get-Date).AddDays(-1)){$_}}
FOREACH 循环语句 :
$array=1..10 foreach ($n in $array){ $n } foreach ($n in $array){ $sum+=$n } ; $sum
foreach($file in dir c:\windows) { if($file.Length -gt 1mb) { $File.Name } }
Do While 循环语句 / While 循环语句 / For 循环语句 :
#Do While 循环语句 $n=1 do { $sum2+=$n ; $n++ } while( $n -le 10); $sum2 #While 循环语句 $n=1 while($n -le 10){ $sum3+=$n ; $n++ } ; $sum3 #For 循环语句 for($n=1;$n -le 10;$n++){ $sum4+=$n } ; $sum4
相关文章推荐
- shell中trap捕捉到信号的处理
- Linux Shell编程记录
- shell 转换unix时间
- [转载]Shell删除各种注释的脚本
- [转载]【Shell脚本】逐行处理文本文件
- shell——html抓取链接
- [转载]Shell正则表达式
- shell脚本单实例互斥
- shell实现的计算器
- PowerShell 数组(Array)
- Adb shell得到root才干
- PowerShell
- bash脚本编程(待续)
- 了解bash shell及其基本使用
- PowerShell 变量(Variable)
- linux shell 自定义函数
- shell——wget命令
- linux shell “(())” 双括号运算符使用
- linux下创建shell脚本
- XShell连接不了虚拟机linux Could not connect to '127.0.0.1' (port 22): Connection failed.