If you meant what you typed and it wasn't a typo, the pipeline
is backwards. Should be:
% head -n 1 file.txt | my_utility.rb
if you meant to run the first line of file.txt to your ruby script.
If you really meant to create file.txt with the first line of the output
of your ruby script, it would be:
% my_utility.rb | head -n 1 > file.txt
Data flows left to right through pipes.
On the other hand, the "redirect to file"
operators (">" or "<") are used with a program/script on the left
and a file on the right. Data flows in the direction of the arrow.
So these two lines are equivalent to each other:
% script.rb < file.txt
% cat file.txt | script.rb
And these two are equivalent to each other (but not to the above):
% script.rb > file.txt
% script.rb | some_program_that_creates_a_file -f file.txt
-glenn
Greg Willits wrote:
> I need to do a couple quick little utility scripts which pipe the
> result of a shell command into a Ruby script.
>
> For example, I imagined it would look like this:
>
> my_utility.rb | head -n 1 file_name.txt
>
> where the ruby script can be as simple as this just to get a working
> skeleton:
>
> #!/usr/bin/env ruby
> p ARGV
>
> But with that syntax I get a "broken pipe" error.
>
> I've googled and tried a few things, but no go. It has to be something
> really simple I am missing.
>
> -- gw
>
>
--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby