getting xargs and mail to play nicely

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Wed Jun 15 13:44:47 UTC 2005


On Wed, Jun 15, 2005 at 09:40:34AM -0400, Peter King wrote:
> 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.

It was a good idea, if it wasn't for the fact mail requires using stdin,
which is something xargs doesn't let you do easily.

Lennart Sorensen
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list