8

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?

AnFi
  • 10,493
  • 3
  • 23
  • 47
Alex Fort
  • 18,459
  • 5
  • 42
  • 51

11 Answers11

27

The unix command tee does this.

man tee
Stephen Deken
  • 3,665
  • 26
  • 31
11
cat > FILENAME
Commodore Jaeger
  • 32,280
  • 4
  • 54
  • 44
11

You're not alone in needing something similar... in fact, someone wanted that functionality 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
B--rian
  • 5,578
  • 10
  • 38
  • 89
Isak Savo
  • 34,957
  • 11
  • 60
  • 92
5

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

Brian Mitchell
  • 2,280
  • 14
  • 12
2

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

JBB
  • 4,543
  • 3
  • 24
  • 25
2

Use <<command>> | tee <<file>> for piping a command <<command>> into a file <<file>>.

This will also show the output.

B--rian
  • 5,578
  • 10
  • 38
  • 89
Scott
  • 7,034
  • 4
  • 24
  • 26
  • This solution has also the advantage that you receive an error message on `stderr` if you are missing the permissions to create the file. – B--rian Jul 12 '18 at 09:41
1

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
  • 13,745
  • 8
  • 34
  • 37
1

If you want a shell script, try this:

#!/bin/sh
exec cat >/path/to/file
sirprize
  • 436
  • 1
  • 3
  • 9
1

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
  • 5,790
  • 3
  • 25
  • 36
0

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.
  • 15,033
  • 14
  • 47
  • 57
0

if you don't care about outputting the result

cat - > filename

or

cat > filename
BCS
  • 75,627
  • 68
  • 187
  • 294