Hey -- at least you're asking about shell scripts in this group, notQuote:> I'm a relative newbie to the world of shell scripts (so please go
> easy on me!).
about getting a free ISP shell account, which is what a lot of people
seem to think this group is about....
So far so good.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.
Using elm from the command line to send mail is not really necessaryQuote:> So far, I've got the syntax down to
> elm -s $subjstr $recip < file_containing_message
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
Right. A filter in Unix is a program that takes standard input for itsQuote:> 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
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
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):
Put it all together (with two extra lines that add some security):
trap 'rm -f $msgfile' 0
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
<sampo.creighton.edu ! psamuels>