: ########################################################################## # Title : mailgrep - search mail folder, print mail text # Author : Heiner Steven # Date : 2003-11-19 # Requires : formail, grepcat # Category : Mail # SCCS-Id. : @(#) mailgrep 2.2 06/03/15 ########################################################################## # Description # ########################################################################## PN=`basename "$0"` # Program name VER='2.2' usage () { echo >&2 "$PN - search mail folder for pattern, $VER usage: $PN [-ilv] pattern [mailfolder ...] -i: ignore character case -l: only list file names matching the pattern -v: inverse sense of searching: print mails NOT matching pattern Searches mail folders for regular expression (like \"grep\"), but prints the whole mail message if it matches the pattern (\"grep\" would only print the matching line)." exit 1 } msg () { echo >&2 "$PN:" "$@" } fatal () { msg "$@"; exit 1; } ########################################################################## while getopts :hilv opt do case "$opt" in [ivl]) grepflags="${grepflags+$grepflags }-$opt";; h) usage;; *) usage;; esac done shift `expr $OPTIND - 1` || exit : ' ------------------------------------------------------------------------- # Uncomment the following lines if your shell does not have a build-in # "getopts" command. Note that "getopt" will strip whitespace from # the arguments. #set -- `getopt :h "$@"` || usage #[ $# -lt 1 ] && usage # "getopt" detected an error grepflags= while [ $# -gt 0 ] do case "$1" in -i) grepflags="${grepflags+$grepflags }-i";; --) shift; break;; -h) usage;; -*) usage;; *) break;; # First file name esac shift done ------------------------------------------------------------------------- ' [ $# -lt 1 ] && usage Search=$1; shift if [ $# -lt 1 ] then # Determine user name. Example "id" output: uid=1000(heiner) gid=100(users) : ${USER:=`expr "\`LANG=C id\`" : 'uid=[0-9]*(\([^)]*\).*'`} set -- `ls "/var/mail/$USER" "/usr/spool/mail/$USER" 2>/dev/null` fi tmp=${TMPDIR:=/tmp}mg.$$ trap 'rm -f "$tmp"' 0 trap "exit 2" 1 2 3 15 for folder do egrep -l $grepflags "$Search" "$folder" >/dev/null || continue formail -cs grepcat $grepflags "$Search" < "$folder" done exit 0