Ahsan Naseem Ahsan Naseem - 1 year ago 152
JSON Question

Parsing variables in curl with bash script

Hey I am using conduit curl method to create tasks from post. It work fine when I run from terminal with hardcoded values. But when I try to execute it with variables it throws an error:


echo "$1"
echo "$2"
echo "$3"
echo "$4"
echo "$5"
echo '{
"transactions": [
"type": "title",
"value": "$1"
"type": "description",
"value": "$2"
"type": "status",
"value": "$3"
"type": "priority",
"value": "$4"
"type": "owner",
"value": "$5"
}' | arc call-conduit --conduit-uri https://mydomain.phacility.com/ --conduit-token mytoken maniphest.edit


./test.sh "test003 ticket from api post" "for testing" "open" "high" "ahsan"


test003 ticket from api post
for testing
{"error":"ERR-CONDUIT-CORE","errorMessage":"ERR-CONDUIT-CORE: Validation errors:\n - User \"$5\" is not a valid user.\n - Task priority \"$4\" is not a valid task priority. Use a priority keyword to choose a task priority: unbreak, very, high, kinda, triage, normal, low, wish.","response":null}

As you can see in error its reading $4 and $5 as values not variables. And I am failing to understand how to use $variables as input in these arguments.

Answer Source

You're using single quotes around the last echo to so that you can use double-quotes inside the JSON, but that causes echo to print the string without expanding anything. You need to use double quotes for the string, so you'll have to escape the double quotes inside of it.

Replace the last echo with this:

echo "{
  \"transactions\": [
      \"type\": \"title\",
      \"value\": \"$1\"
      \"type\": \"description\",
      \"value\": \"$2\"
      \"type\": \"status\",
      \"value\": \"$3\"
      \"type\": \"priority\",
      \"value\": \"$4\"
       \"type\": \"owner\",
       \"value\": \"$5\"

and it'll work. To avoid issues like this you can check http://wiki.bash-hackers.org and http://mywiki.wooledge.org/BashGuide for some general tips for bash newbies. Also, you can use shellcheck with a lot of text editors, which will spot errors like this automatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download