在PowerShell中比较整数与多个条件:

huangapple go评论143阅读模式
英文:

comparing integer with multiple conditions in powershell

问题

抱歉,我只能回答你的翻译请求。以下是翻译好的内容:

抱歉对于这个“愚蠢”的问题,但我要疯了!我需要理解一个变量是否存在,如果存在的话,还要检查它的值是否不是0。我可以使用多个条件,还是必须使用多个语句像是elseif呢?

  1. [int]$AllDbAvail = 0 #(在这种情况下,我已经定义了变量)
  2. [int]$int0 = 0
  3. if ( ( Test-Path variable:AllDbAvail ) -or ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}

输出:

  1. PS > not ok

另一个使用 @Mathias 方法的测试:

  1. Remove-Variable AllDbAvail
  2. if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}

输出:

  1. not ok
  2. [int]$AllDbAvail = 0
  3. if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}

输出:

  1. not ok
  1. $PSVersionTable.PSVersion
  2. 主要 次要 构建 修订
  3. ----- ----- ----- --------
  4. 5 1 17763 3770

也尝试过:

  1. 主要 次要 补丁 预发行标签 构建标签
  2. ----- ----- ----- --------------- ----------
  3. 7 2 1

结果相同。

对于PowerShell来说,“数字0”并不是一个值!或者更确切地说,在某些情况下才是一个值!

并且,如果需要将数字0作为字符串表示,可以在没有[int]前缀的情况下创建一个int,需要用双引号 “0” 表达:

在PowerShell中比较整数与多个条件:

英文:

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?

  1. [int]$AllDbAvail = 0 #(in this case I've defined variable)
  2. [int]$int0 = 0
  3. if ( ( Test-Path !variable:AllDbAvail ) -or ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
  4. PS >not ok

Another test with @Mathias approch:

  1. PS > Remove-Variable AllDbAvail
  2. PS > if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
  3. not ok
  4. PS > [int]$AllDbAvail = 0
  5. PS > if ( ( Test-Path variable:AllDbAvail ) -and ( $AllDbAvail -ne $int0 ) ) {"ok"} else {"not ok"}
  6. not ok

  1. $PSVersionTable.PSVersion
  2. Major Minor Build Revision
  3. ----- ----- ----- --------
  4. 5 1 17763 3770

tried also on:

  1. Major Minor Patch PreReleaseLabel BuildLabel
  2. ----- ----- ----- --------------- ----------
  3. 7 2 1

Same result.

在PowerShell中比较整数与多个条件:

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":

在PowerShell中比较整数与多个条件:

答案1

得分: 1

需要从变量提供程序路径中删除 ! 并将 -or 更改为 -and

  1. if ((Test-Path variable:AllDbAvail) -and ($AllDbAvail -ne 0)) {
  2. "ok"
  3. }
  4. else {
  5. "not ok"
  6. }

话虽如此,需要测试变量的存在有点不太合适 - 你可能需要重新考虑你的方法 在PowerShell中比较整数与多个条件:

正如Nico在评论中指出的,你可以大大简化条件:

  1. if ($AllDbAvail) {
  2. "ok"
  3. }
  4. else {
  5. "not ok"
  6. }

一个未被赋值的变量将评估为 $null - 而 $null 和整数值 0 都将在转换为布尔值时评估为 $false

关于你的 "second test",我不太确定要说什么 - 它的行为正如预期的那样。

你尝试指定一个非零值了吗?

在PowerShell中比较整数与多个条件:

英文:

You need to remove ! from the variable provider path and change -or to -and:

  1. if ((Test-Path variable:AllDbAvail) -and ($AllDbAvail -ne 0)) {
  2. "ok"
  3. }
  4. else {
  5. "not ok"
  6. }

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 在PowerShell中比较整数与多个条件:

As Nico points out in the comments, you can simplify the condition significantly:

  1. if ($AllDbAvail) {
  2. "ok"
  3. }
  4. else {
  5. "not ok"
  6. }

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?

在PowerShell中比较整数与多个条件:

huangapple
  • 本文由 发表于 2023年8月10日 21:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定