内容简介:PowerShell~语法与运算符
基本语法
变量:$开头
$a = "Hello"
条件语句:if else
if ($price -eq 2)
{
Write-Host "偶数"
}
else{
Write-Host "基数"
}
循环语句:for
for($i=0;$i -lt 3;$i++)
{
Write-Host "`$i=$i"
}
比较运算符
| 说 | 示例 | 结果 | |
| -eq | 等于。包含一个相同的值。 |
1 -eq 1 "abc” -eq “abc” 1,2,3 -eq 2 1,2,3,1,2,3,1,2,3 –eq 2 "abc”,"def” -eq “abc |
true true 2 2,2,2 abc |
| -ne | 不等于。包含一个不同的值。 |
1 -ne 1 1,2,3 –ne 1 "abc”,"def” –ne “abc” |
false 2,3 def |
| -gt | (>)大于 | 1 -gt 1 | false |
| -ge | (>=)大于或等于 | 1 -ge 1 | true |
| -lt | (<)小于 | 1 -lt 1 | false |
| -le | (<=)小于或等于 | 1 -le 1 | true |
| -like | 使用通配符 (*) 匹配。 | “abc” -like “*b*” | true |
| -notlike | 使用通配符 (*) 不匹配。 | “abc” -notlike “*b*” | false |
| -match | 当运算符的输入(左侧参数)是一个单标量对象时,-match 和 -notmatch 运算符填充 $Matches 自动变量。当输入是标量时,-match 和 notmatch 运算符返回布尔值,并将 $Matches 自动变量 的值设置为参数的匹配项目。 如果输入是一个集合,则 -match 和 -notmatch 运算符返回该集合中的匹配成员,但运算符不会填 充 $Matches 变量。 |
123 -match “^[\d]+$” "abc” -match “^[\w]+$” "abc”,123 –match “^[\d]+"$” |
true ($matches为123) true ($matches为abc) 123 ($matches为空) |
| -notmatch | 正则表达式比较 | 123 -notmatch “^[\d]+$” | false |
| -contains | 包含运算符。包含一个相同的完整值(而不是值的一部分)。始终返回布尔值。 |
"abc”,"def” -contains “abc” "abc”,"def” -contains “ab” "abc”,"def” -contains “ab*” |
true false false |
| -notcontains | 包含运算符。不包含一个相同值。始终返回布尔值。 | "abc”,"def” -notcontains “abc” | false |
| -replace | 替换,支持正则表达式 |
“abc“ -replace “b”,"-" "a1b2c3” -replace “\d”,"-" |
a-c a-b-c- |
逻辑运算符
| 说明 | 示例 | 结果 | |
| -and |
逻辑与。 仅当两条语句都为 TRUE 时才为 TRUE。 |
$true -and $false $true -and $true (1 -eq 1) -and (1 -eq 2) |
false true false |
| -or |
逻辑或。 当其中一条语句为 TRUE或两条语句都为 TRUE 时为 TRUE。 |
$true -or $false (1 -eq 1) -or (1 -eq 2) |
true true |
| -xor |
逻辑异或。 仅当一条语句为 TRUE而另一条语句为 FALSE 时才为 TRUE。 |
$true -xor $false $true -xor $true |
true false |
|
-not ! |
逻辑非。对后面的语句取反。 |
-not $true !$true |
false false |
以上所述就是小编给大家介绍的《PowerShell~语法与运算符》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- ES6—扩展运算符和rest运算符(6)
- C/C++三元运算符实际上是否具有与赋值运算符相同的优先级?
- Python 运算符
- Python算术运算符
- 004.Python运算符
- JavaScript③运算符
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Fluent Python
Luciano Ramalho / O'Reilly Media / 2015-8-20 / USD 39.99
Learn how to write idiomatic, effective Python code by leveraging its best features. Python's simplicity quickly lets you become productive with it, but this often means you aren’t using everything th......一起来看看 《Fluent Python》 这本书的介绍吧!