【问题标题】:Escape double quotes and single quotes in Jenkins pipeline script在 Jenkins 管道脚本中转义双引号和单引号
【发布时间】:2020-11-04 07:18:33
【问题描述】:

我里面有一个powershell命令,双引号和单引号都存在,为了使命令在dos提示符下成功执行,肯定需要它。我不确定如何让它在管道脚本中转义。

bat "powershell -Command "(Get-Content "${Physical_FolderLoc}\\${Stord_Process_Name}.txt") | ForEach-Object { $_ -replace [RegEx]::Escape('E:\\config'), 'I:\\config' } | Set-Content "${Physical_FolderLoc}\\${Stord_Process_Name}.txt" " "

在上面的命令中,你可以看到倒数第二个 " 是 Get Content 的结束引号,最后一个是 bat 命令。 我用三斜杠尝试了上面的命令,但得到了 groovy 错误。

groovy.lang.MissingPropertyException: No such property: _ for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty

请帮我解决这个问题。

【问题讨论】:

  • 你用什么“蝙蝠”? Jenkins 管道也支持 powershell。
  • 好的。是的,我使用 bat,但是无论我使用 bat 还是 powershell 命令,Jenkins 参数变量都可能无法使用 base64 编码解析。
  • @elou 嗨,Elou。我已经浏览了您关于将 powershell 命令合并到 Jenkins 管道中的帖子,请您帮我将上述 powershell 命令传递到 jenkins 管道中。 Get-Content 之前的双引号 " 肯定需要。我在 bat " " 中传递命令。
  • @Vasiliki Siakka 请帮我解决上面的powershell命令。我已经浏览了您之前关于 No such property: _ for class: groovy.lang.Binding, bcoz 的帖子我收到上述 powershell 命令的相同错误可能是由于双引号转义。

标签: powershell jenkins groovy jenkins-pipeline


【解决方案1】:

更改每个内部双引号字符串,例如:

"${Physical_FolderLoc}\\${Stord_Process_Name}.txt"

到:

('{0}\\{1}.txt' -f $Physical_FolderLoc, $Stord_Process_Name)

【讨论】:

  • 能否请您提供完整的命令,这将非常有帮助。
  • 请花点时间解决命令一次,以便将来我可以处理此类问题。
  • 我按照你的建议试过了,它抛出错误。 groovy.lang.MissingPropertyException:没有这样的属性:_ 类:groovy.lang.Binding 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty 的 groovy.lang.Binding.getVariable(Binding.java:63) (SandboxInterceptor.java:270) 在 org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
  • 我通过了: bat "powershell.exe -Command \"(Get-Content ('{0}\\{1}.txt' -f $Physical_FolderLoc, $Stord_Process_Name)) | ForEach-Object { $_ -replace [RegEx]::Escape('E:\\config'), 'I:\\config' } |设置内容('{0}\\{1}.txt' -f $Physical_FolderLoc, $Stord_Process_Name)\" "
【解决方案2】:

我得到了命令没有转义的确切问题。 powershell 命令中有一个独立的 $ 符号,它也是 groovy 的系统字符,因此也需要转义。(后面的 $ 字符ForEach-Object {)。它现在以 \ 为前缀,因此被转义了。

现在完整的命令是:

bat "powershell -Command \"(Get-Content ${Physical_FolderLoc}\\${Stord_Process_Name}.txt) | ForEach-Object { \$_ -replace [RegEx]::Escape('E:\\config'), 'I:\\config' } | Set-Content ${Physical_FolderLoc}\\${Stord_Process_Name}.txt\" "

虽然没有人试图提供太多帮助,但仍然非常感谢您的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-13
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多