【发布时间】:2019-08-06 11:36:44
【问题描述】:
我写了 _in 函数来检测我们是否必须安装软件包。参数 -packages 和 +packages 有效,但 +base 和 +full 无效,我该如何解决?
$scriptArgs=$args
function _in {
Param($find)
foreach ($i in $scriptArgs) {
if ($i -eq $find) {
return 1
}
}
return 0
}
# Install packages
if (-not (_in("-packages")) -and (_in("+packages") -or _in("+base") -or _in("+full"))) {
PrintInfo "* Installing packages"
}
这行得通:
PS> powershell .\scripts\win\install_zds.ps1 +packages * 安装包 PS> powershell .\scripts\win\install_zds.ps1 +packages -packages-packages 禁用软件包安装,+packages 启用软件包安装。
这不起作用:
PS> powershell .\scripts\win\install_zds.ps1 +base PS> powershell .\scripts\win\install_zds.ps1 +full+base 和 +full 应该启用包安装。
编辑:我想了解原因:
我关注PetSerAI 评论,然后,我像这样删除括号:
if (-not (_in "-packages") -and ((_in "+packages") -or (_in "+base") -or (_in "+full"))) { }
这行得通,但我不明白为什么。我 found this explain 关于 PowerShell 中的括号:
Powershell 是一种经过解析和解释的语言。解释器将括号视为控制结构,调用站点不需要或不需要。
但是对于test-function("Hello"),hello 是字符串而不是结构。
function Test-Function {
Param(
[string]
$hello
)
"String: $hello"
}
Test-Function("Hello")
Test-Function "Hello"
【问题讨论】:
-
if (-not (_in "-packages") -and ((_in "+packages") -or (_in "+base") -or (_in "+full"))) -
@PetSerAl 谢谢!我使用了错误的语法...
-
不要编写自己的
-in实现... -
PowerShell 将裸序列
-packages解释为参数名称,因此不会将其作为参数传递 - 用引号限定它:powershell .\scripts\win\install_zds.ps1 +packages '-packages' -
@TheIncorrigible1 不是一回事
标签: powershell if-statement logical-operators