So far with:


tail -fn0 /var/log/dirsrv/redacted-domain/audit | \
while read line ; do
        echo "$line" | grep -C 5 -i "add: member"

        if [ $? = 0 ]
                 mail -s "New User Added" [email protected] > /dev/null
        echo "$line" | grep -C 5 -i "add: nsaccountlock"

        if [ $? = 0 ]
                 mail -s "Account Disabled" [email protected] > /dev/null

I want to take the contents of grep matches from the file /var/log/dirsrv/redacted-domain/audit and throw them into an e-mail to [email protected]. It feels redundant (and does not work regardless) to throw the grep statement into the email itself with the above loop.

Sorry, I should include a sample of what is being grepped:

time: 20200114133315 dn: cn=users,cn=groups,cn=accounts,dc=ipa,dc=domain,dc=com result: 0 changetype: modify add: member member: uid=example_user,cn=users,cn=accounts,dc=ipa,dc=domain,dc=com

I want to find a match of type add: member and return the entire ^ into an email.

1 Answer 1


You have two options.

One is to send email as you are doing (https://stackoverflow.com/questions/5155923/sending-a-mail-from-a-linux-shell-script)

cat /path/to/file | mail -s "your subject" [email protected]

I suppose you do this on regular basis, and you send this e-mail from some crontab script. In that case type at the top of your crontab [email protected].

[email protected]
0 * * * * /path/to/your/script

In that case server will send an email where the content will be whatever your script outputs on standard output. If script prints nothing the e-mail will not be sent. In that way you do not have to deal with mail program in a script to send an e-mail. Just let your grep print matching lines and they will be sent via e-mail. The problem is, you cannot change the subject that way, but an additional line in content would also be OK, I guess.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .