Reading frin STDIN

I’m using Ruby to create little command line utilities that read from
STDIN and write to STDOUT so that I can string them together with pipes.
The I/O part looks like this…

# open up STDIN for reading
infile = IO.new(0, "r")

# read from STDIN until EOF
infile.each_line do |line|

Now, this works fine when I run it from the command line directly (no
pipe). However, when I try to use a pipe like this (for example):

c:\>type file.txt | ruby_script.rb

I get this error message:

The process tried to write to a nonexistent pipe.
ruby_script.rb:23:in `each_line': Bad file descriptor (Errno::EBADF)
    from ruby_script.rb:23

Is there another way to achieve this effect? Thanx.

Clayton.

Not sure why your way wouldn’t work, but: any particular reason you
aren’t using STDIN and STDOUT?

STDIN.each_line do |line|
# do stuff with line
STDOUT.write line
end

cheers,
–Mark

···

On May 5, 2004, at 1:48 PM, Clayton Wozney wrote:

I’m using Ruby to create little command line utilities that read from
STDIN and write to STDOUT so that I can string them together with
pipes.
The I/O part looks like this…

open up STDIN for reading

infile = IO.new(0, “r”)

read from STDIN until EOF

infile.each_line do |line|

Now, this works fine when I run it from the command line directly (no
pipe). However, when I try to use a pipe like this (for example):

c:>type file.txt | ruby_script.rb

I get this error message:

The process tried to write to a nonexistent pipe.
ruby_script.rb:23:in `each_line’: Bad file descriptor (Errno::EBADF)
from ruby_script.rb:23

Is there another way to achieve this effect? Thanx.

perhaps something like

unless STDIN.tty? # we are in a pipeline
while((line = STDIN.gets))
p line
end
else
end

however - you error is in writing where are you writing to?

why open up infile - why not simply use STDIN?

-a

···

On Wed, 5 May 2004, Clayton Wozney wrote:

I’m using Ruby to create little command line utilities that read from
STDIN and write to STDOUT so that I can string them together with pipes.
The I/O part looks like this…

open up STDIN for reading

infile = IO.new(0, “r”)

read from STDIN until EOF

infile.each_line do |line|

Now, this works fine when I run it from the command line directly (no
pipe). However, when I try to use a pipe like this (for example):

c:>type file.txt | ruby_script.rb

I get this error message:

The process tried to write to a nonexistent pipe.
ruby_script.rb:23:in `each_line’: Bad file descriptor (Errno::EBADF)
from ruby_script.rb:23

Is there another way to achieve this effect? Thanx.

Clayton.

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

Hi,

c:>type file.txt | ruby_script.rb

I get this error message:

The process tried to write to a nonexistent pipe.
ruby_script.rb:23:in `each_line’: Bad file descriptor (Errno::EBADF)
from ruby_script.rb:23

Is there another way to achieve this effect? Thanx.

I’m guessing the error might stem from the bug in Windows’
cmd.exe, which has been talked about here in the past in what
I recall being similar circumstances…

Try invoking ruby explitly, like

c:>type file.txt | ruby ruby_script.rb

…if that fixes it, then I think we were seeing the cmd.exe
bug.

Hope this helps,

Bill

···

From: “Clayton Wozney” clayton.wozney@cibc.com

Thank-you all for your prompt responses. Bill Kelly had the correct
answer. Running my piped scripts by explicitly invoking the ruby.exe
interpreter fixed the problem. I’m a relative newcomer to the Ruby news
group so I haven’t seen the past articles re: cmd.exe. I would have had no
idea where to start looking. Thanx Bill for your prompt and accurate
response and to everyone who pointed out my newb coding errors. Cheers!

Clayton.