[英]How to navigate nested loops within Powershell
I am trying to do something of the following nature我正在尝试做以下性质的事情
if(...){
}
elseif(...){
if(...){
}
else{
...
}
}
else{
...
}
However, it seems that powershell does not like having both an if and and else statement within the elseif loop.但是,powershell 似乎不喜欢在 elseif 循环中同时使用 if 和 else 语句。 Any workarounds for this?
有什么解决方法吗? Thanks for your help!
谢谢你的帮助! I am really new to Powershell
我真的是 Powershell 的新手
I have tried switch statements but they do not make sense for what I am trying to do我已经尝试过 switch 语句,但它们对我正在尝试做的事情没有意义
Not an answer per se, but I tested the structure exactly as provided and it works just fine:本身不是答案,但我完全按照提供的方式测试了结构并且它工作得很好:
function iftest($a, $b, $c) {
if ($a) {
'$a true. $b and $c not tested.'
}
elseif ($b) {
if ($c) {
'$a false. $b true. $c true.'
}
else {
'$a false. $b true. $c false.'
}
}
else {
'$a false. $b false. $c not tested.'
}
}
# command # output
iftest $true $false $false # $a true. $b and $c not tested.
iftest $false $false $false # $a false. $b false. $c not tested.
iftest $false $true $false # $a false. $b true. $c true.
iftest $false $true $true # $a false. $b true. $c false.
As noted by mclayton in the comments, formatting goes a long way toward making the structure clear.正如 mclayton 在评论中指出的那样,格式化对使结构清晰大有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.