groovy + shell:转义字符 [英] groovy + shell : Escaping characters

查看:1834
本文介绍了groovy + shell:转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Groovy沙箱使用Jenkins Pipeline.而且我正在尝试在groovy sh函数中运行shell脚本.

I am using Jenkins Pipeline using Groovy sandbox. And i am trying to run a shell script in a groovy sh function.

原始的shell脚本是

The original shell script is

sed -i 's/sometext/'"${othertext}"'/' filename

我正在尝试将其他特定文本替换为其他文本(动态获取).直接执行脚本时,脚本运行良好. 但是我想在jenkins groovy sh函数中使用它.

I am trying to replace a particular text with other text (taken dynamically). The script works fine when executed directly. But I want to use it in jenkins groovy sh function.

sh(script: '<above shell script>',  returnStdout:false)

但是存在转义字符的问题. 我尝试过这种转义字符的方式

But there is a problem of escaping characters. I tried this way of escaping character

sh (script: '''sed -i 's/sometext/othertext/' filename''', returnStdout:false)

它可以正常工作,但是othertext不是动态获取的. 有人可以帮我用原始脚本转义字符吗? 或者,请提出其他任何建议的方法.

It works fine but othertext is not taken dynamically. Can someone please help me in escaping characters with the original script? Or please suggest any other way of doing this.

推荐答案

使用 daggett mkobit ,我做了几次实验,以下脚本效果很好

With the inputs from daggett and mkobit and i did few experiments, the following script worked well

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

这篇关于groovy + shell:转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆