Email like files in seperate emails


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Email like files in seperate emails
# 1  
Old 07-06-2007
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:
Code:
file1-egress-filter
file2-ingress-filter
stuff1-egress-filter
stuff2-ingress-filter
...

The script is run with the filename given as arguments, such as:
Code:
 ./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:
Code:
#!/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!
# 2  
Old 07-06-2007
Earnstaf,
See if this works for you:
Code:
rm -f All_files.txt
for mEach in $@
do
  cat ${mEach}* >> All_Files.txt
done
mutt -a "All_files.txt" $email

# 3  
Old 07-06-2007
Quote:
Originally Posted by Shell_Life
Earnstaf,
See if this works for you:
Code:
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 (
Code:
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.
# 4  
Old 07-06-2007
See if this is what you want:
Code:
for mBase in $@
do
  mParms=''
  for mEach in ${mBase}*
  do
     mParms=${mParms}" -a ${mEach}"
  done
  mutt -s "${mBase}" ${mParms} $email
done

# 5  
Old 07-06-2007
Quote:
Originally Posted by Shell_Life
See if this is what you want:
Code:
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! Smilie

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.
# 6  
Old 07-06-2007
Quote:
Originally Posted by Shell_Life
See if this is what you want:
Code:
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
rm "$each".txt
done

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?
# 7  
Old 07-06-2007
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop for seperate files

For shell script. If I had two separate files, file.txt and file1.txt and each has just a list of names from the who command. How would I create an if loop to compare each name? (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Seperate Odd and Even numbers from 1 file to 2 files

Hey guys. I have been trying to figure out an easy way to seperate a liste of 150k numbers (10 digits) in a .txt file into odd and even numbers with each of their own files, for a project at work. I've tried Excel, but it was too much for it and it wasnt very simple. So i gave up after... (13 Replies)
Discussion started by: TranceC
13 Replies

3. Shell Programming and Scripting

Sending files to multiple emails

Hi All, I want to send each file to each email id as below. Instead of writing saparate 10 mail commands can we do it in a simple step. file1.csv to raghu.s@hps.com file2.csv to kiran.m@hps.com file3.csv to kenni.d@hps.com file4.csv to rani.d@hps.com file5.csv to sandya.s@hps.com... (2 Replies)
Discussion started by: ROCK_PLSQL
2 Replies

4. UNIX for Dummies Questions & Answers

Archived Emails in UNIX Executable Files

Hello, A while back someone"archived" my emails for me, so I didn't have to worry about my email account filling up with emails. I need to get back into those emails and view them. When I opent the folder it has several files. The largest being an "mbox", which I am assuming has all of... (3 Replies)
Discussion started by: shaffer1921
3 Replies

5. UNIX for Dummies Questions & Answers

looping through file and creating seperate files

Hi, My first post!! I have a files with header, something like this Header_Row AMC|D1|D2|D2 AAO|D3|D4|D5 AMC|D6|D7|D8 AAO|D9|D10|D11 . . . . . and millions fo records thereafter like this. I want to read the above file in a loop and write the lines having AMC into another... (1 Reply)
Discussion started by: amitbakre
1 Replies

6. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

7. UNIX for Dummies Questions & Answers

comparing strings in seperate files

Hello, I am comparing files with for mismatches using fgrep but I've run into a problem. fgrep -vf $file1 $file2 > mismatches.dat file1 and file2 both contain file names on each line file1 has filenames which are up to 92 characters long and contain the "$" char. example file name:... (2 Replies)
Discussion started by: orahi001
2 Replies

8. Shell Programming and Scripting

Split File into seperate files

Hi, So I have a text file which I want to separate into separate text files. I would use the split command but the problem here is that the text file is separated by delimiters. For example: blah blah blah ------ more text ----- and some more text So basically the first part should be... (4 Replies)
Discussion started by: eltinator
4 Replies

9. Shell Programming and Scripting

How do I stop printf output from going into seperate txt files

I am using printf "%-75s%+10s %5s %1s \n" $s $z $x $y > status It works really well, however, when I email status it is sending 10 emails when I would like it to be in one. Is there a way to make all the output to go into one instance of a txt file. Yet still keep it 1 on each... (12 Replies)
Discussion started by: chrchcol
12 Replies
Login or Register to Ask a Question