From owner-freebsd-questions Wed Sep 4 06:57:36 1996 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id GAA12416 for questions-outgoing; Wed, 4 Sep 1996 06:57:36 -0700 (PDT) Received: from mail.EUnet.hu (mail.eunet.hu [193.225.28.100]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id GAA12410 for ; Wed, 4 Sep 1996 06:57:32 -0700 (PDT) Received: by mail.EUnet.hu, id PAA12042; Wed, 4 Sep 1996 15:57:23 +0200 Received: by CoDe.CoDe.hu (PAA00454); Wed, 4 Sep 1996 15:45:19 GMT From: Gabor Zahemszky Message-Id: <199609041545.PAA00454@CoDe.CoDe.hu> Subject: Re: Help with Scripts To: freebsd-questions@freebsd.org Date: Wed, 4 Sep 1996 15:45:19 +0000 (GMT) Cc: bextreme@m4.sprynet.com In-Reply-To: <199609040032.RAA08169@m4.sprynet.com> from "Jesse" at Jan 10, 80 06:46:06 pm X-Mailer: ELM [version 2.4 PL24] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk > > 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 -:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:- Earth is the cradle of human sense, but you can't stay in the cradle forever. Tsiolkovsky