the devmin quest

linux administration-development tips and tricks

Si vous cherchez mon site professionnel, merci de cliquer ici.

new mail alert in bash prompt


Bash has a mailcheck feature supposed to warn you whenever you received a new mail. This use the $MAIL, $MAILCHECK and $MAILPATH environment variables. I had many problems with this. Most of the time, it only warns me on login, and sometimes not at all, making it quite unreliable.This article discusses a simple way to include a check for new mails directly in the bash prompt.

The idea is to create a cache file for your mailbox. You make an alias for your mail reader (mutt, in my case) that will touch the cache files when the reader is launched. Then, in your bash prompt, you call a script that will verify if the mailbox file is newer than the cache file. And that’s all. Here is what I use.

/usr/local/bin/newmail

 #!/usr/bin/env bash

MAILDIR=$HOME/Mail

if [[ -d $MAILDIR ]]; then
  for file in $MAILDIR/*; do

    cache_file="$MAILDIR/.$(basename $file)"
    if [[ ! -e $cache_file ]]; then
      touch $cache_file
    fi

    if [[ $file -nt $cache_file ]]; then
      echo -e "new mail in $file\n "
    fi

  done
fi

~/.bashrc

 PS1="\$(/usr/local/bin/newmail)\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] "
alias mutt="touch $HOME/Mail/.* && mutt"

Don’t omit the backslash for the newmail subshell, or it will be only executed once, on startup.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Technorati






Write a Comment

Note: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>