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:
- Send a
GETrequest tohttp://xkcd.com/info.0.json, fetch the value of thenumelement. - Send another request to
http://xkcd.com/XXX/info.0.jsonwhereXXXis the value ofnum.
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.