bash:如何对字段进行 grep 和循环,在字段响应时附加结果

bash: how to grep for a field and loop, appending results while field is in response

我有一个 bash 脚本,它使用他们的 REST api:

提取我们存储在 nexus 存储库中的一些工件
get_hosts()
{
    curl --silent -X GET "https://foo.bar/service/rest/v1/search?maven.groupId=foo.bar.hosts" |
        jq '.items[].name' --raw-output |
        sort -u
}

do_work()
{
    for host in $(get_hosts);
    do
        ...
    done
}

最近由于工件的数量超过了某个阈值而崩溃,并且 nexus 使用 pagination

Many of the REST API's make use of a pagination strategy for dealing with operations that can return a large number of items. This strategy is built around the notion of a continuationToken and a page size that determines the maximum number of items that can be returned in a single response.

GET /service/rest/v1/<api>?<query>
{
  "items" : [
    ...
  ],
  "continuationToken" : "88491cd1d185dd136f143f20c4e7d50c"
}
GET /service/rest/v1/<api>?<query>&continuationToken=88491cd1d185dd136f143f20c4e7d50c

问题:

如何修改我的 get_hosts 函数以 grep 查找 continuationToken,并循环直到收到所有页面?

你可以试试这个:

unset ct
get_hosts()
{
    res="$(curl -s "https://foo.bar/service/rest/v1/search?maven.groupId=foo.bar.hosts$ct" )"
    jq '.items[].name' --raw-output <<< "$res" | sort -u
    ct="&continuationToken=$(jq .continuationToken <<< "$res")"
}

do_work()
{
    while hosts="$(get_hosts)"; test -n "$hosts"
    do
        for host in $hosts;
        do
            ...
        done
    done
}