sed命令忽略Groovy脚本中的单引号 [英] Sed command ignoring single quotes in Groovy scripts
问题描述
您好,当我尝试在Groovy中使用SED替换字符串时,遇到了这个问题,它忽略了我传递的单引号.这是我尝试在sed中使用双引号的代码,它引发错误.
Hello I am facing this issue when I try to replace string using SED in Groovy, it's ignoring the Single quotes which I am passing. Here is my code I tried using double quotes inside sed and it's throwing errors.
stage('Version')
{
dir('./Dest/Scripts/')
{
sh "sed -i 's/VERSION_BUILD=0/VERSION_BUILD= '$Version', System2 = '$name'/g' setversion.sql"
}
}
我想要的输出是
UPDATE &Shared_Version SET SharedVersion = '2010', System1 = 'XXXX', InetsoftVersion = 2, VERSION_BUILD='20180302', System2 = 'test';
COMMIT;
但是,在运行Groovy脚本后,我得到的结果低于以下结果.
however I am getting below results after I run the Groovy script.
UPDATE &Shared_Version SET SharedVersion = '2010', System1 = 'XXXX', InetsoftVersion = 2, VERSION_BUILD= 20180302, System2 = test;
COMMIT;
我确实在shell Command中知道我们是否传递双引号将其替换,但是Groovy不喜欢它.
I do know in shell Command if we pass double quotes it will replace, however Groovy is not liking it.
sed -i "s/VERSION_BUILD=0/VERSION_BUILD= '$Version', System2 = '$name'/g" setversion.sql
有人可以帮助我在这里解决此问题吗?
Can some one help me to address this problem here.
谢谢
推荐答案
以下脚本按预期工作
sh "sed -i 's/VERSION_BUILD=0/VERSION_BUILD= \\x27${Version}\\x27, System2 = \\x27$name\\x27/g' setversion.sql"
重点是单引号可以转义为\ x27
The point is that single quote can be escaped as \x27
这篇关于sed命令忽略Groovy脚本中的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!