Home > Software design >  Bash evaluation order
Bash evaluation order

Time:11-03

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"'" :

  1. First " is put into output string
  2. First ' finishes output string
  3. Then put "$artifact" in output string
  4. Finally '" starts another output string with " as the first character.
  • Related