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:
GET
request to http://xkcd.com/info.0.json
, fetch the value of the num
element.http://xkcd.com/XXX/info.0.json
where XXX
is the value of 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+'
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.XXX
) into the second curl -s 'http://xkcd.com/XXX/info.0.json'
command.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]+).*/\1/'
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]+).*/\1/')
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]+).*/\1/')/info.0.json"
By the way, I highly recommend jq as the command line JSON processor. To extract num
with jq
, it's as simple as
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With