带有Bash的Bash脚本卷曲请求 [英] Bash scripting curl request with twist

查看:211
本文介绍了带有Bash的Bash脚本卷曲请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用curl从API提取数据,并且完成了大部分脚本,但是需要一些帮助.该API的结果是需要检索多个页面,并且已经能够对该函数进行脚本编写,但是我不知道如何使循环停止.另外,在弄清楚如何将每个页面数据分解为单个事件时,我需要一些帮助.每个事件都以相同的字符串开头,因此我知道用于拆分事件的文本,但是我不知道这样做的语法.有人也可以协助吗?我的代码如下:

I'm attempting to pull in data from an API using curl, and have the majority of the script done, but need some assistance. The API has results in multiple pages I need to retrieve, and I've been able to script that function, but I can't figure out how to make the loop stop. Also, I need some assistance in figuring out how to break each pages data up into individual events. Each event starts with the same string, so I know the text to use in order to break up the events, but I don't know the syntax for doing so. Can someone assist with that as well? My code is below:

#!/bin/sh
string='next\":null'
  for ((i=1;until $string;i++)); do
    curl -H "Authorization: Token insertlongtokenhere" -H "Content-Type: application/json" https://api.app.secunia.com/api/tickets/?page=$i
done

使用此字符串方法给我一个

Using this string method gives me a

./secunia.sh: line 3: ((: until next\":null: syntax error: invalid arithmetic operator (error token is "\":null")

我尝试了其他方法将双引号注释掉而无济于事,所以我有点被困住了.

I've tried other ways of commenting out the double quotes to no avail, so I'm kind of stuck.

推荐答案

for标头中取出最终测试,并在循环内进行测试.然后,您可以使用break结束循环.

Take the end test out of the for header, and test for it inside the loop. You can then use break to end the loop.

for ((i=1; ; i++)); do
    contents=$(curl -H "Authorization: Token insertlongtokenhere" -H "Content-Type: application/json" https://api.app.secunia.com/api/tickets/?page=$i)
    echo "$contents"
    if [[ $contents =~ 'next":null' ]]
    then break
    fi
done

这篇关于带有Bash的Bash脚本卷曲请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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