getting xargs and mail to play nicely

Peter King peter.king-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Wed Jun 15 13:40:34 UTC 2005


Several solutions were proposed to my original inquiry; all of them
work, and none of them use xargs -- which I suppose is therefore the
wrong tool for the job.

The solutions were of two types: those that used a shell looping construct
(either "while" or "for"), in a script or on an ugly command-line, and
those from Jim Prior that used sed and awk.

The first type of solution, in a shell script:

[1]   grep @ addressfile |
           while read addr
           do
              mail -s SUBJECT "$addr" < message
           done

Grepping allows all sorts of comments etc. in the addressfile; clearly we
could declare variables for $subject, $message, and get some measure of
generality.

[2] For those fluent in sed/awk, here are Jim's solutions:

   grep @ addressfile | sed -e 's/^/mail -s SUBJECT /' -e 's/$/ <messagefile/' | bash

   grep @ addressfile | sed -e 's/^/mail -s SUBJECT '\''/' -e 's/$/'\'' <messagefile/' | bash

   grep @ addressfile | awk '{print "mail -s SUBJECT '\''"$0"'\'' <messagefile"}' | bash

These are clever ways of getting the right invocations of mail, one per line,
sent to bash.

I'm inclined to think solutions of the first sort are "better" in that they
require invoking grep and mail for each line of the addressfile, whereas the
second sort requires invoking grep, sed/awk, and mail. But all this is in shell
and quick-n-dirty in the first place, so the efficiency gain doesn't matter.

Thanks to all who responded! My instincts to use xargs -- not one of the more
common commands -- were awry.

-- 
Peter King			 	peter.king-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Department of Philosophy
215 Huron Street
The University of Toronto		    (416)-978-3788 ofc
Toronto, ON  M5S 1A1
       CANADA

http://individual.utoronto.ca/pking/

=========================================================================
GPG keyID 0x7587EC42 (2B14 A355 46BC 2A16 D0BC  36F5 1FE6 D32A 7587 EC42)
gpg --keyserver pgp.mit.edu --recv-keys 7587EC42
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
URL: <http://gtalug.org/pipermail/legacy/attachments/20050615/278904e9/attachment.sig>


More information about the Legacy mailing list