Email like files in seperate emails

My goal is to send multiple files to a person based on their input. The files have similar names like:

file1-egress-filter
file2-ingress-filter
stuff1-egress-filter
stuff2-ingress-filter
...

The script is run with the filename given as arguments, such as:

 ./mail.sh file stuff

would email all of the above files.

The script I have currently does email all the files, but it puts each into it's own email and I would like file1 and file2 grouped and stuff1 and stuff2 grouped, etc. Here's what I have:

#!/bin/bash
TFILE="/tmp/$(basename $0).$$.tmp"
> $TFILE
clear
     echo "Who would you like to receive the email?"
     echo "[1] Person1"
     echo "[2] Person2"
     echo "[3] Other"
     read choice
     case $choice in
             1) email="person1@place.com" ;;
             2) email="person2@place.com" ;;
             3) echo -n "What email address would you like to send the files to? " ; read email
     esac

array=( $* )
clear
for file in ${array[@]:0}
do
ls $file*-filter >> $TFILE
done

files=`cat $TFILE`
for file in $files
do
cp $file "$file".txt
echo "Mailing" "$file".txt
mutt -s "$file" -a "$file".txt $email \
< /dev/null
rm "$file".txt
done
rm $TFILE

I think that should be pretty easy to read through, but if there is a better way to go about that portion, I'd be very happy to learn a better way.

As stated above, my main question is, is it possible to differentiate between file1 and 2 and stuff1 and 2 to put them in different emails via multiple -a flags on the mutt line.

I hope that was all coherent. Thanks for your help!

Earnstaf,
See if this works for you:

rm -f All_files.txt
for mEach in $@
do
  cat ${mEach}* >> All_Files.txt
done
mutt -a "All_files.txt" $email

Shell Life, wouldn't that just join all the files into a single file and email that?
I want one email to go out with file1 and file2 attached (

mutt -s "Files" -a file1 -a file2 $email

) and another email with stuff1 and stuff2 attached.

Is it possible to make the loop differentiate? I was thinking of holding the file it just emailed in the current loop to a variable, and then test that against the next file it was going to email and if they match, put them in the same email but if not, start a new mutt line.

I'm stepping a little above my head with that, and I'm not sure it's even possible.

See if this is what you want:

for mBase in $@
do
  mParms=''
  for mEach in ${mBase}*
  do
     mParms=${mParms}" -a ${mEach}"
  done
  mutt -s "${mBase}" ${mParms} $email
done

Wow, very nice Shell_Life... you never let me down! :slight_smile:

The nested loops really ..."threw me for a loop" at first, but I echoed the var after each one and I got it now. That is very nice. I think I'm finally learning the syntax and such, I just need to develop the ability to "think outside the box" on things such as this.

Anyways, thanks again.

One quick issue. I modified your script as shown above. The problem is I need to delete the .txt files that it's creating, but at the bottom of the loops where I rm it, the variable only has one file or the other (file1 or file2) thus leaving the other.

I don't feel safe doing rm *.txt ... ideas?

for mBase in $@
do
  mParms=''
  for mEach in ${mBase}*-filter
  do
    cp ${each} ${each}.txt
     mParms=${mParms}" -a ${mEach}.txt"
  done
  echo "Mailing" $base
  mutt -s "${mBase}" ${mParms} $email
  for mEach in ${mBase}*-filter
  do
    rm -f ${each}.txt
  done
done