views:

2265

answers:

11

I'm looking for a little shell script that will take anything piped into it, and dump it to a file.. for email debugging purposes. Any ideas?

+2  A: 

Use Procmail. Procmail is your friend. Procmail is made for this sort of thing.

JBB
+14  A: 

The unix command tee does this.

man tee
Stephen Deken
If it were up to me, imphasing, I'd "accept" this answer
Nathan Fellman
+4  A: 
cat > FILENAME
Commodore Jaeger
+4  A: 

The standard unix tool tee can do this. It copies input to output, while also logging it to a file.

Brian Mitchell
+4  A: 

You're not alone in needing something similar.. in fact, someone wanted i decades ago and developed tee :-)

Of course, you can redirect stdout directly to a file in any shell using the > character:

echo "hello, world!" > the-file.txt
Isak Savo
A: 

Huh? I guess, I don't get the question?

Can't you just end your pipe into a >> ~file

For example

echo "Foobar" >> /home/mo/dumpfile

will append Foobar to the dumpfile (and create dumpfile if necessary). No need for a shell script... Is that what you were looking for?

Mo
+1  A: 

If you want to analyze it in the script:

while /bin/true; do
    read LINE
    echo $LINE > $OUTPUT
done

But you can simply use cat. If cat gets something on the stdin, it will echo it to the stdout, so you'll have to pipe it to cat >$OUTPUT. These will do the same. The second works for binary data also.

terminus
A: 

if you don't care about outputting the result

cat - > filename

or

cat > filename
BCS
A: 

If you want a shell script, try this:

#!/bin/sh
exec cat >/path/to/file
sirprize
+1  A: 

If exim or sendmail is what's writing into the pipe, then procmail is a good answer because it'll give you file locking/serialization and you can put it all in the same file.

If you just want to write into a file, then - tee > /tmp/log.$$ or - cat > /tmp/log.$$ might be good enough.

Liudvikas Bukys
A: 

<< command >> | tee < file >>

This will also show the output.

Scott