我正在使用使用 Groovy 沙箱的 Jenkins 管道。我正在尝试在 groovy sh 函数中运行 shell 脚本。
原来的shell脚本是
sed -i 's/sometext/'"${othertext}"'/' filename
我正在尝试用其他文本(动态获取)替换特定文本。该脚本在直接执行时工作正常。
但我想在 jenkins groovy sh 函数中使用它。
sh(script: '<above shell script>', returnStdout:false)
但是存在转义字符的问题。
我试过这种逃避性格的方式
sh (script: '''sed -i 's/sometext/othertext/' filename''', returnStdout:false)
它工作正常,但
othertext
不是动态获取的。有人可以帮我用原始脚本转义字符吗?
或者请建议任何其他方式来做到这一点。
最佳答案
来自 daggett 的输入和 mkobit我做了很少的实验,下面的脚本运行良好
def l_othertext = sh(script: 'echo ${othertext}', returnStdout: true).trim()
print('l_othertext='+l_othertext)
sh "sed -i 's/sometext/'${l_othertext}'/' filename"
关于shell - 常规 + shell : Escaping characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201797/