Date: Wed, 4 Sep 1996 15:45:19 +0000 (GMT) From: Gabor Zahemszky <zgabor@CoDe.hu> To: freebsd-questions@freebsd.org Cc: bextreme@m4.sprynet.com Subject: Re: Help with Scripts Message-ID: <199609041545.PAA00454@CoDe.CoDe.hu> In-Reply-To: <199609040032.RAA08169@m4.sprynet.com> from "Jesse" at Jan 10, 80 06:46:06 pm
next in thread | previous in thread | raw e-mail | index | archive | help
>
> Hello! If anyone knows of any good places to look for information on
> sh or csh scripts in FreeBSD I would greatly appreciate it.
>
> What I am trying to do is make a small script that will scan all the
> mail files in /var/mail for the keywords listed in a file, then if it
> finds any of those keywords, to move the mail file that it was in to
> a seperate directory (like /tmp/review), and copy a stock letter in
> it's place. It would need to run indefinatly in a constant loop. So
> far I have gotten far enough to search the mail files for the
> keywords using grep, however I don't know how to use IF correctly to
> see if grep actually found anything. If have checked all the manpages
> for info on csh and sh, but they where uninformative to say the
> least.
Hello!
1) If you would like to search for strings, it would be better to use
fgrep, not grep.
2) The grep family returns with a 0 status if it's found anything, and
non-0 if there are some errors, or not-found.
3) there are some comments in this list after a #
Here is a little script in sh.
---
#!/bin/sh
while true ; do # infinite loop
for i in /var/mail/* ; do # go throug the mail files
if fgrep -f /tmp/stringlist $i >/dev/null 2>&1 ; then # if we found anything
cat $i >> /tmp/review/`expr $i : '.*/\(.*\)'`
# cp $i /tmp/review
cp /dev/null $i ; mail `expr $i : '.*/\(.*\)'` < /tmp/stockletter
# cp /tmp/stockletter $i
fi
done
sleep 600
done
---
a) put your stringlist somewhere else, and modify the name in the list
(put one string per line, in that file)
b) -"- stockletter -"-
c) beware not to put any string from the stringlist file into the letter,
because it will give you an infinite ``found'' and new mail.
d) the line beginning with cat append the mails to the ``review''-file,
if you need to overwrite the file in ``review'' put a # in the beg. of the line,
and delete the # from the next line (cp)
e) the ``mail'' line send mail him/her, if would like put a file uncomment
this line, and comment the next ``cp'' line
f) I think, the e/f changes are very wrong method.
g) If you mail, beware any strings, generated by mailers.
h) I don't know, why do you need to run in infinitely, it would be better to
run it from crontab at about 5 minutes or once a day, etc. In that case, remove
the ``while true'' line, the ``sleep'' line and the last ``done'' line, too.
i) if you have bash/pdksh/ksh installed on your computer,
change the reference in the first line to the path to that shell,
and change the ``expr'' strings. Instead of:
`expr $i : '.*/\(.*\)'`
put
${i##*/}
(It will be quicker, and more unreadable.)
j) And the last line: maybe you will have some problems with mails, arriving
when your script is running. Maybe you have to think about some file locking
mechanism.
--
Gabor Zahemszky <zgabor@CoDe.hu>
-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-
Earth is the cradle of human sense, but you can't stay in the cradle forever.
Tsiolkovsky
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199609041545.PAA00454>
