什么特殊的性格!在powershell中意味着什么?

use*_*560 5 powershell special-characters

什么是特殊字符"!" 在powershell中意味着什么?或者列出所有特殊字符和含义的网站.例如:$ string = blah!$ String(返回$ false)

mjo*_*nor 8

Powershell使用了!character作为logical-not运算符的别名.

$true
!$true
$false
!$false

True
False
False
True
Run Code Online (Sandbox Code Playgroud)


Mic*_*rgl 5

PowerShell 将所有空、$Null 或 0 解释为布尔值 $False。Bool 只能有 $True 或 $False。

通过将值转换为布尔值,您可以查看 PowerShell 对每个值的解释:

[bool]0        # False
[bool]1        # True
[bool]""       # False
[bool]"test"   # True
[bool]$null    # False
Run Code Online (Sandbox Code Playgroud)

局部 NOT 运算将每个布尔值转换为其相反数:

!$True   # Is $False
!$False  # Is $True

![bool]0        # True
![bool]1        # False
![bool]""       # True
![bool]"test"   # False
![bool]$null    # True
Run Code Online (Sandbox Code Playgroud)

您将一个字符串分配给一个变量,然后检查它是否为空。

$string = blah
!$String         # $String is not $Null or Empty so it is $True
                 # But the !(NOT) operation turns it to $False
Run Code Online (Sandbox Code Playgroud)

编程语言中的条件和循环仅适用于布尔值。

因此,当获取用户输入时,您可以使用它来检查用户是否输入了文本,并对其做出反应:

$UserName = Read-Host -Prompt "Whats your Name Sir?"
If ($UserName) {
     Write-Output "Happy Birthday $UserName"
}
Else {
     Write-Output "I can't congratulate you as I don't know your name :("
}
Run Code Online (Sandbox Code Playgroud)