LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   programming logic required (https://www.linuxquestions.org/questions/linux-newbie-8/programming-logic-required-4175461243/)

sysmicuser 05-09-2013 01:21 AM

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


Ygrex 05-09-2013 01:37 AM

probably «break» just after mailx?

sysmicuser 05-09-2013 01:57 AM

@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?

unSpawn 05-09-2013 03:07 AM

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


pan64 05-09-2013 03:38 AM

grep -l <pattern> <filelist> will give you the name of those files, pattern will look like:
^[^,]*,[^,]*,abcdefgh,

sysmicuser 05-09-2013 03:57 AM

@unSpawn

Pretty down, shame we don't have mpack any other better alternative you can think of?

sysmicuser 05-09-2013 03:58 AM

unSpawn.

Impressed, I have never such a code before, can you explain every line of it?(f you don't mind?)

pan64 05-09-2013 04:06 AM

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

}


unSpawn 05-09-2013 04:10 AM

Quote:

Originally Posted by sysmicuser (Post 4947729)
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 (Post 4947731)
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

sysmicuser 05-09-2013 06:40 PM

@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.

sysmicuser 05-09-2013 06:42 PM

thank you pan64 :)

unSpawn 05-13-2013 01:29 AM

Quote:

Originally Posted by sysmicuser (Post 4948180)
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


sysmicuser 08-14-2013 11:24 PM

Thank you unSpawn :)


All times are GMT -5. The time now is 02:34 PM.