I'm trying to download some artifacts from artifactory, using bash and the AQL.
artifacts=("blah" "blah2")
for i in ${!artifacts[@]};
do
artifact="${artifacts[$i]}*jar"
resultAsJson=$(curl -u$username:"$password" -X POST http://$host/artifactory/api/search/aql -H "content-type: text/plain" -d 'items.find({ "repo": {"$eq":"libs-release-local"}, "name": {"$match" : "*********"}})')
echo $resultAsJson
done
Above, where I've put ********
, it works if I put in a string literal, or string with wildcard. Like artifact*jar
works.
But I can't get it to evaluate $artifact
before curl evaluates it. So it searches for the literal string, $artifact
instead of blah*jar
. I've tried a lot of things now.
CodePudding user response:
The reason in simple: you are using single quotes which don't expand shell variables.
You have to replace:
'items.find({ "repo": {"$eq":"libs-release-local"}, "name": {"$match" : "$artifact"}})'
With:
"items.find({ \"repo\": {\"$eq\": \"libs-release-local\"}, \"name\": {\"$match\" : \"$artifact\"}})"
CodePudding user response:
If you want to expand just one variable, single quoting is more readable :
'items.find({ "repo": {"$eq":"libs-release-local"}, "name": {"$match" : "'"$artifact"'"}})'
Explanation of "'"$artifact"'"
:
- First " is put into output string
- First ' finishes output string
- Then put "$artifact" in output string
- Finally
'"
starts another output string with " as the first character.