Shell Person Help me keep the shell people alive.

21Jun/095

Quickly Check Gmail with BASH

bells

A very simple script to check if I have any new gmail:

#!/bin/bash
## Quickly checks if I have new gmail

echo -e "Checking for new messages... \c"

atomlines=`wget -T 3 -t 1 -q --secure-protocol=TLSv1 \
 --no-check-certificate \
 --user=USERNAME --password=PASSWORD \
 https://mail.google.com/mail/feed/atom -O - \
 | wc -l`

echo -e "\r\c"

[ $atomlines -gt "8" ] \
 && echo -e " You have new gmail.  \c" \
 || echo -e " No new gmail.  \c"

Just replace USERNAME and PASSWORD with your information (caution - your password is stored in plain text).  I use colors on mine, but I'm not sure I can correctly post the requisite special character in WordPress.  Here's what it looks like in gedit.

gedit

I can make that special character in gnome-terminal by pressing CTRL+v and then CTRL+[
There's probably an easier way to use colors, I just don't know it.

In case it's not clear from the script, this just downloads the atom feed (an XML file) and checks if it's more than eight lines long.  If it is, then you have unread mail.  (At least on my account, when my inbox has no unread items, the atom feed is eight lines long).  The number may need to be adjusted if gmail changes the format of their feeds.

Comments (5) Trackbacks (1)
  1. You can shorten that wget line a fair bit with curl:
    curl -su user:pass https://mail.google.com/mail/feed/atom

  2. Thanks Adam, I’ve never used curl for anything before, but I’ll check it out.

  3. Adam rocks!!! curl is clean,fast and simple. thanks.

  4. Good stuff!
    You may also hide your password by using something like:
    echo -n “password:”
    read -es mypass

    then changing your wget to:
    atomlines=`wget -T 3 -t 1 -q –secure-protocol=TLSv1 \
    –no-check-certificate \
    –user=USERNAME –password=$mypass

    Cheers!

  5. VERY NICE!


Leave a comment