简体   繁体   English

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

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

I have one powershell command inside that both Double quotes and single quotes are present and it's needed for sure in order to make the command execute successfully in dos prompt.我有一个 powershell 命令,其中存在双引号和单引号,并且肯定需要它才能使命令在 dos 提示符下成功执行。 I am not sure how to make it escape in pipeline script.我不知道如何让它在管道脚本中转义。

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" " "

In the above command you can see the second last " is ending quote for Get Content and last one is for bat command. I tried the above command with triple slash but getting groovy error.在上面的命令中,您可以看到倒数第二个 " 是获取内容的结束引号,最后一个是 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

Please help me to resolve this.请帮我解决这个问题。

Change each internal double quoted string, eg:更改每个内部双引号字符串,例如:

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

to:到:

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

I got the exact issue why the command was not escaping.我得到了为什么命令没有转义的确切问题。 There is a stand alone $ symbol in the powershell command which is also a system character for groovy, so that need to be escaped as well.(The $ character which is after ForEach-Object { ). powershell 命令中有一个独立的$符号,它也是 groovy 的系统字符,因此也需要对其进行转义。(在ForEach-Object {之后的$字符)。 It's now prefixed with \\, so escaped.它现在以 \\ 为前缀,所以转义了。

Now the complete command is :现在完整的命令是:

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\" "

Though no one had tried to help much, but still thanks a lot for the suggestion.虽然没有人试图提供太多帮助,但仍然非常感谢您的建议。

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

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