Alex Zhulin
Alex Zhulin

Reputation: 1305

bash: dynamic string for curl

I'm trying to get files from specific folder, load them with curl and get result to variable:

#!/bin/bash
shopt -s nullglob
FILES=*
for f in $FILES
do
  core=$f
  #echo "curl -H \"Content-Type: application/json\" --data-binary @$f http://v-cdh-master:8983/solr/$core/update/json?commit=true"
  result=$(curl -H "Content-Type: application/json" -s --data-binary @$f http://v-cdh-master:8983/solr/$core/update/json?commit=true)
done

The result of running this script is

curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

If I remove comment from echo string I could see the valid url like this

curl -H "Content-Type: application/json" --data-binary @affiliates_multival http://v-cdh-master:8983/solr/affiliates_multival/update/json?commit=true

Which works perfect when I copy/paste it to terminal.

Where is my mistake?

Upvotes: 0

Views: 1086

Answers (1)

chepner
chepner

Reputation: 532518

The ? in the URL causes the entire argument to be treated as a glob, which is not matching a file. Since you have the nullglob option enabled, the word is removed from the call to curl. Quoting the argument, as you do with the echo command, will resolved the problem by preventing pathname generation.

result=$(curl -H "Content-Type: application/json" -s --data-binary @"$f" "http://v-cdh-master:8983/solr/$core/update/json?commit=true")

Upvotes: 2

Related Questions