LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-09-2013, 01:21 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
programming logic required


Guys,

I need some help here. I have a directory which has "x" no of files.

For each file it has data separated by "," !!! wow !! that is nice , we got delimiter now that makes life easy !!!
Now all what I want to do is any row(each row has 6 fields separated by ",") if it has 3 rd field as "abcdefgh" then I want to send that file as an attachment to user.

I have implemented the logic but it seems it send emails(spam) but all i want is just one email with that file(sending with uuencode).

Here is the code.

Code:
cd $BASE_DIRECTORY

for i in $(find $BASE_DIRECTORY -type f -mtime -7)
do
while IFS="," read -r rec1 rec2 rec3 _
do
if [[ "$rec3" == "abcdefgh" ]]; then
        echo -e "$i is the requested file"
        uuencode $i $i|mailx -s "Invalid record $i file" test@testme.com
else
        echo "file $i is fine and does not need to sent"
fi
done<$i
done
 
Old 05-09-2013, 01:37 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
probably «break» just after mailx?
 
1 members found this post helpful.
Old 05-09-2013, 01:57 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@Ygrex

Thank you ! It did worked. Anything else cool stuff or smart thing we can do make script faster? or anything cool thing which you can think at this stage?
 
Old 05-09-2013, 03:07 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I prefer using 'mpack':
Code:
#!/bin/sh --
# Set debug mode when testing:
# set -vx
# Set default behaviour:
LANG=C; LC_ALL=C; export LANG LC_ALL
[ -z "${BASE_DIRECTORY}" ] && exit 127; cd "${BASE_DIRECTORY}" || exit 127
find "${BASE_DIRECTORY}" -type f -mtime -7|while read ITEM; do
 RES=$(awk -F',' '{if ($3 == "abcdefgh") print}' "${ITEM}")
 [ ${#RES} -eq 0 ] || mpack -s "Invalid record in \"${ITEM}\"" \
  "${ITEM}" test@testme.com
done
exit 0
 
1 members found this post helpful.
Old 05-09-2013, 03:38 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
grep -l <pattern> <filelist> will give you the name of those files, pattern will look like:
^[^,]*,[^,]*,abcdefgh,
 
1 members found this post helpful.
Old 05-09-2013, 03:57 AM   #6
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@unSpawn

Pretty down, shame we don't have mpack any other better alternative you can think of?
 
Old 05-09-2013, 03:58 AM   #7
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
unSpawn.

Impressed, I have never such a code before, can you explain every line of it?(f you don't mind?)
 
Old 05-09-2013, 04:06 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
sorry, I have no time to test it, but probably something like this:
Code:
grep -lR <pattern> "${BASE_DIRECTORY}" | xargs -n 1 send_mail
function send_mail
{ 
    uuencode $1 $1|mailx -s "Invalid record $1 file" test@testme.com

}
 
1 members found this post helpful.
Old 05-09-2013, 04:10 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by sysmicuser View Post
Pretty down, shame we don't have mpack any other better alternative you can think of?
Don't have, can't find or not allowed to install?


Quote:
Originally Posted by sysmicuser View Post
can you explain every line of it?
Sure:
Code:
# This is just a hedge as some tools tend to produce unexpected output if those environment variables are set differently:
LANG=C; LC_ALL=C; export LANG LC_ALL
# If the variable is empty then exit with a distinctive exit value:
[ -z "${BASE_DIRECTORY}" ] && exit 127
# If we can't cd into that directory then exit with a distinctive exit value:
cd "${BASE_DIRECTORY}" || exit 127
# Most of the time using a "while" loop is preferable [0]:
find "${BASE_DIRECTORY}" -type f -mtime -7|while read ITEM; do
 # Using the comma as separator check the third value for that string
 # and fill the variable with it.
 RES=$(awk -F',' '{if ($3 == "abcdefgh") print}' "${ITEM}")
 # if the variable isn't empty then do something with it:
 [ ${#RES} -eq 0 ] || mpack -s "Invalid record in \"${ITEM}\"" \
  "${ITEM}" test@testme.com
done
# Always exit nicely:
exit 0
0) http://mywiki.wooledge.orgDontReadLinesWithFor
1) Other common questions / problems:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.org/UsingFind
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
1 members found this post helpful.
Old 05-09-2013, 06:40 PM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@unSpawm.

How to do if I want to exit at the same time echo message and send it via email? means in addition to exit I want to echo some message and send some critical email. How to do?

---------- Post added 05-10-13 at 09:40 AM ----------

@unSpawn.

Not installed on our box.
 
Old 05-09-2013, 06:42 PM   #11
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
thank you pan64
 
Old 05-13-2013, 01:29 AM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by sysmicuser View Post
How to do if I want to exit at the same time echo message and send it via email?
You could simply?
Code:
if [ "${CONDITION}" = "match_this_condition" ]; then
 echo "Urgent message" | mail -s "Urgent" test@testme.com[B]
 exit 1
fi
 
1 members found this post helpful.
Old 08-14-2013, 11:24 PM   #13
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Thank you unSpawn
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 04:45 AM
Guidance on GTK+2.0 programming required. deepankar15 Programming 10 05-13-2009 12:56 PM
programming logic || scared centr0 Programming 12 07-10-2003 04:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration