Using cURL and sed to output an image based on a bool's state

252 views Asked by At

I am making a geeklet (script that sits on the OS X desktop, from Geektool) that takes the status of unread messages from Reddit (using me.json from the API) using the command

echo curl -X GET -u 'username:password' http://www.reddit.com/api/me.json | sed -e "s/.*\"has_mail\":SOMEHOW GET TRUE/FALSE OF THE BOOL"

and then take the state of the bool to set an image.

Input should be either TRUE (there is an unread message) or false (there are no unread messages). Output should be an image in a local directory, changing depending on true/false.

Two Questions:

  1. How do you return TRUE/FALSE with sed?
  2. How do you take the TRUE/FALSE and set an image (locally)?

Thanks!

1

There are 1 answers

2
Gilles Quénot On

Sed is not the better tool to do this, try doing this :

#!/bin/bash

out=$(curl -X GET -u 'username:password' http://www.reddit.com/api/me.json | ...)
if [[ $out == *TRUE* ]]; then
    echo 'Do something'
    exit 0
elif [[ $out == *FALSE* ]]; then
    echo 'Do another thing'
    exit 1
else
    echo >&2 "Command fails"
fi

or with :

curl ... | grep -q TRUE && do_Something || do_another_thing