#!/bin/bash USERNAME="$1" [ -z "$USERNAME" ] && echo "Usage : $0 [username] [-d]" && exit 1 DEBUG=0 [ "$2" == "-d" ] && DEBUG=1 MAX_LOG=9 function search_user () { USER="$1" MAILLOG="$2" [ ! -f "$MAILLOG" ] && MAILLOG="$MAILLOG.gz" [ -f "$MAILLOG" ] && zgrep "sasl_username=$1" $MAILLOG|sed 's/.*: \([A-Z0-9]*\): client=.*$/\1/' } search_user $USERNAME /var/log/mail.log for i in $( seq 1 $MAX_LOG ) do search_user $USERNAME /var/log/mail.log.$i done