繁体   English   中英

在Jenkins管道脚本中转义双引号和单引号

[英]Escape double quotes and single quotes in Jenkins pipeline script

我有一个 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" " "

在上面的命令中,您可以看到倒数第二个 " 是获取内容的结束引号,最后一个是 bat 命令。我尝试使用三重斜杠执行上述命令,但出现了常规错误。

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

请帮我解决这个问题。

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

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

到:

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

我得到了为什么命令没有转义的确切问题。 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\" "

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM