shell script being called by filter

shell script being called by filter

Post by Peter Samuels » Sat, 23 May 1998 04:00:00




Quote:> I'm a relative newbie to the world of shell scripts (so please go
> easy on me!).

Hey -- at least you're asking about shell scripts in this group, not
about getting a free ISP shell account, which is what a lot of people
seem to think this group is about....

Quote:> I've got a script (standard run of the mill sh) that is called by
> filter (the ELM variety).  It functions fine as it is, but I'm
> wanting to add components to the script itself.  Specifically I'm
> wanting to script to send me a copy of the mail.

So far so good.

Quote:> So far, I've got the syntax down to

> elm -s $subjstr $recip < file_containing_message

Using elm from the command line to send mail is not really necessary
unless you want it to save a copy of the message in your `out' folder,
or do its own alias processing.  That's probably all it does before
calling sendmail, so if you want to skip the middle man you can run
/usr/sbin/sendmail (or /usr/lib/sendmail or wherever your sendmail
lives) directly, with those same arguments.

Note also that you should almost never send bare variables to a command
line -- if, for example, $subjstr has spaces in it (which most subject
lines do) then everything after the first space will be taken as email
addresses.  Use double quotes:

  /usr/sbin/sendmail -s "$subjstr" "$recip" < file_containing_message

Quote:> Now my problem is how to get the message into the file.

> I figured I'd use the echo method, but I can't figure out what to echo.
> $1 and $2 only yield the parameters given to the script, and $3 is
> empty.

Right.  A filter in Unix is a program that takes standard input for its
data, so you just need to send standard input to sendmail.  You can
put standard input into file_containing_message with

  cat > file_containing_message

but there's no reason to; if you haven't touched the input yet you can
just pass it on to sendmail directly:

  /usr/sbin/sendmail -s "$subjstr" "$recip"

Now, unfortunately, you no longer have any input, so whatever else the
script wanted to do with it it can't.  If that's the case, you can just
use that `cat' command above (no, don't panic, this isn't a Useless Use
of Cat) and redirect the resultant file into whatever commands you
wish, including sendmail.  Also you probably want to clean up after
yourself, so the following line will remove the file when the script
exits:

  trap 'rm -f file_containing_message' 0

One last note: do not use the filename "file_containing_message", or
you'll start losing mail.  The reason is if you get two messages really
close together you'll have two copies of your script running at once,
and they will clobber each other's files.  The quick 'n' dirty way to
generate a unique filename is with the $$ variable (your PID number):

  msgfile=/tmp/file_containing_message.$$

Put it all together (with two extra lines that add some security):

  #!/bin/sh

  umask 077
  trap 'rm -f $msgfile' 0
  msgfile=/tmp/elm_filter.$$
  while [ -e $msgfile ]; do msgfile=/tmp/elm_filter.`sh -c 'echo $$'`; done
  cat > $msgfile

  ... rest of script, redirecting where necessary from $msgfile ...

  /usr/sbin/sendmail -s "$subjstr" "$recip" < $msgfile

HTH.
--
Peter Samuelson
<sampo.creighton.edu ! psamuels>

 
 
 

shell script being called by filter

Post by Matt Nicholl » Sun, 24 May 1998 04:00:00


Hi,

I'm a relative newbie to the world of shell scripts (so please go easy
on me!).

I've got a script (standard run of the mill sh) that is called by filter
(the ELM variety).  It functions fine as it is, but I'm wanting to add
components to the script itself.  Specifically I'm wanting to script to
send me a copy of the mail.

So far, I've got the syntax down to

elm -s $subjstr $recip < file_containing_message

Now my problem is how to get the message into the file.

I figured I'd use the echo method, but I can't figure out what to echo.
$1 and $2 only yield the parameters given to the script, and $3 is
empty.

Thanks for your help!

Cheers,

Matt Nicholls