# Piping problem

**URL:** <https://www.ruby-forum.com/t/piping-problem/121156>\
**Category:** Ruby\
**Created:** [November 24, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/piping-problem/121156 "2007-11-24T17:54:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 24, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/piping-problem/121156/1 "2007-11-24T17:54:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 1:18am UTC](https://www.ruby-forum.com/t/piping-problem/121156/2 "2007-11-25T01:18:59Z")

</div>

Pawel R. wrote:

> 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

??

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 1:21am UTC](https://www.ruby-forum.com/t/piping-problem/121156/3 "2007-11-25T01:21:46Z")

</div>

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

$

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 6:20am UTC](https://www.ruby-forum.com/t/piping-problem/121156/4 "2007-11-25T06:20:29Z")

</div>

On Nov 24, 10:52 am, Pawel R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 6:30am UTC](https://www.ruby-forum.com/t/piping-problem/121156/5 "2007-11-25T06:30:52Z")

</div>

Jordan Callicoat wrote:

> Well, you’ve defined reverse.rb to read from stdin,

Read the documentation for gets.

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 1:25am UTC](https://www.ruby-forum.com/t/piping-problem/121156/6 "2007-11-25T01:25:01Z")

</div>

7stud – wrote:

> 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  
$

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 8:00am UTC](https://www.ruby-forum.com/t/piping-problem/121156/7 "2007-11-25T08:00:57Z")

</div>

On Nov 24, 11:30 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 25, 2007, 8:15am UTC](https://www.ruby-forum.com/t/piping-problem/121156/8 "2007-11-25T08:15:33Z")

</div>

Pawel R. wrote:

> #!/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

---

<div class="post-metadata">

**Author:** ![Pawel\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@Pawel\_R](https://www.ruby-forum.com/u/Pawel_R)\
**Post date:** [November 26, 2007, 2:25am UTC](https://www.ruby-forum.com/t/piping-problem/121156/9 "2007-11-26T02:25:30Z")

</div>

On Nov 25, 2007 12:52 AM, Pawel R. \>

> reverse.rb:9:in ‘gets’: Bad file descriptor (Errno:EBADF)  
> Do you have any ideas why it doesn’t work?

> ruby -v  
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

> ruby reverse.rb sample.txt | ruby reverse.rb  
> testing only

kind regards -botp
