Piping problem

P

Pawel Radecki

Hello everybody!

I'm trying to write a simple program that reads text from standard input
and writes text to standard output. The source code is as follows:

#!/usr/bin/env ruby
text = ""
while (line = gets)
text+=line
end
print text.reverse


Now I want to execute it on the Windows XP box with a pipe to redirect
results so I know that:

reverse.rb sample.txt | reverse.rb

will show me (on the screen be default) the contents of sample.txt file
in non-reversed way.
I'm executing above command line and getting this:

reverse.rb:9:in 'gets': Bad file descriptor (Errno:EBADF)


Do you have any ideas why it doesn't work?

Thanks in advance for any help!
 
7

7stud --

Pawel said:
Hello everybody!

I'm trying to write a simple program that reads text from standard input
and writes text to standard output. The source code is as follows:

#!/usr/bin/env ruby
text = ""
while (line = gets)
text+=line
end
print text.reverse


Now I want to execute it on the Windows XP box with a pipe to redirect
results so I know that:

reverse.rb sample.txt | reverse.rb

will show me (on the screen be default) the contents of sample.txt file
in non-reversed way.
I'm executing above command line and getting this:

reverse.rb:9:in 'gets': Bad file descriptor (Errno:EBADF)


Do you have any ideas why it doesn't work?

Thanks in advance for any help!

When I get that error, it usually means that I am trying to read(e.g.
gets) from something that is opened for writing only. I'm not sure how
knowing that is useful in this case.

Isn't the command:

reverse.rb sample.txt | reverse.rb

equivalent to:

cat sample.txt

??
 
7

7stud --

I forgot to say, your command works for me on mac os x(unix):

$ cat data.txt
hello world
goodbye

$ r5test.rb data.txt | r5test.rb
hello world
goodbye

$
 
7

7stud --

7stud said:
I forgot to say, your command works for me on mac os x(unix):

$ cat data.txt
hello world
goodbye

$ r5test.rb data.txt | r5test.rb
hello world
goodbye

$

This probably better illustrates what I'm seeing:

$ cat data.txt
hello world
goodbye
$ r5test.rb data.txt

eybdoog
dlrow olleh$ r5test.rb data.txt | r5test.rb
hello world
goodbye
$
 
M

MonkeeSage

Now I want to execute it on the Windows XP box with a pipe to redirect
results so I know that:

reverse.rb sample.txt | reverse.rb

Well, you've defined reverse.rb to read from stdin, but you're passing
it a file with no stdin. I think in XP there is a "type" command that
does what "cat" does on posix systems (outputs contents of file to
stdout), so I think you want this:

type sample.txt | reverse.rb | reverse.rb

I don't have an XP box to test on, but that works with "cat" on linux.
(Ps. Does XP's cmd.exe support pipes "|"? Or are you using cygwin or
something similar? (If so, you may have a version of "cat"
available.))

Regards,
Jordan
 
M

MonkeeSage

Read the documentation for gets.

Yeah, I probably should RTFM before I reply, heh.
Returns (and assigns to +$_+) the next line from the list of files
in +ARGV+ (or +$*+), or from standard input if no files are present
on the command line.

Doh.

Regards,
Jordan
 
T

TerryP

Pawel said:
#!/usr/bin/env ruby
text = ""
while (line = gets)
text+=line
end
print text.reverse

Interestingly it seems to work perfectly fine here,

Terry@Dixie$ ruby --version
7:10
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-freebsd6]
Terry@Dixie$
7:11
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top