You are not logged in.

#1 2008-11-14 20:34:53

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Scripting Help

hey guys,

I think im on the right track here:

#adr=${1:-`whoami`}
#  Typing 'self-mailer.sh adr@someaddy

#cat $0 | mail -s "Feedback"  "$adr" |\   ##this sends mail to me

find ~/Mail/INBOX/Hello | grep ~/Hello "^/hello" -mv Hello

#echo "At `date`, script \"`basename $0`\" mailed to "$adr"firstname.lastname"
#exit 0

But what i actually need is to get those messages from my pine inbox, and put them in a folder on my desktop.
In the script imlooking for a pattern that finds messages that contain the subject, 'Hello'

Could someone help with this?

Last edited by oxoxo (2008-11-16 17:41:21)

Offline

#2 2008-11-14 20:59:48

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Scripting Help

If I understand what you're trying to do correctly:

#!/bin/sh
MAILDIR="/home/filip/Mail/Inbox"
TEXT="Hello"
DESTDIR="/home/filip/Desktop/test"

find "$MAILDIR" -type f | while read N
do
        cat "$N" | grep -q "$TEXT"
        if [ $? == 0 ]; then
                mv "$N" "$DESTDIR"
        fi
done

This script checks all the files in MAILDIR (including subdirectories) and then checks if any of these files contains text TEXT -- if TEXT is matched then the file gets moved to DESTDIR.

Offline

#3 2008-11-14 23:30:06

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Im glad that you understand. But let me be more specific.

I need to grab the mail from the Pine Inbox. I dont know of any directory that can specify that location and:

" pine -if inbox "  does take me where i want to go, but it is not a path.

Offline

#4 2008-11-14 23:48:42

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Scripting Help

Hmm, I've no experience with using pine, so I'm afraid I can't help with that...  But the script should work on any directory you define, so if you establish what the pine inbox directory is you should be able to just use it as $MAILDIR in the script.

Offline

#5 2008-11-16 17:29:15

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Hey,

So grep found the pattern that i needed and moved those messages from my pine inbox to different folder in my mail directory. Now, for some reason, its not working.

When it did work, all i did was email myself a message that matches what you had in the script for the greps pattern. I executed the command file and it worked right away. I checked my folder and it was there. This time i did the same thing and now nothing. Very Strange.

Offline

#6 2008-11-19 01:33:02

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

I figured out why its not working.

When im finding my mail in my MAILDIR, im finding 'files types' specificly the -type f argument for find.
Is there a way to search my message level? Not just the file level itself.

Offline

#7 2008-11-19 01:38:47

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Scripting Help

oxoxo wrote:

I figured out why its not working.

When im finding my mail in my MAILDIR, im finding 'files types' specificly the -type f argument for find.
Is there a way to search my message level? Not just the file level itself.

The find command can only work with individual files -- I'm not sure what you mean by "message level"...  If your mails are not in separate files then the find command is not going to be useful.

Offline

#8 2008-11-19 01:46:36

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Is there a way to transform the find command? Like using grep insted or something equivalent to what you gave me?
Its not working now, so i need to try something new when searching for the messages. The path that i specified is correct so it is in fact searching the correct path.

Offline

#9 2008-11-19 01:57:15

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Scripting Help

Are you using Maildir format for your mail, or mbox format?

mbox stores all messages per folder in one file, so it will be very difficult to move one message to another folder. I'd hazard to say almost impossible to do in a script using grep and find etc

Maildir stores each message in a different file, so that will make it easier.

Offline

#10 2008-11-19 02:12:46

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Maildir for my mail. And all my messages are in /var/spool/mail/name.lastname as seperate file names.
And from what im told is where my all my messages are stored.

Also mbox might store all the mail as one file. So im not going to worry about that.

Offline

#11 2008-11-19 02:40:47

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Scripting Help

In that case, fwojciec's script should work...?

Offline

#12 2008-11-19 03:06:44

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Ok..i know whats happening.

What happens is if ONE FILE is named "Hello" EVERYTHING will be moved to a seperate folder in my mail directory.

Im trying now to just get the one file named "hello" in a seperate folder. NOT EVERYTHING because of ONE FILE.

Offline

#13 2008-11-19 03:19:36

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Scripting Help

A file with the same name as the string you're looking for won't be treated any differently to a file named "rumpelstiltskin"

Offline

#14 2008-11-19 03:26:06

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

No its not the same name.
If i have the files:

hello
zzzzz

Both of them will be moved.
I think it might be something to do with the control statements.
Also, in the script is there a way to turn off the case sensitivity?
I want to grab hello and Hello.

Offline

#15 2008-11-19 04:09:11

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Scripting Help

The logic of the above script is very simple.

1. find all *files* (-type f) in a given directory (MAILDIR)
2. for each file: check if it contains the given string (TEXT)
3. if and only if the file contains the specified string move the file to DESTDIR

So it doesn't matter what a file is called -- what matters is whether it is a file (not a directory or a link) and whether it contains the given string.

As far as making the script disregard the lower/upper case; something like this should work:

#!/bin/sh
MAILDIR="/home/filip/Mail/Inbox"
TEXT="Hello"
DESTDIR="/home/filip/Desktop/test"

TEXT=$(echo "$TEXT" | tr "[:upper:]" "[:lower:]")
[ ! -d "$DESTDIR" ] && mkdir "$DESTDIR"

find "$MAILDIR" -type f | while read N
do
        cat "$N" | tr "[:upper:]" "[:lower:]" | grep -q "$TEXT"
        if [ $? == 0 ]; then
                mv "$N" "$DESTDIR"
        fi
done

I also made the script create the DESTDIR in case it doesn't exist.

Offline

#16 2008-11-19 05:19:17

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Scripting Help

fwojciec wrote:

As far as making the script disregard the lower/upper case; something like this should work...

Ummm.  `grep -i` would be simpler tongue

Offline

#17 2008-11-19 05:26:14

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

3. if and only if the file contains the specified string move the file to DESTDIR.

-Thats not whats happening.

If i just have 1 file with the name, "Hello" and execute the script i will end up with the Hello file in my new folder and 0 messages.

If i have two files, say: "Hello" and "ZZZZZZ" it will move both files to the new folder.

If i have 3 Files: ZZZZ, YYYY, XXXX  the script wont do anything.

What im saying is that if their is a single occurence of the TEXT string it will move every file to DESTDIR.

**The Subject is what matters the most**

Similar to the procmail rule file:
:0
^Subject:.*Hello
helloFolder


I can tell your an expert with UNIX.
You can prolly tell that i am not.

Last edited by oxoxo (2008-11-19 05:30:16)

Offline

#18 2008-11-19 06:36:30

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Scripting Help

I don't Know what is happening.  The script does not care about names.  There is nothing in the script that in any way checks what the name of a file is.  What do you mean by names?

Procmail does not deal with file names, it seems to be dealing with the subject of an email rather than a file name.  Maybe this is the source of confusion?  Or are the files in MAILDIR called the same as the Subjects of the emails they contain?

Offline

#19 2008-11-19 06:39:50

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Scripting Help

fukawi2 wrote:
fwojciec wrote:

As far as making the script disregard the lower/upper case; something like this should work...

Ummm.  `grep -i` would be simpler tongue

The smiley looks eerily like your avatar tongue
You are right of course smile

#!/bin/sh
MAILDIR="/home/filip/Mail/Inbox"
TEXT="Hello"
DESTDIR="/home/filip/Desktop/test"

[ ! -d "$DESTDIR" ] && mkdir "$DESTDIR"

find "$MAILDIR" -type f | while read N
do
        cat "$N" | grep -iq "$TEXT"
        if [ $? == 0 ]; then
                mv "$N" "$DESTDIR"
        fi
done

Last edited by fwojciec (2008-11-19 06:45:12)

Offline

#20 2008-11-19 07:42:36

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

<sigh>


Thanks for your guys' help!

Offline

#21 2008-11-19 07:43:40

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Scripting Help

Perhaps change the script to something like this...?

#!/bin/sh
MAILDIR="/home/filip/Mail/Inbox"
TEXT="Hello"
DESTDIR="/home/filip/Desktop/test"

[ ! -d "$DESTDIR" ] && mkdir "$DESTDIR"

find "$MAILDIR" -type f | while read N
do
        cat "$N" | grep -iqE "^Subject:.*${TEXT}.*"
        if [ $? -eq 0 ]; then
                mv "$N" "$DESTDIR"
        fi
done

Problem found - we need to use '-eq' instead of '==' to compare integers smile

Offline

#22 2008-11-19 18:01:27

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Still moving everything when one instance of "hello" occurs.

#!/bin/bash

sed 's/^From/==END==\n==From==/'"$1" |\

sed -n 'H; ${g;s/\n/+=+/g;p}' |\

sed 's/==END==+=+==From==/\nFrom/g' |\

grep -E "$2" |\

sed 's/+=+/\n/g'

#Move files to DESTDIR

What im trying to do with that code is to read my Mbox now.
And somehow implement the sed commands that do what your program isnt

Offline

#23 2008-11-20 05:24:08

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Scripting Help

Allright so, This is what me and another had figured out:

#!/bin/bash

cat mbox | sed -r 's/From /END====From /' | sed -n 'H; ${g;s/\n/+=+/g;p}' |\
sed 's/END====From/END\nFrom /g' |\
egrep ".*Subject: Hello.*" |\
sed 's/+=+/\n/g' > ~/Mail/Hello

And it works so nice!

Also:

I have three files, .forward ( in home ) .procmailrc and general.rc ( in my .procmail directory ) And im wondering if i can get my general.rc file to do the same thing as the above code. My general.rc looks like this:

:0
* ^From:.*sumone@sumaddy.com
* ^Subject:.*hello
$HOME/Mail/Hello

What i think that file does is it looks for who the mail is from, whether or not hello is in the subject line and puts what it found into a Hello folder.

So my .forward looks like this:

"|IFS=' '&&exec usr/bin/procmail||exit 75 #username"

Now the most important file .procmailrc that ties everything together is as follows:

VERBOSE=off
MAILDIR=$HOME/Mail
PMDIR=$HOME/.procmail
DEFAULT=/var/spool/mail/username
LOGFILE=$PMDIR/log
INCLUDERC=$PMDIR/general.rc
#end of .procmailrc

All three of these files work together to do the same task as what i've been helped with.
Im not executing anything with these three files. At least i dont think so. ..

Offline

Board footer

Powered by FluxBB