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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:55:37  来源:igfitidea点击:

Parsing a curl json response and using response to build another request

bashcurlgrep

提问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 询问最新的漫画或特定的漫画,因此我需要:

  1. Send a GETrequest to http://xkcd.com/info.0.json, fetch the value of the numelement.
  2. Send another request to http://xkcd.com/XXX/info.0.jsonwhere XXXis the value of num.
  1. 向 发送GET请求http://xkcd.com/info.0.json,获取num元素的值。
  2. 发送另一个请求到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+'
  1. 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.
  2. I have no idea how to read the output of grep (as XXX) into the second curl -s 'http://xkcd.com/XXX/info.0.json'command.
  1. 我一直无法弄清楚如何将捕获组与 一起使用grep,因此我需要两次 grep json。常见的建议是使用-P,它在 Mac OS X 10.10 中不受支持。
  2. 我不知道如何将 grep (as XXX)的输出读入第二个curl -s 'http://xkcd.com/XXX/info.0.json'命令。

回答by 4ae1e1

  1. 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]+).*//'
    
  2. 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"
    
  1. 在 OS X 上,您可以使用系统 Perl:

     curl -sS http://xkcd.com/info.0.json | /usr/bin/perl -pe 's/.*"num": ([0-9]+).*//'
    
  2. 您可以使用命令替换将输出保存到变量中:

    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 numwith jq, it's as simple as

顺便说一下,我强烈推荐jq作为命令行 JSON 处理器。要提取物numjq,它的那样简单

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 jqthat 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