Mail Files Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mail Files Script
# 1  
Old 07-05-2007
Mail Files Script

Please let me know if I'm the right track here, or how it should be done better.
Code:
#!/bin/bash
list=`ls -al /some/directory | grep "$1"* | awk '{print $9}'`

for file in $list
do
mail -s "$file" me@email.com < $file
done

My concern is with the list variable. The output is like:
Code:
file1
file2
file3

Does the for loop keep each of those filenames in order to loop through?

Thoughts?
# 2  
Old 07-05-2007
Earnstaf,
To answer your question, yes, it does.
Keep in mind that the best way to learn is testing it.
Run the following:
Code:
list=`ls -al /some/directory | grep "$1"* | awk '{print $9}'`
for file in $list
do
  echo "Working with file = "$file
  #####mail -s "$file" me@email.com < $file
done

# 3  
Old 07-05-2007
Thanks Shell Life. I'm getting some unwanted results. I've modified the script to look like this:
Code:
#!/bin/bash
list=`ls /some/directory | grep "$1"*`
echo $list
echo "starting loop"
for file in $list
do
echo "Mailing" $file
#mail -s .......
done

I run the code with
Code:
./mail.sh file1

Output is:
Code:
starting loop

So it's not expanding the list variable.
# 4  
Old 07-05-2007
I understand your problem now.
Here is the solution:
Code:
#!/bin/bash
list=`ls /some/directory/$1*`
echo $list
echo "starting loop"
for file in $list
do
  echo "Mailing" $file
  #mail -s .......
done

# 5  
Old 07-05-2007
Very good, it works that way Shell Life.

Is it not possible to pipe within the variable expansion?

Edit: or if I have to make it more specific, like if I need to do something like "grep "$1.*-ingress-filter" to restrict the output to the list variable.
Edit 2:
I've tried the following but it doesnt seem to do the trick.
I set list as you specified above, then this:
Code:
list2=`echo $base | grep "$1.*-filter"`

Ok, I just did:
Code:
list=`ls /some/directory/$1*-filter`

and that works for me. I'll read up on the variable expansion and pipes.

Last edited by earnstaf; 07-05-2007 at 05:40 PM..
# 6  
Old 07-05-2007
Earnstaf,
You are confusing file name with file content.
The 'grep' command reads file contents.
The 'ls' command reads file names.
# 7  
Old 07-05-2007
Quote:
Originally Posted by earnstaf
Thanks Shell Life. I'm getting some unwanted results. I've modified the script to look like this:
Code:
#!/bin/bash
list=`ls /some/directory | grep "$1"*`
echo $list
echo "starting loop"
for file in $list
do
echo "Mailing" $file
#mail -s .......
done

I run the code with
Code:
./mail.sh file1

Output is:
Code:
starting loop

So it's not expanding the list variable.
I think that the problem comes from the pattern * of your grep command.
The shell expand "$1"* to files starting with the value of $1 in your current directory.
Remove the * :
Code:
list=`ls /some/directory | grep "$1"`

The result is the list of the files in /some/directory which contain $1 in their name.

The result given by Shell_Life' solution is the list of the files in /some/directoty which have a their name starting with $1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Shell Programming and Scripting

Script to attach latest files of directories in a mail.

Hello Folks, I am looking for the script which will go to directory and check for the latest 5 files and send a mail to attaches these files to user. Kindly guide. Regards (7 Replies)
Discussion started by: sadique.manzar
7 Replies

3. Shell Programming and Scripting

Script that gathers specific values from files and puts it into HTML to send e-mail

Hi All, Sorry for long topic here. So the drill goes like that, I need a script which gathers different values from different files/locations. A_CT=`cat a.dat | awk -F'|' '{print $1}' >> report.txt` B_CT=`cat b.dat | awk -F'|' '{print $3}' >> report.txt` C_CT=`cat c.dat | awk -F'|'... (4 Replies)
Discussion started by: shivakid
4 Replies

4. Shell Programming and Scripting

Script to mail if files do not exist

Hi all! I need some help with a script, but I want to make it better, my script prints the last two files on each directory: echo " " echo "******************* mediation_sgsn:***********************" ls -lrt /moneta_collected03/mediation_sgsn | tail -2 echo " " echo... (6 Replies)
Discussion started by: fretagi
6 Replies

5. Shell Programming and Scripting

Script for checking files for last hour and send a mail

Hello, I need to write a script to check the files in one folder , if that folder doesn't have files generated from last 1 hr then we need to send mail to particular persons. So Can you please help me to write script to check the files and send email. Thank you.. (1 Reply)
Discussion started by: archana23
1 Replies

6. OS X (Apple)

Script to process winmail.dat files in apple mail from outlook senders

Does anyone know of a way to automate the "massaging" of those STUPID winmail.dat files that come to Apple Mail extensions as a result from people sending mail from outlook clients? Specifically, something that would run when you attempt to open a winmail.dat file and auto extract and open... (2 Replies)
Discussion started by: herot
2 Replies

7. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

8. Shell Programming and Scripting

script to read mail on a mail server

Hi All, Is there any way Unix shell script can directly read a mail on a mail server (for eg. abc@xyz.com) and save it as a text file in a unix directory ? Thanks PRKS ---------- Post updated at 08:37 AM ---------- Previous update was at 08:36 AM ---------- I am using ksh (1 Reply)
Discussion started by: PRKS
1 Replies

9. Shell Programming and Scripting

Script to count files and get the disk usgae on individual mail folders

I have several mail accounts using dovecot maildr format for which I'd like to create a script so that when I run it, for each user it prints not only the name of the user but all of the folders contained and outputs each of the names of the folders created by the user (including the default cur,... (1 Reply)
Discussion started by: phpfreak
1 Replies

10. Shell Programming and Scripting

Script needed which will send a mail with attachment files

Hello, I need a shell script which need to send a mail with 4 *.csv files as attachments.Anybody can help me? At present i have a script which is sending only one file as attachments.But i need a script which send 4 files in a shot. #!/bin/ksh /bin/mail xxxx@xx.xom << EOF Subject:... (4 Replies)
Discussion started by: Mar1006
4 Replies
Login or Register to Ask a Question