If/ElseIf 块不与 -or 一起工作
If/ElseIf block not working with -or
我为我儿子写的脚本有问题。我的意图是简单地提醒他记住他的家务。我最近才开始做 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
}
这是完整的代码,已更正并按我的要求工作。
## 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
}
就像您自己发现的那样,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/zero 长度字符串转换为布尔值时为 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
.
星期几也可以像您在上面看到的那样表示为整数。这可能会降低代码的可读性,但我认为这是一种延伸。
您还可以使用哈希表来处理杂务列表和相关日期。以下内容将允许您轻松地在任何给定的一天添加家务(甚至可以在一天内添加多项家务)
$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
子句即可简单列出一天的琐事。
我为我儿子写的脚本有问题。我的意图是简单地提醒他记住他的家务。我最近才开始做 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
}
这是完整的代码,已更正并按我的要求工作。
## 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
}
就像您自己发现的那样,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/zero 长度字符串转换为布尔值时为 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
.
星期几也可以像您在上面看到的那样表示为整数。这可能会降低代码的可读性,但我认为这是一种延伸。
您还可以使用哈希表来处理杂务列表和相关日期。以下内容将允许您轻松地在任何给定的一天添加家务(甚至可以在一天内添加多项家务)
$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
子句即可简单列出一天的琐事。