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
Script to count files and get the disk usgae on individual mail folders phpfreak Shell Programming and Scripting 1 05-17-2008 05:15 PM
two files as a message in mail ali560045 Shell Programming and Scripting 2 12-13-2007 02:53 AM
Script needed which will send a mail with attachment files Mar1006 Shell Programming and Scripting 4 11-24-2007 08:59 AM
Automatic Log files to my E-mail. adel8483 SUN Solaris 2 04-10-2007 05:10 AM
I need a script that script extract info from mail meravd Shell Programming and Scripting 1 10-19-2004 12:15 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-05-2007
earnstaf earnstaf is offline
Registered User
  
 

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

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

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

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

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

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Earnstaf,
You are confusing file name with file content.
The 'grep' command reads file contents.
The 'ls' command reads file names.
  #7 (permalink)  
Old 07-05-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
Quote:
Originally Posted by earnstaf View Post
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.
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:57 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