Show Unread Gmails in Gnu Screen Status Bar

terminal.png

Below will show have to use a simple script that will fetch the total number of unread emails in your gmail account and display that in screen using backticks.

In your .screenrc file you need to have the following bolded section;

caption always “%{= kw}%-w%{= BW}%n %t%{-}%+w %-=   | %1` Unread |”

Also in your .screenrc file you need the following line:

backtick 1 60 60 /home/<username>/bin/get_gmail

in your home directory you should go ahead and create a bin directory if you do not already have one

mkdir ~/bin
cd bin/

Then use vim or emacs to create the file get_gmail in the bin directory.  The contents of the file should be as follows:

#!/bin/bash
gmail_login="username"
gmail_password="password"

fetch=”$(wget –secure-protocol=TLSv1 –timeout=3 -t 1 -q -O – https://${gmail_login}:${gmail_password}@mail.google.com/mail/feed/atom –no-check-certificate )”
line=”$(echo “$fetch” | grep ‘fullcount’)”
unread=”$(echo “$line” | sed “s/<fullcount>\(.*\)<\/fullcount>/\1/”)”

if [ -z "$unread" ]; then
echo “!”
else
echo “$unread”
fi

Now this file will contain your username and password in it so we’ll make the file only readable to the user:

chmod 700 get_gmail

You’ll want to test the script just to make sure.  It will display the number of emails if there are any unread OR it will display a ! if there’s none to find.  Once you’re sure the script is working correctly go ahead and fire up screen and enjoy.  Now you have cli gmail notifications.

Related Posts

4 Comments

  1. Hi Guys,

    Liked the script and its a lot better than i could have come up with, however it took me a wee while to make it work, so i thought i would share. I also made a couple of changes.

    Firstly there was a typo in the variable $fetch

    it reads;

    fetch=”$(wget –secure-protocol=TLSv1 –timeout=3 -t 1 -q -O – https://${gmail_login}:$(gmail_password}@mail.google.com/mail/feed/atom –no-check-certificate )”

    However the part containing

    {gmail_login}:$(gmail_password}

    should be

    {gmail_login}:${gmail_password}

    Just a curly bracket needed that was all. I also noticed in that line that you had an -O in wget but you named it -, i have TBH i just deleted that line, it made things complicated for me, and for me the output was named atom, not sure if that is the same for everyone or if its just cas of the ubuntu and wget i’m using.

    Which leads me on to the grep part of the script, due to the output option being – and it not being used in grep it couldn’t find anything i changed the line to this

    from;

    line=”$(echo “$fetch” | grep ‘fullcount’)”

    to;

    line=”$(echo “$fetch” | grep ‘fullcount’ atom)”

    Which leads me on the next change i made. I like the idea of having a message if there wasn’t a any unread messages but what i found was that the if statement would report 0 rather !. I presume its because the statement in effect is true, 0 is not being counted as null but as number. So i made the following change

    from;

    if [ -z "$unread" ]; then

    to;

    if [ "$unread" == 0 ]; then

    My logic was is the condition could only be met by one criteria which is that 0 unread messages

    and i also changed

    echo “!”

    to;

    echo “You Have No Unread Emails $gmail_login”

    and added;

    rm atom

    to the end of the script to remove un-needed file

    So my whole script looks like this now;

    #!/bin/bash

    # Gmail Vars, must be configured by user

    gmail_login=”gmailusername”
    gmail_password=”gmailpassword”

    # Calls

    fetch=”$(wget –secure-protocol=TLSv1 –timeout=3 -t 1 -q https://${gmail_login}:${gmail_password}@mail.google.com/mail/feed/atom –no-check-certificate )”

    line=”$(echo “$fetch” | grep ‘fullcount’ atom)”

    unread=”$(echo “$line” | sed “s/\(.*\)/\1/”)”

    # Checking and displaying unread emails

    if [ "$unread" == 0 ];

    then
    echo “You Have No Unread Emails $gmail_login”
    else
    echo “$unread”

    fi

    # removing un-needed files

    rm atom

    My tupence worth, please feel free to ignore the changes or build on them but i thought it be only fair seems as you did the donkey work on it to pass my changes back

    Yours

    Arron AKA finux