我正在为我儿子写的剧本有问题。我的意图是提醒他记住他的家务。我刚刚开始做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
}
答案 0 :(得分:5)
就像你发现自己一样,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中的非null /零长度字符串为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
更改。如果切换日中没有条款,那么默认值只是$choreOther
。
一周中的日子也可以表示为整数,就像你在上面看到的那样。这可能会降低代码的可读性,但我认为这是一个延伸。
答案 1 :(得分:1)
以下是完整的代码更正并按我的意愿工作。
## 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
}
答案 2 :(得分:1)
您还可以使用Hashtables来处理杂项列表和相关日期。以下内容可让您轻松地在任何一天添加家务(甚至可以在一天内完成多件家务)
$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
块,它只会列出当天的琐事。