#!/bin/sh -e usage() { cat >&2 << EOF usage: $(basename $0) options Postfix message queue grep utility by MW (v0.99). Help: -h Show this message Selection criteria (exclusive): -f Match sender address -r Match recipient address -s Match against the size in bytes -d Match against the date (format Mon Jan 30 10:16:15) Display options: -l Long Format [Default] -i Message IDs only Examples: $(basename $0) -f 'MAILER-DAEMON' -i $(basename $0) -r '.*@example\.com' EOF } LONGFORMAT=true while getopts ":hf:r:s:d:li" OPTION do case $OPTION in h) usage exit 0 ;; \?) echo "Invalid option: -$OPTARG" >&2 usage exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 usage exit 1 ;; f) MATCHFIELDS='$7' EXPR=$(echo $OPTARG | sed 's/\\/\\\\/g') ;; r) MATCHFIELDS='$8' EXPR=$(echo $OPTARG | sed 's/\\/\\\\/g') ;; s) MATCHFIELDS='$2' EXPR=$(echo $OPTARG | sed 's/\\/\\\\/g') ;; d) MATCHFIELDS='$3 " " $4 " " $5 " " $6' EXPR=$(echo $OPTARG | sed 's/\\/\\\\/g') ;; l) LONGFORMAT=true ;; i) LONGFORMAT=false ;; esac done if [ -z ${EXPR+x} ]; then usage exit 1 fi postqueue -p | tail -n+2 | head -n-1 | grep -v '^ *(' | awk \ 'BEGIN { RS = "" } { if ('"$MATCHFIELDS"' ~ /'"${EXPR}"'/ && $9 == "") print'"$( $LONGFORMAT || printf ' $1' )"'; } ' | tr -d '*!'