Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash循环一个curl请求,输出到文件并停止,直到响应为空_Bash_Curl - Fatal编程技术网

Bash循环一个curl请求,输出到文件并停止,直到响应为空

Bash循环一个curl请求,输出到文件并停止,直到响应为空,bash,curl,Bash,Curl,我有下面的bash文件,现在它正在基于for循环循环循环一个curl请求。然而,我想知道如何继续循环直到响应为空 #!/bin/bash # Basic while loop counter=1 for ((i=1;i<=2;i++)); do curl -o gettext.txt --request GET \ --url "https://api.io/v1/candidates?page=${counter}&per_pa

我有下面的bash文件,现在它正在基于for循环循环循环一个curl请求。然而,我想知道如何继续循环直到响应为空

#!/bin/bash

# Basic while loop
counter=1
for ((i=1;i<=2;i++));
    do
        curl -o gettext.txt --request GET \
        --url "https://api.io/v1/candidates?page=${counter}&per_page=500" \
        --header 'Authorization: Basic aklsjdl;fakj;l;kasdflkaj'
    ((counter++))
    done

echo $counter
echo All done
不幸的是,我调用的API是基于每页最多500个结果的页面的。我试图从2017年开始收集数据,因此数据量很大

我想继续反击,直到回应为空

#!/bin/bash

# Basic while loop
counter=1
for ((i=1;i<=2;i++));
    do
        curl -o gettext.txt --request GET \
        --url "https://api.io/v1/candidates?page=${counter}&per_page=500" \
        --header 'Authorization: Basic aklsjdl;fakj;l;kasdflkaj'
    ((counter++))
    done

echo $counter
echo All done
#/bin/bash
#基本while循环
计数器=1

对于((i=1;i您可以使用
break
在任意点结束循环:

#!/bin/bash
for ((counter=1; 1; counter++)); do
  curl -o gettext.txt --request GET \
    --url "https://api.io/v1/candidates?page=${counter}&per_page=500" \
    --header 'Authorization: Basic aklsjdl;fakj;l;kasdflkaj'
  if [ ! -s gettext.txt ]; then
    break;
  fi
  # do something with gettext.txt
  # as in your question, it will be overwritten in the next iteration
done
echo "$counter"
echo "All done"

正如作者在自己帖子上的评论所述,返回的数据是json格式的。作者没有问如何附加两个json文件,但这是他/她完成工作的必要步骤。为了附加两个json,json1和json2,可能跳过json1最后一个字节
和json2第一个字节
{
,在它们之间添加
就足够了。这里我使用
jq
连接两个JSON,作为一种更通用的方法

在下面所示的示例中,
nextjsonchunk
文件是在每次请求时获得的json文件。如果它有内容,则使用
jq
将其附加到
mainjsonfile
。如果它似乎为空(根据其大小推断),则循环中断,结果移动到当前文件夹并进行清理

使用
curl

#!/usr/bin/env bash

tempfolder=/dev/shm  # temporary memory parition, avaiable in ubuntu
emptyjsonize=10      # the minimum json file length, to be used as a threshold

for ((counter=1; 1; counter++))
do
  curl "https://api.io/v1/candidates?page=${counter}&per_page=500" \
    --header "Authorization: Basic aklsjdl;fakj;l;kasdflkaj" \
    --ouput $tempfolder/nextjsonchunk
  if [ $(wc -c <$tempfolder/nextjsonchunk) -le $emptyjsonize ]; then break; fi
  jq -s '.[0]*.[1]' $tempfolder/mainjsonfile $tempfolder/nextjsonchunk > $folder/mainjsonfile
done
rm $tempfolder/nextjsonchunk # cleaning up
mv $tempfolder/mainjsonfile ./jsonresultfile # end result
  • 获取两个json示例并测试它们之间的合并,以检查是否正确完成,这是一个好主意

  • 确保空json文件检查是否正常也很好,10字节只是一个猜测

  • 示例中使用了
    tmpfs
    (内存中)分区
    /dev/shm
    ,以避免大量写入,但其使用是可选的

    • 像这样吗

      #!/bin/bash
      
      # Basic while loop
      counter=1
      while true; do
          data=$(curl --request GET \
              --url "https://api.io/v1/candidates?page=${counter}&per_page=500" \
              --header 'Authorization: Basic aklsjdl;fakj;l;kasdflkaj')
          [[    $data ]] || break
          echo "$data"   >> gettext.txt
          ((counter++))
      done
      
      echo $counter
      echo All done
      

      作为旁注,我总是在curl中使用
      -L
      选项。为什么不每次输出到一个新文件,然后在输出文件中对一些html或其他内容进行grep,直到grep变为空?@Roadowl输出是一个json响应,所以从那里开始,我打算将json转换为csv或excel文件。我做了一次测试,大约有140个每个页面有500条记录,所以我不想要140个单独的文件。首先让它工作,然后优化你关心的事情(比如临时文件的数量)之后。@that otherguy使哪个部分起作用?bash命令的工作方式与现在一样,但是,我正在试图找到一种方法,在没有响应时退出循环。谢谢,每次都会覆盖txt文件还是会追加?如果它是OVERWRITES,我如何在每次迭代时追加?要追加,请使用第二个文件,其中包含所有内容一起。在循环的末尾添加
      cat gettext.txt>>everything.txt
      。你知道这在不存在这种分区的mac上是如何工作的吗?我从来没有使用过mac,也许有人知道并且可以回答。