# Automating script for a command line utility

**URL:** <https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690>\
**Category:** ruby-talk\
**Created:** [18 December 2002 15:10 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690 "2002-12-18T15:10:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Date3](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Shashank\_Date3](https://rubytalk.org/u/Shashank_Date3)\
**Post date:** [18 December 2002 15:10 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690/1 "2002-12-18T15:10:37Z")

</div>

I am trying to automate an interactive DOS command line utility (something  
like irb)  
using ruby 1.7.3 (on Windows 2000).

I tried using IO#popen but could not make the inputs (puts) interlace with  
outputs (gets).  
I had to batch all the puts’s together before doing the gets.

Any suggestions ?

TIA,  
– shanko

---

<div class="post-metadata">

**Author:** ![Shashank\_Date3](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Shashank\_Date3](https://rubytalk.org/u/Shashank_Date3)\
**Post date:** [19 December 2002 04:14 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690/2 "2002-12-19T04:14:07Z")

</div>

Ok, so may be I should explain this with some example code ( here irb is  
choosen only for demonstration):

```
check = IO.popen("ruby c:/ruby/bin/irb.rb",'r+')
check.puts("s = 'abcd'")
check.puts("puts 'matched' if s=~/a|b|c|d/")
check.puts("puts $:")
check.puts("puts $\"")
check.close_write
check.readlines.each { |line| puts line.upcase unless line =~ 'nil' }

```

which on my machine (ruby 1.7.3 (2002-11-17) [i386-mswin32]) produces:

```
irb(main):001:0> s = 'abcd'
 "abcd"
irb(main):002:0> puts 'matched' if s=~/a|b|c|d/
 matched
irb(main):003:0> puts $:
 C:/ruby/lib/ruby/site_ruby/1.7
 C:/ruby/lib/ruby/site_ruby/1.7/i386-msvcrt
 C:/ruby/lib/ruby/site_ruby
 C:/ruby/lib/ruby/1.7
 C:/ruby/lib/ruby/1.7/i386-mswin32
 .
irb(main):004:0> puts $"
 irb.rb
 e2mmap.rb
 irb/init.rb
 irb/context.rb
 irb/workspace.rb
 irb/extend-command.rb
 irb/ruby-lex.rb
 irb/slex.rb
 irb/ruby-token.rb
 irb/input-method.rb
 irb/locale.rb
 tempfile.rb
 delegate.rb
irb(main):005:0> exit

```

Notice that I have to club all the “check.puts” functions before the  
“check.close\_write” and then do readlines

Is there a way to check.puts each command and then immediately read the  
lines of output ?  
I think IO#popen3 might do this, but it is not available on Windows.

TIA,

– shanko

“Shashank Date” [sdate@kc.rr.com](mailto:sdate@kc.rr.com) wrote in message  
[news:e90M9.26673$\_b.451371@twister.kc.rr.com](mailto:news:e90M9.26673$_b.451371@twister.kc.rr.com)…

> **···**
>
> > I am trying to automate an interactive DOS command line utility (something  
> > like irb)  
> > using ruby 1.7.3 (on Windows 2000).
> > 
> > I tried using IO#popen but could not make the inputs (puts) interlace with  
> > outputs (gets).  
> > I had to batch all the puts’s together before doing the gets.
> > 
> > Any suggestions ?
> > 
> > TIA,  
> > – shanko

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [19 December 2002 04:43 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690/3 "2002-12-19T04:43:24Z")

</div>

Hi,

> Is there a way to check.puts each command and then immediately read the  
> lines of output ?

Try to send “STDOUT.sync = true” line first. This is because  
the child process buffers stdout. In many UNIX, using pseudo  
TTY tells processes to not buffer output, but Windows has no  
this feature or similar.

I remember I wrote about this, regarding cygwin and DOS prompt  
issue.

> I think IO#popen3 might do this, but it is not available on Windows.

Correct, but impossible as OS doesn’t support it.

> **···**
>
> At Thu, 19 Dec 2002 13:14:07 +0900, Shashank Date wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Shashank\_Date3](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Shashank\_Date3](https://rubytalk.org/u/Shashank_Date3)\
**Post date:** [19 December 2002 05:14 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690/4 "2002-12-19T05:14:31Z")

</div>

Per nobu’s suggestion, this is what I did:

```
check = IO.popen("ruby c:/ruby/bin/irb.rb",'w+')
check.puts("STDOUT.sync = true")
check.puts("puts $:")
check.readlines.each { |line| puts line unless line =~ 'nil' }
check.close_write
check.readlines.each { |line| puts line unless line =~ 'nil' }

```

But the program seemed to hang.  
So I killed it (using CTRL+Break in TextPad) and I got this:

Interrupted!

and after a couple of minutes …

```
C:/ruby/lib/ruby/1.7/irb.rb:234:in `write': Invalid argument

```

(Errno::EINVAL)  
from C:/ruby/lib/ruby/1.7/irb.rb:234:in `print' from C:/ruby/lib/ruby/1.7/irb.rb:234:in `signal\_handle’  
from C:/ruby/lib/ruby/1.7/irb.rb:66:in `start' from C:/ruby/lib/ruby/1.7/irb.rb:65:in `call’  
from C:/ruby/lib/ruby/1.7/irb/input-method.rb:49:in `gets' from C:/ruby/lib/ruby/1.7/irb/input-method.rb:49:in `gets’  
from C:/ruby/lib/ruby/1.7/irb.rb:130:in `eval_input' from C:/ruby/lib/ruby/1.7/irb.rb:129:in `signal\_status’  
… 14 levels…  
from C:/ruby/lib/ruby/1.7/irb.rb:70:in `start' from C:/ruby/lib/ruby/1.7/irb.rb:69:in `catch’  
from C:/ruby/lib/ruby/1.7/irb.rb:69:in `start’  
from c:/ruby/bin/irb.rb:13

```
Tool completed with exit code 4

```

Please help.

Thanks very much !

– shanko

[nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote in message  
news:200212190443.gBJ4hLb24230@sharui.nakada.kanuma.tochigi.jp…

> **···**
>
> > Hi,
> > 
> > At Thu, 19 Dec 2002 13:14:07 +0900, \> Shashank Date wrote:
> > 
> > > Is there a way to check.puts each command and then immediately read the  
> > > lines of output ?
> > 
> > Try to send “STDOUT.sync = true” line first. This is because  
> > the child process buffers stdout. In many UNIX, using pseudo  
> > TTY tells processes to not buffer output, but Windows has no  
> > this feature or similar.
> > 
> > I remember I wrote about this, regarding cygwin and DOS prompt  
> > issue.
> > 
> > > I think IO#popen3 might do this, but it is not available on Windows.
> > 
> > Correct, but impossible as OS doesn’t support it.
> > 
> > –  
> > Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Bulat\_Ziganshin](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bulat\_Ziganshin](https://rubytalk.org/u/Bulat_Ziganshin)\
**Post date:** [19 December 2002 06:18 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690/5 "2002-12-19T06:18:34Z")

</div>

Hello Shashank,

Thursday, December 19, 2002, 8:14:31 AM, you wrote:

> Per nobu’s suggestion, this is what I did:

> ```
> check = IO.popen("ruby c:/ruby/bin/irb.rb",'w+')
> check.puts("STDOUT.sync = true")
> 
> ```

> But the program seemed to hang.

you must exec “sync” at both sides 🙂 add after first line

check.sync = true

i use this techinque to work with perl.exe and it really works

> **···**
>
> –  
> Best regards,  
> Bulat [mailto:bulatz@integ.ru](mailto:bulatz@integ.ru)

---

<div class="post-metadata">

**Author:** ![Shashank\_Date3](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Shashank\_Date3](https://rubytalk.org/u/Shashank_Date3)\
**Post date:** [19 December 2002 14:56 UTC](https://rubytalk.org/t/automating-script-for-a-command-line-utility/3690/6 "2002-12-19T14:56:14Z")

</div>

Hello Bulat,

“Bulat Ziganshin” [bulatz@integ.ru](mailto:bulatz@integ.ru) wrote in message

> you must exec “sync” at both sides 🙂 add after first line
> 
> check.sync = true
> 
> i use this techinque to work with perl.exe and it really works

Ok, so now I have:

```
check = IO.popen("ruby c:/ruby/bin/irb.rb",'w+')
check.sync = true
check.puts("STDOUT.sync = true")
check.puts("puts $:")
check.readlines.each { |line| puts line unless line =~ 'nil' }
check.close_write
check.readlines.each { |line| puts line unless line =~ 'nil' }

```

and it still does not work :-((

Can I see your code which drives perl.exe ?  
May be it has something to do with the target application (in your case  
“perl.exe” in my case “osql.exe”)  
I am using “irb” only as an example.

Thanks,  
– shanko
