如何在jenkins-pipeline bash命令中的单引号内转义单引号 [英] How do I escape a single quote inside single quotes in jenkins-pipeline bash command

查看:471
本文介绍了如何在jenkins-pipeline bash命令中的单引号内转义单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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

推荐答案

  1. 使用"包裹sed命令,然后您可以直接在命令中使用'而不逃脱它.

  1. 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屋!

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