大佬教程收集整理的这篇文章主要介绍了powershell – 如果/ ElseIf阻止不使用-or,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
到目前为止我所得到的是,似乎评估与-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 }@H_450_24@问题是我不认为它在当天被正确评估,所以今天是星期二,评估结果是$mon -or $wed -or $fri
如果我按照以下方式对每天进行重新编码,那就像预期的那样工作.为什么不使用-or?
if($today -eq $tuE) { msg /time:2500 * $choreBath }@H_450_24@
if(($today -eq $mon) -or ($wed) -or ($fri))@H_450_24@
$today -eq $mon -or $today -eq $wed -or $today -eq $fri@H_450_24@或另一种看待它的方式.
($today -eq $mon) -or ($today -eq $wed) -or ($today -eq $fri)@H_450_24@PowerSHell不需要括号,但如果事情不顺利,最好使用它们.
当转换为布尔值时,PowerSHell中的非null /零长度字符串为true.专注于第二个条款,它可以改写为
"Wednesday" -or "Friday"@H_450_24@这总是如此.这就是为什么你的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@H_450_24@我们将所有对msg的调用删除为一个语句,因为只有$message更改.如果一个苦差日没有被交换机中的条款覆盖,那么默认值只是$choreOther.
以上是大佬教程为你收集整理的powershell – 如果/ ElseIf阻止不使用-or全部内容,希望文章能够帮你解决powershell – 如果/ ElseIf阻止不使用-or所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。