簡體   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