MissingEndParenthesisInMethodCall - 检查环境变量是否为空

时间:2018-03-15 19:08:01

标签: powershell docker environment-variables

我正在尝试检查我的PowerShell脚本中的环境变量是空还是未设置。该脚本在Docker容器中运行,如果定义了环境变量,则用于提取新代码:

CMD if (-not ([string]::IsNullOrEmpty(env:UPDATE_FROM_GITHUB))) { \
        Write-Host Git pull started; \
        PortableGit\bin\git.exe pull; \
    }; \

我收到了一堆错误:

web_1     | At line:1 char:110
web_1     | + ... ence = 'SilentlyContinue'; if (-not ([string]::IsNullOrEmpty(env:UPDA ...
web_1     | +                                                                  ~
web_1     | Missing ')' in method call.
web_1     | At line:1 char:110
web_1     | + ... ue'; if (-not ([string]::IsNullOrEmpty(env:UPDATE_FROM_GITHUB))) { Wr ...
web_1     | +                                            ~~~~~~~~~~~~~~~~~~~~~~
web_1     | Unexpected token 'env:UPDATE_FROM_GITHUB' in expression or statement.
web_1     | At line:1 char:110
web_1     | + ... ence = 'SilentlyContinue'; if (-not ([string]::IsNullOrEmpty(env:UPDA ...
web_1     | +                                                                  ~
web_1     | Missing closing ')' in expression.
web_1     | At line:1 char:110
web_1     | + ... ue'; if (-not ([string]::IsNullOrEmpty(env:UPDATE_FROM_GITHUB))) { Wr ...
web_1     | +                                            ~~~~~~~~~~~~~~~~~~~~~~
web_1     | Missing closing ')' after expression in 'if' statement.
web_1     | At line:1 char:132
web_1     | + ... e'; if (-not ([string]::IsNullOrEmpty(env:UPDATE_FROM_GITHUB))) { Wri ...
web_1     | +                                                                 ~
web_1     | Unexpected token ')' in expression or statement.
web_1     | At line:1 char:133
web_1     | + ... '; if (-not ([string]::IsNullOrEmpty(env:UPDATE_FROM_GITHUB))) { Writ ...
web_1     | +                                                                 ~
web_1     | Unexpected token ')' in expression or statement.
web_1     | At line:1 char:134
web_1     | + ... ; if (-not ([string]::IsNullOrEmpty(env:UPDATE_FROM_GITHUB))) { Write ...

我不知道如何从这里开始。错误似乎是多余的,但没有一个明确的起点。它可能与Docker如何解析命令有关吗?

1 个答案:

答案 0 :(得分:0)

PowerShell变量(包括环境变量)必须始终引用sigil $

因此,env:UPDATE_FROM_GITHUB必须为 $env:UPDATE_FROM_GITHUB

要简单测试是否定义了环境变量/具有值,您就不必严格需要-not [string]::IsNullOrEmpty(...);相反,您可以利用PowerShell的隐式布尔转换

  • $null或空字符串被视为 $False
  • 以及任何非空字符串 $True

把它们放在一起:

CMD if ($env:UPDATE_FROM_GITHUB) { \
        Write-Host Git pull started; \
        PortableGit\bin\git.exe pull; \
    }; \
相关问题