0

I want to search a file for two strings, and make it email me THE STRING it was searching for, only when it is not found. (e.g when it doesn't find the string 'FIRST_SEARCH_STRING', I want it to email me 'FIRST_SEARCH_STRING).

I can make it email the strings when found using:

grep -Ew 'FIRST_SEARCH_STRING|SECOND_SEARCH_STRING'|sendmail "subject" my@email.com
  • It's not clear what action needs to be taken when the first string is found? Should the subject line in the email contain '2nd str notfound` and vice-versa? Also what if none of the strings is found then what should be the subject line? – user218374 May 16 '17 at 6:45
  • i would like to have the string that was NOT found emailed to me. – Sushma Sharma May 16 '17 at 16:27
1
if ! grep -q 'pattern' /path/to/file; then
    mail -s 'pattern not found in file' user@example.com
else
    do_stuff
fi
  • perhaps a loop like for search in FIRST_SEARCH_STRING SECOND_SEARCH_STRING ..., so that they can get an email with the (non)-matching word? – Jeff Schaller May 15 '17 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.