bash 解析 curl json 响应并使用响应构建另一个请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29621584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing a curl json response and using response to build another request
提问by Samuel Lindblom
I am trying to build an shell script that will fetch the image url for a random XKCD comic, so that I can display it using übersicht. Since you can only ask the XKCD API for the latest comic or a specific comic I need to:
我正在尝试构建一个 shell 脚本,该脚本将获取随机 XKCD 漫画的图像 url,以便我可以使用 übersicht 显示它。由于您只能向 XKCD API 询问最新的漫画或特定的漫画,因此我需要:
- Send a
GET
request tohttp://xkcd.com/info.0.json
, fetch the value of thenum
element. - Send another request to
http://xkcd.com/XXX/info.0.json
whereXXX
is the value ofnum
.
- 向 发送
GET
请求http://xkcd.com/info.0.json
,获取num
元素的值。 - 发送另一个请求到
http://xkcd.com/XXX/info.0.json
其中XXX
是的值num
。
My current command looks like this and successfully returns the comic number:
我当前的命令如下所示并成功返回漫画编号:
curl -s 'http://xkcd.com/1510/info.0.json' | grep -Eo '"num": \d+' | grep -Eo '\d+'
- I haven't been able to figure out how to use capture groups with
grep
, so I need to grep the json twice. The common advice is to use-P
, which is not supported in Mac OS X 10.10. - I have no idea how to read the output of grep (as
XXX
) into the secondcurl -s 'http://xkcd.com/XXX/info.0.json'
command.
- 我一直无法弄清楚如何将捕获组与 一起使用
grep
,因此我需要两次 grep json。常见的建议是使用-P
,它在 Mac OS X 10.10 中不受支持。 - 我不知道如何将 grep (as
XXX
)的输出读入第二个curl -s 'http://xkcd.com/XXX/info.0.json'
命令。
回答by 4ae1e1
On OS X you can use the system Perl:
curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//'
You can save the output to a variable with command substitution:
num=$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//') curl -sS "http://xkcd.com/${num}/info.0.json"
or more concisely, two-in-one, albeit not very readable:
curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//')/info.0.json"
在 OS X 上,您可以使用系统 Perl:
curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//'
您可以使用命令替换将输出保存到变量中:
num=$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//') curl -sS "http://xkcd.com/${num}/info.0.json"
或者更简洁地说,二合一,虽然不是很可读:
curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//')/info.0.json"
By the way, I highly recommend jqas the command line JSON processor. To extract num
with jq
, it's as simple as
顺便说一下,我强烈推荐jq作为命令行 JSON 处理器。要提取物num
与jq
,它的那样简单
curl -sS http://xkcd.com/info.0.json | jq '.num'
and although you didn't ask for it, here's a simple one-liner with jq
that extracts the URI of the latest image:
虽然您没有要求它,但这里有一个简单的单行代码,jq
用于提取最新图像的 URI:
curl -sS "http://xkcd.com/$(curl -sS http://xkcd.com/info.0.json | jq '.num')/info.0.json" | jq -r '.img'
Example output:
示例输出:
http://imgs.xkcd.com/comics/spice_girl.png