#!/bin/bash mailfile=${MAIL:-/var/spool/mail/$LOGNAME} getusername () { awk -F : '{if ($1 == ENVIRON["LOGNAME"]) {sub (/,.*/, "", $5); print $5; }}' /etc/passwd } [ "$MAILNAME" ] || MAILNAME=$(getusername) export MAILNAME newmail () { cat $mailfile | formail -c -X From: -X Subject: -X To: -s awk ' /From: .* <.*@.*>/ { sub(/ <.*/, ""); FROM = substr($0, 7); next } /From: .*@.* <.*>/ { sub(/.*.*/, ""); FROM = $0; next } /From: / { FROM = substr($0, 7); next } /To: .* <.*@.*>/ { sub(/ <.*/, ""); TO = substr($0, 5); next } /To: .*@.* <.*>/ { sub(/.*.*/, ""); TO = $0; next } /To: / { TO = substr($0, 5); next } /Subject: / { SUBJECT = substr($0, 10) } END { sub(/^"/, "", FROM); sub(/"$/, "", FROM); sub(/^$/, "", FROM); if (FROM == ENVIRON["MAILNAME"]) { sub(/^"/, "", TO); sub(/"$/, "", TO); sub(/^$/, "", TO); FROM = sprintf("To %s", TO); } sub(/^ */, "", FROM); sub(/ *$/, "", FROM); sub(/^ */, "", SUBJECT);sub(/ *$/, "", SUBJECT); printf "%-16.16s: %s\n", FROM, SUBJECT; }' } usage () { cat <&2 Usage: $0 [-h] [-q] [-t] [-f filename] Display all mail message senders and subjects. -h -- generate this help listing -q -- generate no output, just a return value -t -- just indicate "You have mail" if appropriate, but no details -f -- indicate a mail file other than ${MAIL:-/var/spool/mail/$LOGNAME} Environment: MAILNAME is used to determine determine mail from you. If not set, the name field from /etc/passwd is used. If set, MAIL contains the default mailbox, if not /var/spool/mail/LOGNAME is used. Exit codes: 0 -- mail present, 1 -- no mail, 2 -- bad arguments EOF } TEST=false QUIET=false while getopts "f:hqt" arg do case $arg in f) mailfile=$OPTARG ;; q) QUIET=true ;; t) TEST=true ;; h) usage ; exit 2 ;; ?) usage ; exit 2 ;; esac done NEWMAIL=false newmail | grep -q . && NEWMAIL=true if ! $QUIET then if $NEWMAIL then if $TEST then echo "You have mail" else newmail fi fi fi $NEWMAIL