groovy + shell:转义字符

Sid*_*rth 5 shell groovy groovyshell jenkins jenkins-pipeline

我正在使用 Groovy 沙箱使用 Jenkins 管道。我正在尝试在 groovy sh 函数中运行 shell 脚本。

原来的shell脚本是

sed -i 's/sometext/'"${othertext}"'/' filename
Run Code Online (Sandbox Code Playgroud)

我正在尝试用其他文本(动态获取)替换特定文本。该脚本在直接执行时工作正常。但我想在 jenkins groovy sh 函数中使用它。

sh(script: '<above shell script>',  returnStdout:false)
Run Code Online (Sandbox Code Playgroud)

但是存在转义字符的问题。我试过这种逃避性格的方式

sh (script: '''sed -i 's/sometext/othertext/' filename''', returnStdout:false)
Run Code Online (Sandbox Code Playgroud)

它工作正常,但othertext不是动态获取的。有人可以帮我用原始脚本转义字符吗?或者请建议任何其他方式来做到这一点。

Sid*_*rth 3

根据daggettmkobit的输入,我做了一些实验,以下脚本运行良好

def l_othertext = sh(script: 'echo ${othertext}', returnStdout: true).trim()
print('l_othertext='+l_othertext)
sh "sed -i 's/sometext/'${l_othertext}'/' filename"
Run Code Online (Sandbox Code Playgroud)