I have a bash script where I am storing some hostnames in a bash array which I read in from a file. I would then like to pass that array to the description on the jira I am trying to create using the Jira Server Rest API v2. However, with the following code, I keep getting an error. If someone can provide some guidance it'd be much appreciated.
counter=0
declare -a hosts
while read line
do
hosts[$counter]=$line
done < hostsfile.txt
curl --request POST --url 'https://myjiraurl.com/rest/api/2/issue' --header 'Authorization: Basic <auth_code>' --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{ "fields": { "description": "This is an automated JIRA.\nHost List:'${hosts[@]}'", "issuetype": { "id": "10000" }, "project": { "id": "13806" }, "summary": "Host List" } }'
The error I am getting is
{"errorMessages":["Unexpected end-of-input in VALUE_STRING\n at [Source: org.apache.catalina.connector.CoyoteInputStream@6bc37815; line: 1, column: 239]"]}curl: (7) Failed to connect to host1 port 80: Connection refused
curl: (7) Failed to connect to host2 port 80: Connection refused
curl: (7) Failed to connect to host3 port 80: Connection refused
curl: (3) [globbing] unmatched close brace/bracket in column 106
You've bumped up against bash quoting rules, which can be challenging. Regardless, I prefer to use jq to construct JSON data in scripts.
For example:
HOSTS=$(tr "\n" " " < hostsfile.txt | sed 's/ $//')
DATA_JSON=$(jq --null-input --compact-output --arg hostlist "${HOSTS}" \
'{"fields":{"description":("Host List: "+$hostlist),"issuetype":{"id":"10000"},"project":{"id":"13806"},"summary":"Host List"}}')
This worked, thanks much!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.