0

i have following text file (output.txt)

TECH-746 TECH 10400
TECH-747 TECH 10400

i need to read all columns and pass it to 3 variables and then submit it to curl command. While read simple won't work with curl (don't know why) so i need to use for loop (it works with curl), can i use one for loop or need to nest multiple ones

for project in `cat output.txt`; do
echo $project
curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null

code above works, so i just want to "extend" to to include all other columns in file

5
  • If you had all three columns, where would column 2 and column 3 go in your curl command? Or would they be ignored, and you would only use column 1? Commented Mar 7, 2018 at 15:27
  • 1
    You should strongly consider using jq (or something similar) to create your JSON rather than relying on shell interpolation. Commented Mar 7, 2018 at 15:27
  • @chepner, i can't use jq because having difficulties getting issues with subtasks (created python script which created output.txt) Commented Mar 7, 2018 at 15:32
  • @Milister I'm not sure what you mean. curl ... --data "$(jq --arg p "$project" '{key: $p}')" is an example of what I am talking about. This ensures the value of $project is encoded properly to form a valid JSON string. Commented Mar 7, 2018 at 15:53
  • @chepner it works now, solution provided by Jeff Breander works, mistakenly posted wrong code Commented Mar 7, 2018 at 15:56

1 Answer 1

3

while read can pull a line into distinct variables.

while read project col2 col3
do
  curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null
done < sourcefile.txt

EDIT: The curl command also misses escaping one quote, compare the two lines below, the first one is the original, the second one is the one I've corrected.

curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null
curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test\",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, yes i know, but while simple won't pass values to curl, that's why i need to use for loop
@Milister then there's a problem with how you wrote your curl or your while loop. while doesn't inherently block curl commands.
Then can yo u please edit your question to provide a better example of what you want to achieve with your sample input? Which specific curl command do you want to build with that input?
@JeffBreadner it works, it seems missing escaping quote did the trick !! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.