Tee utility in Ruby that works on Windows
Sterling Camden
I often check my referrer logs to see how people found my site. Chip’s Tips gets the vast majority of its traffic from search engines. Sometimes I’ll see a search that didn’t quite reach its intended result, and that will inspire a new post.
Quite often I see “ruby tee windows” or something similar, which leads people to my tee utility for Windows. That seems fair enough, except that it isn’t written in Ruby. It’s written in C#, which means that it has 12 source files and the “real” code requires 29 lines. Furthermore, since it’s a .NET executable, it won’t run if it’s installed in a folder that isn’t “trusted”. Talk about red tape.
Besides, this application is trivial with a capital TRIV in Ruby, using the ‘shell’ library. Heedless of its impact on page load time, I’m including the entire source file right here:
require 'shell'
Shell.new.tee(ARGV[0]) < STDIN > STDOUT
Save that to tee.rb, and then you can perform the canonical basic test:
echo hello world | tee.rb "hello.txt"
See “hello” echoed to the console, and check out the contents of hello.txt. Naturally, this utility is far more useful for trapping the log of a long build procedure or some other process that generates tons of output, but which you’d like to see echoed to the console at the same time.
Of course, you could also do the whole thing at the command line:
echo hello | ruby -e "require 'shell';Shell.new.tee('hello.txt') < STDIN > STDOUT"
But I like having it saved in a file so I don’t have to remember it. This should also work just fine on *nix systems, but heck you’ve got the native tee there.
Posted in Ruby, Unix, Windows |
9 Comments » RSS 2.0 | Sphere it!




[...] Tee utility in Ruby that works on Windows — Chip’s Tips for Developers So easy… even a Windows user can do it. (tags: ruby tee windows programming) Tags: business, buzzwords, consulting, fees, programming, ruby, security, techrepublic, tee, windows [...]
[...] a version in Ruby, go here. Tags: .NET, C#, cpptee, mks, streamwriter, tee, UNIX, utility, [...]
FYI,
This doesn’t work in some version of Windows, you will get nothing in your output file. This is some wierd shell error where STDIN doesn’t get connected properly. If this happens, try
echo hello | ruby path\to\tee.rb hello.txt
Or if you get sick of that, try creating ‘tee.cmd’ as
@ruby c:\path\to\tee.rb %*
Then you can do
echo hello | tee hello.txt
This is quite annoying – some things are much easier on Linux – but your post is the top result on google so thanks for your help
Could it be that the file association with .rb isn’t set up?
Thanks, glad you found it helpful.
No, I thought of that first thing, but to be sure I just double-checked and the file association is correct… just doesn’t work correctly. Wierd stuff.
Thanks
Sam
What version of Windows?
2003 server, 32 bit
Just wanted to mention that if one compiles the Ruby implementation as a binary on Windows, then you have an alternative to the .NET implementation, which can be portable and usable on systems w/o .NET installed.
I’ve never compiled a Ruby binary though but have done similar things with Perl and Python.
By the way, can this tee feature be easily implemented in Perl or Python tool?
There are a couple of good solutions out there for Perl:
http://search.cpan.org/~dagolden/Tee-0.13/lib/Tee.pod
http://search.cpan.org/~kenshan/IO-Tee-0.64/Tee.pm
I’m sure you could so something similar in Python as well. Here’s one approach: http://shallowsky.com/blog/programming/python-tee.html
Not as simple as my Ruby example, though.