英文:
comparing integer with multiple conditions in powershell
问题 {#heading}
抱歉,我只能回答你的翻译请求。以下是翻译好的内容:
抱歉对于这个"愚蠢"的问题,但我要疯了!我需要理解一个变量是否存在,如果存在的话,还要检查它的值是否不是0。我可以使用多个条件,还是必须使用多个语句像是elseif呢?
[int]$AllDbAvail = 0 #(在这种情况下,我已经定义了变量)
[int]$int0 = 0
if ( ( Test-Path variable:AllDbAvail ) -or ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
输出:
PS > not ok
另一个使用 @Mathias 方法的测试:
Remove-Variable AllDbAvail
if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
输出:
not ok
`[int]$AllDbAvail = 0
if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
`
输出:
not ok
$PSVersionTable.PSVersion
主要 次要 构建 修订
----- ----- ----- --------
5 1 17763 3770
也尝试过:
主要 次要 补丁 预发行标签 构建标签
----- ----- ----- --------------- ----------
7 2 1
结果相同。
对于PowerShell来说,"数字0"
并不是一个值!或者更确切地说,在某些情况下才是一个值!
并且,如果需要将数字0作为字符串表示,可以在没有[int]
前缀的情况下创建一个int
,需要用双引号 "0"
表达:
Sorry for the "stupid" question but I'm going crazy!
I need to understand if a variable exist and, if exist, checking also if its value isn't 0.
I can use multiple conditions, or I've to use multiple statements like elseif?
[int]$AllDbAvail = 0 #(in this case I've defined variable)
[int]$int0 = 0
if ( ( Test-Path !variable:AllDbAvail ) -or ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
`PS >not ok
`
Another test with @Mathias approch:
PS > Remove-Variable AllDbAvail
PS > if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
not ok
PS > [int]$AllDbAvail = 0
PS > if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
not ok
$PSVersionTable.PSVersion
Major Minor Build Revision
*** ** * ** ***
`5 1 17763 3770
`
tried also on:
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 2 1
Same result.
For powershell "number 0"
isn't a value! Or better, is a value only in certain circumstances!
And an int
can be created without the [int]
prefix, if number 0 have to be a string needs to be expressed with double quotes "0"
:
答案1 {#1}
得分: 1
需要从变量提供程序路径中删除 !
并将 -or
更改为 -and
:
if ((Test-Path variable:AllDbAvail) -and ($AllDbAvail -ne 0)) {
"ok"
}
else {
"not ok"
}
话虽如此,需要测试变量的存在有点不太合适 - 你可能需要重新考虑你的方法
正如Nico在评论中指出的,你可以大大简化条件:
if ($AllDbAvail) {
"ok"
}
else {
"not ok"
}
一个未被赋值的变量将评估为 $null
- 而 $null
和整数值 0
都将在转换为布尔值时评估为 $false
。
关于你的 "second test",我不太确定要说什么 - 它的行为正如预期的那样。
你尝试指定一个非零值了吗?
You need to remove !
from the variable provider path and change -or
to -and
:
if ((Test-Path variable:AllDbAvail) -and ($AllDbAvail -ne 0)) {
"ok"
}
else {
"not ok"
}
That being said, having to test for the existence of a variable is a bit of a code smell - you might want to rethink your approach
As Nico points out in the comments, you can simplify the condition significantly:
if ($AllDbAvail) {
"ok"
}
else {
"not ok"
}
A variable that hasn't been assigned to will evaluate to $null
- and both $null
and the integer value 0
will in turn evaluate to $false
when converted to a boolean.
With regards to your "second test", I'm not really sure what to say - it's behaving exactly as expected.
Did you try specifying a non-zero value?