0

I have written a shell script which is using curl command to build a Jenkins job with parameters. Following is the code.

#!/bin/bash
val3=( $(cut -d ',' -f3 csvfile.csv) )
printf "%s\n" "${val3[0]}"
echo $val3

curl -X POST https://my-jenkins.abc.com/inst/job/Aut/job/pipe-workflow/job/Job3/build \
 --user user:token \
  --data-urlencode json='{"parameter": [{"name":"valA", "value":"${val3}"}]}'

I am reading a file called as 'csvfile.csv' and passing the value of $val3 in the curl command. The output of $val3 is 'John'. I am not able to substitute the value of $val3 in the curl. It is taking it literary. I also tried following but none of them helped.

--data-urlencode json='{"parameter": [{"name":"valA", "value":"$val3"}]}'

--data-urlencode json='{"parameter": [{"name":"valA", "value":"${val3[0]}"}]}'

--data-urlencode json='{"parameter": [{"name":"valA", "value":"val3"}]}'

--data-urlencode json='{"parameter": [{"name":"valA", "value":"${val3}"}]}'

--data-urlencode json="{"parameter": [{"name":"valA", "value":"${val3}"}]}"

None of the above is working. The last one is throwing error. When I see in Jenkins job, the value of parameter is as following:

$val3, val3, ${val3}

It is taking it as it is than substituting the value.

printf "%s\n" "${val3[0]}"
echo $val3

The above two lines are giving me output as John but while putting it in curl, its not working. What is causing this and how do I fix it?

Thank you in advance

Raji
  • 857
  • 6
  • 18
  • 37
  • http://stackoverflow.com/q/6697753/2088135 – Tom Fenech May 16 '17 at 10:48
  • Thanks Tom. I have tried both single and also double quotes and it did not work – Raji May 16 '17 at 11:08
  • 1
    You need to use double quotes on the outside and then escape the double quotes inside the string. I assume that the variables don't contain any double quotes themselves. – Tom Fenech May 16 '17 at 11:11
  • When I ue double quotes outside, it is giving error: Error

    An error occurred processing your request. Ask your Jenkins administrator to look up details. ErrorID=864a6488-2aef-4804-b001-55aefd4910b0


    Stack trace suppressed by the suppress stack trace plugin It works fine with singlr quote outside, I think. I referred the post for Jenkins from here https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
    – Raji May 16 '17 at 11:40

1 Answers1

1

Here are some problems that I can see:

val3=( $(cut -d ',' -f3 csvfile.csv) )

This creates an array, so you probably want this:

val3=$(cut -d ',' -f3 csvfile.csv)

Then your variable should be inserted like this:

--data-urlencode json="{\"parameter\": [{\"name\": \"valA\", \"value\":\"$val3\"}]}"

You may also want to consider using a tool like jq to produce your JSON:

json="$(jq -nc --arg v "$var3" '{ parameter: [{ name: "valA", value: $v }] }')"
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Thank a ton!! This is working after modifying assignment statement as mentioned by you and putting slash. Thanks a lot – Raji May 16 '17 at 13:16