# Does open("| #{cmd}", "r") work in Windows?

**URL:** <https://www.ruby-forum.com/t/does-open-cmd-r-work-in-windows/115316>\
**Category:** Ruby\
**Created:** [September 23, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/does-open-cmd-r-work-in-windows/115316 "2007-09-23T19:23:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 23, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/does-open-cmd-r-work-in-windows/115316/1 "2007-09-23T19:23:23Z")

</div>

All,

Attempting to debug the autotest gem on Windows. I’ve gotten as far as  
discovering that in the code below, the open call will succeed (and cmd  
has a real command in it), however the file object f, will be at eof  
immediately.

I can run this command on the command line in Windows and see that it  
has output.

My questions is:

should I expect open("| ", “r”) to run the command and then  
get the output in the attached block on Windows?

```
  open("| #{cmd}", "r") do |f|

```

until f.eof? do  
c = f.getc  
putc c  
line \<\< c  
if c == ?\n then  
@results \<\< line.pack(“c\*”)  
line.clear  
end  
end  
end

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 23, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/does-open-cmd-r-work-in-windows/115316/2 "2007-09-23T22:17:46Z")

</div>

Wes G. wrote:

> should I expect open("| ", “r”) to run the command and then  
> get the output in the attached block on Windows?

I tested the following under Windows2000…

> > > > > Example \>\>\>\>\>  
> > > > > C:\Dokumente und Einstellungen\wolfgang\>time  
> > > > > Aktuelle Zeit: 22:14:57,53  
> > > > > Geben Sie die neue Zeit ein:

C:\Dokumente und Einstellungen\wolfgang\>irb  
irb(main):001:0\> open(’|time’, ‘a+’) do |f|  
irb(main):002:1\* old = f.gets.chomp  
irb(main):003:1\> f.write ‘11’  
irb(main):004:1\> puts “old: #{old}”  
irb(main):005:1\> end  
old: Aktuelle Zeit: 22:15:12,21  
=\> nil  
irb(main):006:0\> exit

C:\Dokumente und Einstellungen\wolfgang\>time  
Aktuelle Zeit: 11:00:12,28  
Geben Sie die neue Zeit ein:

> > > > > EoE \>\>\>\>\>

…and it works.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 23, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/does-open-cmd-r-work-in-windows/115316/3 "2007-09-23T22:54:50Z")

</div>

It turns out that my problem is that the command being passed to  
Kernel.open is in the form

C:\ruby\bin\ruby …

Because the command is enclosed in double quotes ("), the \r is being  
interpreted as a special character and the path is not understood  
correctly.

If you:

1. replace the double quotes with single quotes, or
2. leave the double quotes and escape the backslashes in the path, or
3. replace the backslashes with forward slashes,

it will work.

I’ll look into submitting a patch for ZenTest so that the calling code  
will work.

Wes
