Chip's Tips for Developers

Contains coding, but not narcotic.

Tee utility in Ruby that works on Windows

June 28th, 2008 1:37:07 pm pst by 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.

Tags: , , ,

Posted in Ruby, Unix, Windows | 9 Comments » RSS 2.0 | Sphere it!

9 Responses to “Tee utility in Ruby that works on Windows”

  1. [...] 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 [...]

  2. [...] a version in Ruby, go here. Tags: .NET, C#, cpptee, mks, streamwriter, tee, UNIX, utility, [...]

  3. Sam says:

    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 :)

  4. David says:

    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?

Leave a Reply

Better Tag Cloud