Bash Store Curl Results into Array

时间:2017-12-04 15:58:51

标签: arrays bash curl

我正在运行以下curl命令:

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”行,删除“创建”选项。

编辑:

我使用了以下命令:

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页面中删除每个URL。命令是:

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

但我无法弄清楚如何将每一行存储为一个单独的变量,因此我可以删除每一行。

1 个答案:

答案 0 :(得分:2)

使用jq提取网址。

$ 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 FAQ 001

(我假设网址不包含换行符;如果是这种情况,请切换到可以更轻松处理任意数据的其他语言。)