Bash将卷曲结果存储到数组中 [英] Bash Store Curl Results into Array

查看:62
本文介绍了Bash将卷曲结果存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下curl命令:

I am running the following curl command:

results=$(curl -USERNAME:PASSWORD "URL/search/dates?dateFields=created&from=${Three_Months_Ago}&today&repos=generic-sgca")
echo "$results"

我得到这个作为回报:

"results" : [ {
    "uri" : "URL/api/storage/generic-sgca/Lastest_Deploy.tar",
    "created" : "2017-09-14T11:59:14.483-06:00"
  }, {
    "uri" : "URL/api/storage/generic-sgca/Installer-Deploy-0.0.5/SignalStudioPro-Alpha-1-linux-x64-installer.run",
    "created" : "2017-09-14T20:11:37.733-06:00"
  }

似乎实际上没有将curl结果存储为数组。我希望能够将每个 uri存储为变量,并使用 -X DELETE命令删除每个文件。如何单独获得 uri行,删除创建选项。

It doesn't seem to actually be storing the curl results as an array. I want to be able to store each "uri" as a variable and use an "-X DELETE" command to remove each file. How can I get the "uri" line on it's own, remove the "created" option.

编辑:

我使用了以下命令:

results=$(curl -username:password "URL/api/search/dates?dateFields=created&from=${Three_Months_Ago}&today&repos=generic-sgca" | jq -r '.results[].uri')
echo "$results"

我得到了这个回报:

URL/api/storage/generic-sgca/Lastest_Deploy.tar
URL/api/storage/generic-sgca/Installer-Deploy-0.0.5/SignalStudioPro-Alpha-1-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-0.0.5/SignalStudioPro-Alpha-1-windows-installer.exe
URL/api/storage/generic-sgca/Installer-Deploy-0.0.99/SignalStudioPro-Alpha-1-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-0.0.99/SignalStudioPro-Alpha-1-windows-installer.exe
URL/api/storage/generic-sgca/Installer-Deploy-0.2.0/SignalStudioPro-Alpha-2-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-0.2.0/SignalStudioPro-Alpha-2-windows-installer.exe
URL/api/storage/generic-sgca/Installer-Deploy-100.0.0/SignalStudioPro-Alpha-2-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-100.0.0/SignalStudioPro-Alpha-2-windows-installer.exe
URL/api/storage/generic-sgca/Installer-Deploy-101.0.0/SignalStudioPro-Alpha-2-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-101.0.0/SignalStudioPro-Alpha-2-windows-installer.exe
URL/api/storage/generic-sgca/Installer-Deploy-99.9.9/SignalStudioPro-Alpha-2-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-99.9.9/SignalStudioPro-Alpha-2-windows-installer.exe
URL/api/storage/generic-sgca/Installer-Deploy-99.9.91/SignalStudioPro-Alpha-2-linux-x64-installer.run
URL/api/storage/generic-sgca/Installer-Deploy-99.9.91/SignalStudioPro-Alpha-2-windows-installer.exe

现在我想要运行curl -X DELETE命令以从我的Artifactory页面中删除所有这些网址。该命令将是:

Now I want to run a curl -X DELETE command to delete each of those urls from my Artifactory page. The command would be:

curl username:password -X DELETE "URL FROM ABOVE"

但是我不知道如何将每一行存储为单独的变量,因此可以删除每一行。

But I can't figure out how to store each line as a separate variable so I can delete each one.

推荐答案

使用 jq 提取URL。

$ curl ... | jq -r '.results[].uri'
URL/api/storage/generic-sgca/Lastest_Deploy.tar
URL/api/storage/generic-sgca/Installer-Deploy-0.0.5/SignalStudioPro-Alpha-1-linux-x64-installer.run

然后使用您喜欢的正确技术进行迭代逐行显示命令的输出;有关详细信息,请参见 Bash常见问题001

Then use your favorite correct technique for iterating over the output of a command line-by-line; see Bash FAQ 001 for details.

(我假设一个URL将不包含换行符;如果是这种情况,请切换到另一种可以更轻松地处理任意数据的语言。)

(I'm assuming a URL will not contain a newline; if that's the case, switch to a different language that can handle arbitrary data more easily.)

这篇关于Bash将卷曲结果存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆