如何在jenkins-pipeline bash命令中的单引号内转义单引号 [英] How do I escape a single quote inside single quotes in jenkins-pipeline bash command
问题描述
在jenkins-pipeline中,我试图使用SED将以下行追加到文件末尾.
in jenkins-pipeline, I'm trying to use SED to append the following line to the end of a file.
sh "sed -i '\$ s/\$/ public_file=\\/var\\/lib\\/jenkins\\/workspace\\/test-project\\ ansible_ssh_common_args='-o StrictHostKeyChecking=no' /' file.txt"
但是我不知道如何在我的Jenkins文件中转义下面的行以使其起作用.
but I can't figure out how to escape the below line in my Jenkins file to make it work.
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
我已经尝试了以下方法,该方法可在katacoda游乐场中使用,但不能在jenkins管道中使用.
I've already tried the following, which works in katacoda playground, but not in jenkins pipeline.
'"'"'-o StrictHostKeyChecking=no'"'"' /' file.txt
推荐答案
-
使用
"
包裹sed命令,然后您可以直接在命令中使用'
而不逃脱它.
use
"
to wrap sed command, then you can use'
in command directly without to escape it.
使用@
,但是将/
用作sed s
命令的定界符,那么您无需转义文件路径中出现的/
即可使整个命令更加简洁和可读.
use @
, but /
as delimiter for sed s
command, then you no need to escape the /
appeared in file path to make the whole commend more concise and readable.
sed -i "\$ s@\$@ public_file=/var/lib/jenkins/workspace/test-project ansible_ssh_common_args='-o StrictHostKeyChecking=no' @" file.txt
这篇关于如何在jenkins-pipeline bash命令中的单引号内转义单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!