Sed command ignoring single quotes in Groovy scripts

谁说胖子不能爱 提交于 2021-01-28 08:27:26

问题


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"              
            }
        }

My desired output is

UPDATE &Shared_Version SET SharedVersion = '2010', System1 = 'XXXX', InetsoftVersion = 2, VERSION_BUILD='20180302', System2 = 'test';
COMMIT;

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;

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.

Thanks


回答1:


Following script works as you expected

sh "sed -i 's/VERSION_BUILD=0/VERSION_BUILD= \\x27${Version}\\x27, System2 = \\x27$name\\x27/g' setversion.sql"

The point is that single quote can be escaped as \x27



来源:https://stackoverflow.com/questions/49074364/sed-command-ignoring-single-quotes-in-groovy-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!