How to do bash variable replacement with curl artifactory AQL query [duplicate]

南笙酒味 提交于 2021-02-11 13:44:27

问题


I would like to be able to use bash variable replacement in the following query to Artifactory Query Language api (AQL)

In the shell, the command works like this:

$ curl -H 'content-type: text/plain' -H 'X-Api-Key: APIKEYGOESHERE' -X POST https://foo.jfrog.io/foo/api/search/aql -d '
> items.find({
>         "repo":{"$eq":"foo-docker"},
>         "path":{"$match":"REPONAME/*"}
> })
> .sort({
>         "$desc":["created"]
> }
> ).limit(1)'

{
"results" : [ {
  "repo" : "docker-local",
  "path" : "REPONAME/0.0.1-dev.54621",
  "name" : "manifest.json",
  "type" : "file",
  "size" : 3470,
  "created" : "2019-12-31T11:09:38.106Z",
  "created_by" : "automation",
  "modified" : "2019-12-31T11:09:37.940Z",
  "modified_by" : "automation",
  "updated" : "2019-12-31T11:09:38.107Z"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 1,
  "total" : 1,
  "limit" : 1
}

However, putting this curl command into a bash variable with replacement is proving difficult

Here's what I've tried so far, no luck

#!/bin/bash
# This script can find the most recent version of a docker image in artifactory

if [ $# -ne 2 ]; then
    echo "Usage: $0 apikey repo-path-name"
    exit 1
fi

apikey=$1
echo "apikey: $apikey"
repopath=$2
echo "repopath: $repopath"

output=`curl -H 'content-type: text/plain' -H "X-Api-Key: $apikey" -X POST https://foo.jfrog.io/foo/api/search/aql -d 'items.find({
        "repo":{"\$eq":"foo-docker"},
        "path":{"\$match":"$repopath/*"}
})
.sort({
        "$desc":["created"]
}
).limit(1)'`

echo $output

It almost works, except the $repopath variable does not get substituted into the call.


回答1:


The variable isn't expanding because it's inside single quotes (starting right before items.find). You should temporarily switch from single to double quotes to do the expansion, like this:

#!/bin/bash
# This script can find the most recent version of a docker image in artifactory

if [ $# -ne 2 ]; then
    echo "Usage: $0 apikey repo-path-name"
    exit 1
fi

apikey=$1
echo "apikey: $apikey"
repopath=$2
echo "repopath: $repopath"

output=`curl -H 'content-type: text/plain' -H "X-Api-Key: $apikey" -X POST https://foo.jfrog.io/foo/api/search/aql -d 'items.find({
        "repo":{"\$eq":"foo-docker"},
        "path":{"\$match":"'"$repopath"'/*"}
})
.sort({
        "$desc":["created"]
}
).limit(1)'`

echo $output


来源:https://stackoverflow.com/questions/59572096/how-to-do-bash-variable-replacement-with-curl-artifactory-aql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!