【问题标题】:shell script to recognise jira key识别jira key的shell脚本
【发布时间】:2017-07-26 08:45:07
【问题描述】:

我的 jenkins 作业配置中的以下行执行 shell 检索 jira 密钥

JIRA_KEY=$(curl --request GET "http://jenkins-server/job/myProject/job/mySubProject/job/myComponent/${BUILD_NUMBER}/api/xml?xpath=/*/changeSet/item/comment" | sed -e "s/<comment>\(.*\)<\/comment>/\1/")

JIRA_KEY=$(echo $JIRA_KEY | cut -c10-17)

但如果文本不以 jira 键开头,那么根据当前逻辑,它将分配 10-17 范围内的任何文本。当 &lt;comment&gt; 中不存在 jira 键时,我需要在变量 JIRA_KEY 中存储空字符串“”,我们该怎么做?

这里是xml

<freeStyleBuild _class="hudson.model.FreeStyleBuild">
    <changeSet _class="hudson.plugins.git.GitChangeSetList">
        <item _class="hudson.plugins.git.GitChangeSet">
            <comment>
                JRA-1011 This is commit 
                message.
            </comment>
        </item>
    </changeSet>
</freeStyleBuild>

【问题讨论】:

  • 不清楚,您能否告诉我们预期的输出是什么,我看到您正在尝试获取 10 到 17 的字符,请添加更多信息以在代码标签中发布。
  • 正如帖子中提到的“当&lt;comment&gt;中不存在jira键时,我需要在变量JIRA_KEY中存储空字符串”

标签: regex shell sed


【解决方案1】:

正如我在评论部分提到的,不清楚您需要哪种输出,因此基于一些假设,您能否尝试关注并让我知道。 I- 如果您需要 to 之间的所有字符串,则可以运行以下命令。

awk '/<\/comment>/{a="";next}/<comment>/{a=1;next}a'  Input_file

II- 如果您只需要在 to 之间找到 JRA 字符串,那么您可以执行以下操作。

awk '/<\/comment>/{a="";next}/<comment>/{a=1;next} a && /JRA/{match($0,/[a-zA-Z]+[^ ]*/);print substr($0,RSTART,RLENGTH)}'  Input_file

【讨论】:

  • 好的,请告诉我您期望的输出是什么?
  • 中不存在 jira 键时,我需要在变量 JIRA_KEY 中存储空字符串“”
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 2014-12-07
相关资源
最近更新 更多