From: Adam Skobi Date: 2008-10-17T18:29:18+09:00 Subject: Re: Damn you cmd.exe! ------=_Part_5982_28037555.1224235788849 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Fri, Oct 17, 2008 at 10:39 AM, Robert Klemme wrote: > > > Everything's dandy. It works like a charm. When I try to create an >> equivalent in win32: >> > > Do you use the native Windows Ruby or cygwin? > Native Windows Ruby. > > IO.popen( "cmd.exe", "r+" ) do |cmd| >> cmd.puts 'cd' >> puts cmd.gets >> end >> >> This thing fails miserably. I can't really pinpoint what is the root cause >> of it. >> > > Then what does "fails miserably" mean? Any error messages? > Yes i do. It writes in my native language but it translates to something along the lines: "The process tried to write to a non-existent stream" > > > I have found here on comp.lang.ruby two solutions. First one: >> >> IO.popen( "cmd.exe", "r+" ) do |cmd| >> Thread.new(cmd) do |io| >> while line = io.gets >> puts line >> end >> end >> > > Yep, that's better anyway (see above). > > cmd.puts "dir" >> cmd.puts "cd \\" >> cmd.puts "dir" >> >> sleep 3 >> end >> >> It works but is sucks badly on many levels as you can see. Other one: >> > > You need to remember the thread and join it at the end of the block if you > want to make sure that all output from the shell is printed before you leave > the block. > > > IO.popen("cmd.exe /c #{single_command}") do | pipe | >> pipe.each_line { |line| puts line } >> end >> >> This thing is not so bad but there is a small problem. I want my DSL user >> to >> be able to write such simple commands as >> >> cd C:\ >> dir >> >> The problem is although the both command will run correctly, the change of >> current directory will be lost between the commands. I can think of some >> crazy workarounds but I was wondering if there is a way for the cmd.exe to >> behave normally. Any help would be appreciated. >> > > Well, what is "normal" anyway? Not all operating systems are similar and > just because Window's shell behaves differently does not make it abnormal. > :-) > You see, speaking simply, I want to have control on what 'goes in' and what 'goes out'. I don't want to have a background thread reading everything from cmd.exe as I wont be able to tell which command produced the output. I haven't tested it thoroughly but I think it should look like in /bin/bash. Simple stuff: shell.puts 'cd /home/me' shell.puts 'pwd' puts shell.gets # /home/me I am really bedazzled as to why it doesn't seem to work in win32. -- Adam Skobi ------=_Part_5982_28037555.1224235788849--