If either of the files exist then send email alert in bash shell script

Trying to mock a little shell script.. There is a process that already exists which creates one of the two files (i.e, file1 or file2). My goal is to be able to send email alert depending on which file is created.

For example, if file1 exists then send email to John, David, Smith stating that file1 was created. If file2 exists then send an email to the same group of people (John, David, Smith ) stating that file2 was created.

If I write a simple if-else it will work but I will need to repeat some parts of the code which I am trying to avoid.

if [ -f "$file1" ]
        send email to three people mentioned 
        send email to three people mentioned saying file2 was created since it always creates two files 

Here I am trying to construct a script in a better way and also I don’t want to repeat “send email…” commands three times because I may need to add more people into the list in the future.

Please help. Thanks in advance.


Something like


userlist="$HOME/myusers.cfg" # one user per line

function send_alert {
   while read -r user comment; do
      echo "my command to send to ${user}"
      echo "subject=$1"
      echo "Content=Hello ${comment}n$2"
   done < "${userlist}"

if [ -f "$file1" ]; then
   send_alert "My subject" "Content: ${file1} exists."
   send_alert "My subject" "Content: ${file2} exists."

I added an optional field comment in the myusers.cfg, so you can add some comment after a space (name person, something).
You can adjust send_alert for more business rules, such as only send once an hour an alert, only send during working hours except for the person on night shift, whatever.