3

I want to see the number of unread messages in my unix mail messages account in the command prompt. How can I do this?

Update: Here is the output from mail and mailx when I have sent myself two messages:

"/var/mail/alex": 2 messages 2 new 
 N   1 Alex Thu Sep 25 16:53  13/420   subject for askubuntu 1 
 N   2 Alex Thu Sep 25 16:53  13/420   subject for askubuntu 2 
muru
  • 193,181
  • 53
  • 473
  • 722
alexoakley1
  • 133
  • 6

2 Answers2

2

Open gedit ~/.bashrc file and add this script at the end of that (don't forget you have to add this script at the END of file):

NewMail(){
    NEWMAIL=$(mailx &)
    UNREAD=$(echo $NEWMAIL |grep -o 'messages.*new' | cut -f2 -d" ")
}
NewMail # call NewMail function

if [ -n "$UNREAD" ]; then
    PS1="[Hi, alex. you have $UNREAD new mail(s)] $PS1"
fi

We wrote a function whose name is NewMail.

Explanation of function:

The result of first line(NEWMAIL=$(mailx)) according to you information in body of question are:

"/var/mail/alex": 2 messages 2 new [...]    # if you have a new unread mail

OR

No mail for alex                            # if you don't have a new mail

That I stored the above result in a NEWMAIL variable there.

I passed the result into next command in second line with this command echo $NEWMAIL.

Then with this command grep -o 'messages.*new' I get the result just between "message" word and "new" word, which is: messages 2 new.

  • -o - Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Now we have this messages 2 new result. Then I pass this result to next command cut -f2 -d" ", to getting just number of messages:

  • -f flag used for fields and -f2 select only second field from "messages 2 new"
  • -d" " with one space here is used for field delimiter.

Then after running second line, we have the number of new unread mail and that is: 2 here.

In this NewMail # call NewMail function line, we call the NewMail function.

With this if condition, I check if you have new mail. Then the command prompt will change and shows: "[Hi, alex. you have # new mail(s)]" before the prompt. If you have not got any new mail then the command prompt will not change.

if [ -n "$UNREAD" ]; then 
    PS1="[Hi, alex. you have $UNREAD new mail(s)] $PS1"
fi

-n flag with [ -n "$UNREAD" ] checks if the length of UNREAD is Not zero.

PS1 is defines your command prompt that configure into .bashrc file in your home directory. This is what we open/edit this file. Then I edit that to include unread message count before your command prompt. See:

PS1="[Hi, alex. you have $UNREAD new mail(s)] $PS1"

Here's the screenshot if I have new mails:

enter image description here

And if I have not got any new mail:

enter image description here

That's it. Just copy and paste the script at the end of your ~/.bashrc file.

αғsнιη
  • 35,092
  • 41
  • 129
  • 192
0

here's a bash function that does this: bash - Local mail is (almost) never checked in gnome-terminal, how can I change that? - Ask Ubuntu

MAIL_CHECK_TIME=0
mypromt()
{
    local pwd='~'
    local MAIL_SECONDS_DIFF=10

    local MAIL_ELAPSED_SECONDS=$((SECONDS - MAIL_CHECK_TIME))

    [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}

    printf "\033]0;%s@%s:%s\033\\%s" "${USER}" "${HOSTNAME%%.*}" "${pwd}"

    # if [ ! "$SSH_CONNECTION" ]; then
        # if [ "$MAIL_CHECK_TIME" -eq "0" ] || [ "$MAIL_ELAPSED_SECONDS" -gt "$MAIL_SECONDS_DIFF" ]; then
        if [[ "$MAIL_CHECK_TIME" -eq "0" || "$MAIL_ELAPSED_SECONDS" -gt "$MAIL_SECONDS_DIFF" ]]; then
            local MAILX="$(mailx 2>/dev/null &)"
            local COUNT=$(echo "$MAILX" | wc -l)
            local COUNT=$((COUNT-3))
            local MESSAGE_TEXT="message"
            if [ "$COUNT" -gt "0" ]; then
                if [ "$COUNT" -gt "1" ];then
                    MESSAGE_TEXT="messages"
                fi
                printf "You have $COUNT mail $MESSAGE_TEXT.\n \033]0;%s@%s:%s\033\\%s" "${USER}" "${HOSTNAME%%.*}" "${pwd}"
            fi
            MAIL_CHECK_TIME=$SECONDS
        fi
    # fi

    # echo "seconds: $SECONDS"
    # echo "check: $MAIL_CHECK_TIME"
    # echo "elapsed: $MAIL_ELAPSED_SECONDS"
}
# uses mx  linux /etc/profile.d prompt hook
PROMPT_COMMAND="mypromt"