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
  • 197,895
  • 55
  • 485
  • 740

2 Answers2

3

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 or precisely after the prompt is set):

UNREAD="$(mailx <<< q | head -n2 | tail -n1 | cut -f4 -d" ")"

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

We wrote a variable that reads the count of unread mails using the mailx program.

Explanation of command inside the variable:

mailx <<< q

We pass "q" to the stdin of mailx so it quits immediately after the prompt is displayed.

head -n2 | tail -n1

from the first outputted 2 lines we only want the first line

cut -f4 -d" "

The mail count report goes:

/var/mail/alex: X messages Y unread

We want the fourth space-separated word (Y).

Now we have the number of unread messages inside the UNREAD variable, but it could be empty if there is no unread messages where the message would be shorter, i.e.:

/var/mail/alex: X messages

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.

elig
  • 103
  • 4
αғsнιη
  • 35,660
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))

[ &quot;$PWD&quot; != &quot;$HOME&quot; ] &amp;&amp; pwd=${PWD/#$HOME\//\~\/}

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

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

# echo &quot;seconds: $SECONDS&quot;
# echo &quot;check: $MAIL_CHECK_TIME&quot;
# echo &quot;elapsed: $MAIL_ELAPSED_SECONDS&quot;

}

uses mx linux /etc/profile.d prompt hook

PROMPT_COMMAND="mypromt"