如何使用curl人工AQL查询进行bash变量替换 [英] How to do bash variable replacement with curl artifactory AQL query
问题描述
我希望能够在对Artifactory Query Language api(AQL)的以下查询中使用bash变量替换
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
}
但是,将curl命令替换为bash变量却证明很困难
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
除了$ repopath变量不会被替换为调用之外,它几乎可以正常工作.
It almost works, except the $repopath variable does not get substituted into the call.
推荐答案
该变量未扩展,因为它在单引号内(从 items.find
之前开始).您应该暂时将单引号切换为双引号,以进行扩展,如下所示:
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
这篇关于如何使用curl人工AQL查询进行bash变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!