The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing strings in seperate files orahi001 UNIX for Dummies Questions & Answers 2 03-27-2008 12:04 PM
where are my email log files mstarcom UNIX for Dummies Questions & Answers 3 11-28-2007 06:39 PM
Split File into seperate files eltinator Shell Programming and Scripting 4 08-03-2007 03:27 PM
How do I stop printf output from going into seperate txt files chrchcol Shell Programming and Scripting 12 07-26-2006 10:08 PM
Email files with files - wild character bobo UNIX for Dummies Questions & Answers 4 01-16-2006 10:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-06-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
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 (permalink)  
Old 07-06-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 07-06-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Quote:
Originally Posted by Shell_Life View Post
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 (permalink)  
Old 07-06-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 07-06-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Quote:
Originally Posted by Shell_Life View Post
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!

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 (permalink)  
Old 07-06-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Quote:
Originally Posted by Shell_Life View Post
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 (permalink)  
Old 07-06-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695

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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0