简体   繁体   English

在bash中的字符之间添加某些变量值

[英]Add certain variables value in between characters in bash

I am currently writing one script which creates an issue in jira based on //TODO comments in my java file. 我目前正在编写一个脚本,该脚本根据我的Java文件中的// TODO注释在jira中创建问题。 for that, i am using JIRA REST API. 为此,我正在使用JIRA REST API。

here's my testing script which creates only one issue from a file, 这是我的测试脚本,该脚本仅从文件创建一个问题,

#!/bin/bash
file=jira.txt


while IFS='' read -r line || [[ -n "$line" ]]; do
filename=$(tr -d '\n\r' <<< $(cut -d':' -f1 <<< $line))
summary=$(tr -d '\n\r' <<< $(cut -c4- <<< $(cut -d':' -f3 <<< $line)))

echo -e "\033[1mIssue Name\033[0m = $summary"
echo -e "\033[1mDescription\033[0m = $filename"
key="Project-181"
#key=$(curl -u username:password -X POST --data '{
#    "fields": {
#       "project":
#       {
#          "key": "project"
#       },
#       "summary": "'"$summary"'",
#       "description": "'"$filename"'",
#       "issuetype": {
#          "name": "Task"
#       }
#   }
#}' -H "Content-Type: application/json" http://JIRA-URL/rest/api/2/issue/ | jq '.key')
echo "key=$key"
sed  -i "/${summary}/s/$/ ${key}/" $filename
    done < "$file"

OUTPUT of script is, 脚本的输出

Issue Name = TODO remove hack for my task
Description = /opt/test/testfile.java
key=project-181

I purposely commented JIRA REST API part as I don't want to generate new issue every time I ran test script 我特意评论了JIRA REST API部分,因为我不想每次运行测试脚本时都产生新问题

the output of commented JIRA REST API is as same as a variable declared above that " key="Project-181 " 注释的JIRA REST API的输出与在该“ key =” Project-181 “上方声明的变量相同。

Content of jira.txt is as follows, jira.txt的内容如下,

/opt/test/testfile.java:211:            // TODO remove hack for my task

Now if I ran this script it will run successfully but when I check into "testfile.java " I see this, 现在,如果我运行此脚本,它将成功运行,但是当我签入“ testfile.java ”时,我看到了,

 // TODO remove hack for my task "Project-181"

I want something like that to happen, 我希望这样的事情发生

// TODO project-181 Remove hack for my task 

Any suggestion would be welcome Thank You. 任何建议都将受到欢迎,谢谢。

Given summary="TODO remove hack for my task" , if you want to insert $key after the "TODO", change the sed line to this: 给定summary="TODO remove hack for my task" ,如果要在$key “ TODO”之后插入$key ,请将sed行更改为:

sed  -i "/${summary}/s/TODO \(.*\)/TODO ${key} \1/" $filename

Your original sed command "/${summary}/s/$/ ${key}/" replaced the end of the line $ with a space followed by $key , effectively appending $key to the end instead of inserting after "TODO". 您原来的sed命令"/${summary}/s/$/ ${key}/"用空格后面的$key替换了$行的末尾,实际上将$key附加到末尾,而不是在“ TODO”之后插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM