简体   繁体   English

在 groovy jenkins 管道中转义 shell 字符

[英]Escaping shell character in groovy jenkins pipeline

I have some issue to escape quote character in my jenkins pipeline.我有一些问题要在 jenkins 管道中转义引号字符。 for example :例如 :

I want to append a text in to file through the pipeline using this command below:我想使用以下命令通过管道将文本附加到文件中:

openshift.rsh("${podname}", """sh -c 'echo "define( 'WP_HOME', 'http://rabah-test.com' );" >> wp-config.php'""")

and expecting this in to the wp-config.php file:并期望将其写入 wp-config.php 文件:

define( 'WP_HOME', 'http://rabah-test.com' );

but unfortunately i don't have the quotes in my result :但不幸的是,我的结果中没有引号:

define( WP_HOME, http://rabah-test.com );  

I believe it is not an issue with jenkins.我相信这不是詹金斯的问题。 Try to run your command in the terminal first.尝试先在终端中运行您的命令。 You have a simple quotes for your sh command and simple quotes inside the define.你的 sh 命令有一个简单的引号,定义中有一个简单的引号。

I would try something like我会尝试类似的东西

sh -c 'echo "define( \'WP_HOME\', \'http://rabah-test.com\' );" >> wp-config.php'

Note, instead of adding the define to the wp-config.php file, I would be tempted to version a default WP_HOME and replace it by a sed请注意,我不会将定义添加到 wp-config.php 文件,而是倾向于将默认的 WP_HOME 版本化并用 sed 替换它

I found how to deal with it.我找到了如何处理它。 Instead of using simple quotes, I decided to use double quotes and escape with two backslashes.我决定使用双引号并用两个反斜杠转义,而不是使用简单的引号。

Like this:像这样:

sh -c 'echo "define( \\" WP_HOME \\" , \\" http://rabah-test.com \\" );" >> wp-config.php'

And it now works perfectly.现在它可以完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM