powershell - Solved: Strange behaviour of string comparison in Powershell
问题描述
Consider the following function :
function myfunc()
{
if (condition1) {
return 'nowork'
} elseif (condition2) {
return $false
} elseif (condition3) {
return $true
}
Now if I call this function, and I know that condition3 is true, I can see that True is returned:
...
$result = myfunc
Write-Host $result
(this writes True to the console.)
The next statement in the calling function is an if statement to determine what was returned, and act upon that:
$result = myfunc
Write-Host $result
if ($result -eq 'nowork') {
do this..
} elseif ($result -eq $false) {
do that..
} elseif ($result -eq $true) {
do something else..
}
And this is where it gets strange (to me). Even though I can see that True is returned, the if statement decides to go do 'do this..', the first branch of the if statement, where I would have expected that 'do something else..' would have been done. Another strange thing is that it sometimes works, sometimes not. I tried changing the if statement to:
if ('nowork' -eq $result)
and then what went wrong first now worked, but later on the same issue re-appeared.
I'm guessing there's something wrong with my first string comparison, but I can't figure out what. I'm used to writing scripts in Linux (bash), so Powershell must be acting differently.
Btw: script is run in Debian 10, Powershell 7, but the exact same problem also appears on a Windows machine with Powershell 5.0.
Please help..
解决方案
你在比较苹果和橘子
PowerShell 的比较运算符行为取决于左侧操作数的类型。
当您的lhs ( $result
) 是[bool]
(ie. $true
or ) 时,PowerShell 将在比较两者之前$false
尝试将右侧操作数转换为 as well。[bool]
将非空字符串(即'nowork'
)转换[bool]
为$true
,因此if
条件计算为$true -eq $true
-> $true
。
您可以通过手动类型检查来解决此问题:
if($result -is [bool]){
if($result){
# was $true
}
else {
# was $false
}
}
elseif($result -eq 'nowork'){
# was 'nowork'
}
然而,解决这个问题的更好方法是始终返回相同类型的对象。如果您有 3 种不同的退货选项,请考虑enum
:
enum WorkAmount
{
None
Some
All
}
function myfunc()
{
if (condition1) {
return [WorkAmount]::None
} elseif (condition2) {
return [WorkAmount]::Some
} elseif (condition3) {
return [WorkAmount]::All
}
}
推荐阅读
- tensorflow - “Anaconda 无法生成新进程……”我有 sublime 文本错误
- r - r 合并命令返回列表,而不是数据框
- pyspark - 如何将数字转换为百分比
- c++ - 仅使用 libdb2 解析 xml 时的分段错误
- amazon-web-services - Kubernetes:指定或确定 aws 集群中的 pod 可用区
- c - 动态设置 MAX_STRINGS
- java - 使用 Selenium 网格和 jenkins 通过 Web 界面上传带有空手道测试的文件
- c++ - 三元运算符的意外结果
- mysql - 在 SQL 中选择不同的对
- charts - 如何获取Power BI日期切片器中选择的最小日期的对应价格