【问题标题】:If/ElseIf block not working with -orIf/ElseIf 块不与 -or 一起使用
【发布时间】:2015-08-19 00:29:45
【问题描述】:

我为儿子编写的脚本有问题。我的意图是简单地提醒他记住他的家务。我最近才开始做 PowerShell,我真的很喜欢它。我买了几本书,并浏览了许多其他帖子。

到目前为止,我得到的信息如下,如果评估无法与 -or 一起正常工作(或者我搞砸了?)

    ## REMINDERS TO DO CHORES ##

$sun = "Sunday"
$mon = "Monday"
$tue = "Tuesday"
$wed = "Wednesday"
$thu = "Thursday"
$fri = "Friday"
$sat = "Saturday"

$today = (get-date).DayOfWeek

$choreVac = "Vacuum the rooms and stairs"
$choreBath = "Clean the Bathroom Including emptying the garbage"
$choreOther = "No Chores Today -- But keep dishes done up"


if($today -eq $mon -or $wed -or $fri) {
msg /time:2500 * "Today is a Chore Day:  your job is to $choreVac" 
}

elseif ($today -eq $tue -or $sat ) {
msg /time:2500 * "Today is a Chore Day: your job is to $choreBath and PLEASE do a good job"
}
else {
msg /time:2500 * $choreOther
}

问题是我不认为它在当天被正确评估,所以今天是星期二,评估结果是$mon -or $wed -or $fri

如果我按如下方式为每一天重新编码,它会像预期的那样工作。为什么它不适用于-or

if($today -eq $tue) {
msg /time:2500 * $choreBath
}

【问题讨论】:

  • 啊,我刚刚想通了。如果 ($today -eq $mon -or $today -eq $wed -or $today -eq $fri) { 它有效,我必须完全限定它
  • 随时发布您的问题的答案,详细说明您找到的解决方案。
  • 你不应该在上面的问题中提供答案 :) 我已经删除了那部分。
  • @dan-gph 谢谢丹,我还没有能力为你的 cmets 投票,但不用担心。我很感激。

标签: powershell windows-10 powershell-ise powershell-5.0


【解决方案1】:

就像您发现自己一样,PowerShell 并未评估您的 if 语句您的意图。你的表达可以这样理解:

if(($today -eq $mon) -or ($wed) -or ($fri))

正如您的评论中您想要的代码是

$today -eq $mon -or $today -eq $wed -or $today -eq $fri

或另一种看待它的方式。

($today -eq $mon) -or ($today -eq $wed) -or ($today -eq $fri)

PowerShell 不需要括号,但如果事情不顺利,最好使用括号。

当 PowerShell 中的非空/零长度字符串为 true 时,当转换为布尔值时。着眼于第二个子句,它可以重写为

"Wednesday" -or "Friday"

总是true。这就是为什么您的 if 声明在您没想到时会触发的原因。

您编写的代码具有一定的逻辑意义,但在语法上是不正确的。如果您还不熟悉,我想向您介绍的另一种方法是switch。这将有助于减少所有if 语句的混乱,如果随着时间的推移它们变得更加复杂,它将特别有用。

$today = (get-date).DayOfWeek

$choreVac = "Vacuum The Apt"
$choreBath = "Clean the Bathroom Including empting the garbage"
$choreOther = "NO CHORES TODAY -- BUT YOU CAN Keep dishes done up, and Keep Garbage from Overflowing AND CLEAN YOUR ROOM and OR Do Laundry!!!. Especially your bedding"

Switch ($today){
    {$_ -in 1,3,5}{$message = "Today is a Chore Day:  Your job is to`r$choreVac"}
    {$_ -in 2,6}{$message = "Today is a Chore Day:  Your job is to`r$choreBath and PLEASE do a good job"}
    default{$message = $choreOther}
}

msg /time:2500 * $message

我们将所有对 msg 的调用删除到一个语句中,因为只有 $message 发生了变化。如果 switch 中的子句未涵盖家务活,则默认值为 $choreOther

一周中的日子也可以用整数表示,就像你在上面看到的一样。这可能会降低代码的可读性,但我认为这是一个延伸。

【讨论】:

  • @Matt 谢谢,开关很棒。它看起来更像是我在 PL/SQL 中使用过的 case 语句,或者我在 C/C++ 等其他语言中看到的,这也很有趣。 +1
【解决方案2】:

这是完整的代码,已更正并按我的意愿工作。

    ## REMINDERS TO DO CHORES ##

$sun = "Sunday"
$mon = "Monday"
$tue = "Tuesday"
$wed = "Wednesday"
$thu = "Thursday"
$fri = "Friday"
$sat = "Saturday"

$today = (get-date).DayOfWeek

$choreVac = "Vacuum The Apt"
$choreBath = "Clean the Bathroom Including empting the garbage"
$choreOther = "NO CHORES TODAY -- BUT YOU CAN Keep dishes done up, and Keep Garbage from Overflowing AND CLEAN YOUR ROOM and OR Do Laundry!!!. Especially your bedding"

if($today -in ($mon, $wed ,$fri) ) {
msg /time:2500 * "Today is a Chore Day:  your job is to $choreVac" 
}

elseif ($today -in ($tue,$sat)) {
msg /time:2500 * "Today is a Chore Day: your job is to $choreBath and PLEASE do a good job"
}
else {
msg /time:2500 * $choreOther
}

【讨论】:

  • 你可以用-in操作符写得更简洁:if ($today -in ($mon, $wed, $fri)) { }
  • @dan-gph +10 我更喜欢这个!干净得多。干杯。
【解决方案3】:

您还可以使用哈希表来处理家务列表和相关日期。以下将允许您轻松地在任何一天添加家务(甚至在一天中放置多项家务)

$chores = @{Vac   = "Vacuum the Appt";
            Bath  = 'Clean the bathroom including emptying the garbage';
            Other = 'Nothing today -- But keep dishes done up'
           }

$day = @{Sunday    = $chores.Other;
         Monday    = $chores.Vac;
         Tuesday   = $chores.Bath;
         Wednesday = @($chores.Vac,$chores.Other);
         Thursday  = $chores.Other;
         Friday    = $chores.Vac;
         Saturday  = $chores.Bath;
        }

$base = "Chores for today: "
$today = (Get-Date).DayOfWeek
if ($day."$today".count -gt 1) {
    $first = "`n    " + $day."$today"[0]
    $rest = for ($i = 1; $i -lt $day."$today".count; $i++) {
        "`n    " + $day."$today"[$i]
    }

    $msg = $base + $first + $rest
} else {
    $msg = $base + "`n    " + $day."$today"
}

msg /time:2500 * $msg

如果您不想支持一天做多项家务(上面适用于星期三),只需将整个 If/Else 块替换为 else 子句,它就会简单地列出天。

【讨论】:

    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 2016-10-05
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多