Reading frin STDIN

C

Clayton Wozney

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.
 
M

Mark Hubbart

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.

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
 
A

Ara.T.Howard

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.

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
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
B

Bill Kelly

Hi,

From: "Clayton Wozney said:
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
 
C

Clayton Wozney

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top