2017-07-26 36 views
0

以下線在我詹金斯作業配置執行shell檢索JIRA關鍵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範圍內。當jira鍵在<comment>中不存在時,我需要在變量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> 
+0

不清楚,請你讓我們知道什麼是預期的輸出,我看到你正在試圖讓人物從10到17,請添加更多的信息代碼標籤張貼。 – RavinderSingh13

+0

正如在「我需要存儲空字符串」一文中提到的,當jira鍵不在「」中時,變量JIRA_KEY中出現「 –

回答

0

。 我 - 如果你需要所有的字符串之間,那麼你可以運行以下。

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

II-如果你只需要找到JRA字符串,那麼你可以做下面的事情。

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

原始帖子中的代碼已經在執行此操作。 –

+0

好的,那麼請讓我知道你期待的輸出是什麼? – RavinderSingh13

+0

當jira鍵不在中時,我需要在變量JIRA_KEY中存儲空字符串「」 –