[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Shell script reading stdin?



Jim Breton wrote:
> On Sat, Jun 10, 2000 at 10:54:32PM -0400, Mike Werner wrote:
> > What I couldn't find was *any* mention of was how to *use* stdin from
> > within a shell script.  Anyone here ever done anything like this?
> 
> 
> Yes, but this may or may not be suitable for whatever you're doing.  As
> an example, this would take stdin and pipe it to tr.
> 
> #!/bin/sh
> 
> cat | tar [A-Z] [a-z]
> 
> 
> There are probably better ways (which I'd also be interested in
> knowing).  :)

Thank you!  It appears that this will do the job for me.
 
> Fwiw, you could use "exec cat" instead of "cat" if you needed the shell
> to persist after the cat invocation, but that's a different story.

I'll keep that in mind, but for this application I don't think it'll
matter.
 
> Maybe if you posted what you're trying to do, some better ways of doing
> it might come to light?

When using exim as your MTA, you can define filters in the $USER/.forward
file.  One of the options for those filters is to pipe the message to an
external script / program.  The script I am trying to write will receive
a mail message - based on the subject line - that will be sent to me as the
result of a cron job.  What the script will do is:
* strip off the headers
* read a list of email addresses out of the message body
* do a diff between the message and another file
* depending on the results, it'll either send notification to the list of
  email addresses it got from the message body, or if there's been no
  change then it will remain silent

The purpose is to monitor an email list server that I help to admin.  The
server is not exactly very reliable, and in the past has spontaneously
changed its settings.  The message that I'll be processing in this script
is a report from the server of its current settings.  The file that the
message will be diff'ed with is what the settings *should* be.  If they're
still the same, nothing happens.  If somehting has changed, email goes out
to the list of admins that gets read in from the message - the admin's
addresses are one of the settings.

Now then, in the exim docs, it says that when the pipe option is used in
the filter, the external command is run with the message passed on the
standard input.  The primary thing I needed was to get the message from
the standard input to a file.  And, thanks to your message, I now know
that doing:
cat > filename
does just that.  I can then diddle with filename to my heart's content.
-- 
Mike Werner  KA8YSD           |  "Where do you want to go today?"
                              |  "As far from Redmond as possible!"
'91 GS500E                    |
Morgantown WV                 |  Only dead fish go with the flow.



Reply to: