Jenkins Shell字符串引用替换 [英] Jenkins shell string quotation replacement

查看:1056
本文介绍了Jenkins Shell字符串引用替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有执行Shell框的Jenkins作业.在execute shell中,我使用bash而不是破折号(sh).在execute shell中,我有一些应该被解释为包含转义序列的字符串(在本例中为新行:\n),因此这里是一个示例execute shell:

I have a Jenkins job with an execute shell box. In the execute shell I use bash instead of dash (sh). In the execute shell I have strings which are supposed to be interpreted as they contain escape sequences (in this case the new line: \n), so here is an example execute shell:

#!/bin/bash
set -x #echo on
git fetch
...

git commit -m "Merging $DEVELOPEMENTBRANCH into $INITIALBRANCH\n\nThis is an example second paragraph."
...

我的问题是Jenkins解释/替换了脚本块,改变了它的行为,具体来说,它替换了变量并将双引号替换为单引号,因此在控制台输出中看起来像这样:

My problem here is that the script block is interpreted/replaced by Jenkins in a way that it changes its behavior, to be specific it replaces the variables and replaces the double quotes with single quotes so in the console output it looks like this:

[4] $ /bin/bash /tmp/hudson7542128982632971668.sh
+ git fetch
...

+ git commit -m 'Merging my_feature into develop\n\nThis is an example second paragraph'
...

但是由于单引号,因此不会解释\n部分.这有什么窍门?我想保留双引号或至少解释\n序列.

But in this way the \n part won't be interpreted because of the single quotes. What is the trick here? I want to preserve the double quotes or at least interpret the \n sequences.

推荐答案

git commit -m "A multi-line\n\ncommit message"不会产生多行 仍然提交消息.提交消息的字面意义为A multi-line\n\ncommit message. 双引号不会导致bash解释printf转义序列.

git commit -m "A multi-line\n\ncommit message" will not produce a multi-line commit message anyway. The commit message will be, literally, A multi-line\n\ncommit message. Double-quotes do not cause bash to interpret printf escape-sequences.

要获取多行提交消息,您需要:

To get a multi-line commit-message you need:

git commit -m "`printf \"A multi-line\n\ncommit message\"`"

这在Jenkins shell步骤中很好用.

This works fine in a Jenkins shell step.

这篇关于Jenkins Shell字符串引用替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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