遍历Bash Curl搜索结果

时间:2017-12-01 19:22:48

标签: bash curl traversal

我试图浏览curl语句的结果,然后删除返回的每个网址。

#!/bin/bash

declare -i Current_Date
declare -i Three_Months_Ago

Current_Date=($(date +%s%N)/1000000)
Three_Months_Ago=(${Current_Date}-7889238000)

curl -Username:Password "URL/api/search/dates?dateFields=created&from=${Three_Months_Ago}&today&repos=generic-sgca"

返回以下内容:

{
  "results" : [ {
    "uri" : "URL/api/storage/generic-sgca/Lastest_Deploy.tar",
    "created" : "2017-09-14T11:59:14.483-06:00"
  }]

这样的线路超过50条。现在我希望能够运行另一个" -X DELETE" curl命令将删除返回的每个URL。如:

URL=curl -Username:Password "URL/api/search/dates?dateFields=created&from=${Three_Months_Ago}&today&repos=generic-sgca"

curl -Username:Password -X DELETE ${URL}

或类似的东西。我怎么能做到这一点?

编辑:

我已经尝试了这个并且它似乎不起作用:

#!/bin/bash

declare -i Current_Date
declare -i Three_Months_Ago

Current_Date=($(date +%s%N)/1000000)
Three_Months_Ago=(${Current_Date}-7889238000)

while read -r uri; do
curl -Username:Password  -X DELETE "$uri" 
done < <(
    curl -Username:Password "URL/api/search/dates?dateFields=created&from=${Three_Months_Ago}&today&repos=generic-sgca" | # get the list of uris
    grep -o "uri"    | # filter out everything else
    tr -d ','        | # remove commas
    awk '{print $3}'   # print just the uri
    )

我得到了回报:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3225    0  3225    0     0   3225      0 --:--:-- --:--:-- --:--:-- 15140
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed
curl: (3) <url> malformed

1 个答案:

答案 0 :(得分:1)

使用可变数量的输入行执行某些操作的常见Bash习惯用法如下:

while read -r line; do
   something_interesting "$line"

done < <(command)

没有足够的信息可以在这里为你编写整个脚本,但是有些内容应该可行,并为你提供一个起点。

while read -r uri; do
  curl_request_to_delete_resource "$uri"

done < <(
    curl $parameters | # get the list of uris
    grep -o "uri"    | # filter out everything else
    tr -d ','        | # remove commas
    awk '{print $3}'   # print just the uri
    )