Output redirection in windows

I am using the one-click installer on windows (RC2). I can not get
output redirection to work with Kernel.system. Consider the following code

F:\tmp\cont-test\test>cat test.rb
def test(command)
   puts command
   puts Kernel.system(command)
   puts "---------------------------"
end

test("mpto mp-test-01.mp")
test("mpto mp-test-01.mp > mp-test-01-test.tex")

mpto.exe is a helper program that comes with metapost and is part of most tex distributions. It is included in my path environment. I have a file mp-test-01.mp in the current directory. mpto scans the .mp file and outputs on stdout. I want to redirect that output to a file. But > does not work.

Running this program gives

F:\tmp\cont-test\test>ruby test.rb
mpto mp-test-01.mp
\gdef\mpxshipout{\shipout\hbox\bgroup
   \setbox0=\hbox\bgroup}
\gdef\stopmpxshipout{\egroup \dimen0=\ht0 \advance\dimen0\dp0
   \dimen1=\ht0 \dimen2=\dp0
   \setbox0=\hbox\bgroup
     \box0
     \ifnum\dimen0>0 \vrule width1sp height\dimen1 depth\dimen2
     \else \vrule width1sp height1sp depth0sp\relax
     \fi\egroup
   \ht0=0pt \dp0=0pt \box0 \egroup}
\mpxshipout% line 2 mp-test-01.mp
This is a test\stopmpxshipout
\end{document}
true

···

---------------------------
mpto mp-test-01.mp > mp-test-01-test.tex
false
---------------------------

system "mpto ..." executes correctly (the true after \end{document})
while system "mpto ... > ...." fails. No mp-test-01-test.tex file is created.

When I give the same commands from cmd.exe they work. I am confused why something as simple as output redirection is not working. What is the correct way to do this?

Thanks,
Aditya

It works for me on the latest One-Click:

C:\_Ryan\downloads>irb
irb(main):001:0> system('echo "hello world!" > hello.txt')
=> true
irb(main):002:0> exit

C:\_Ryan\downloads>type hello.txt
"hello world!"

Another option is to do the redirection yourself:

File.open('output.txt', 'w') do |file|
   IO.popen('dir') do |pipe|
     pipe.each do |line|
       file.print line
     end
   end
end

Ryan

···

On 5/7/06, Aditya Mahajan <adityam@umich.edu> wrote:

I am using the one-click installer on windows (RC2). I can not get
output redirection to work with Kernel.system. Consider the following code

F:\tmp\cont-test\test>cat test.rb
def test(command)
   puts command
   puts Kernel.system(command)
   puts "---------------------------"
end

test("mpto mp-test-01.mp")
test("mpto mp-test-01.mp > mp-test-01-test.tex")

mpto.exe is a helper program that comes with metapost and is part of most tex
distributions. It is included in my path environment. I have a file
mp-test-01.mp in the current directory. mpto scans the .mp file and outputs on
stdout. I want to redirect that output to a file. But > does not work.

Running this program gives

F:\tmp\cont-test\test>ruby test.rb
mpto mp-test-01.mp
\gdef\mpxshipout{\shipout\hbox\bgroup
   \setbox0=\hbox\bgroup}
\gdef\stopmpxshipout{\egroup \dimen0=\ht0 \advance\dimen0\dp0
   \dimen1=\ht0 \dimen2=\dp0
   \setbox0=\hbox\bgroup
     \box0
     \ifnum\dimen0>0 \vrule width1sp height\dimen1 depth\dimen2
     \else \vrule width1sp height1sp depth0sp\relax
     \fi\egroup
   \ht0=0pt \dp0=0pt \box0 \egroup}
\mpxshipout% line 2 mp-test-01.mp
This is a test\stopmpxshipout
\end{document}
true
---------------------------
mpto mp-test-01.mp > mp-test-01-test.tex
false
---------------------------

system "mpto ..." executes correctly (the true after \end{document})
while system "mpto ... > ...." fails. No mp-test-01-test.tex file is created.

When I give the same commands from cmd.exe they work. I am confused why
something as simple as output redirection is not working. What is the correct
way to do this?

Thanks,
Aditya

It works for me on the latest One-Click:

C:\_Ryan\downloads>irb
irb(main):001:0> system('echo "hello world!" > hello.txt')
=> true
irb(main):002:0> exit

C:\_Ryan\downloads>type hello.txt
"hello world!"

Strange, I get

E:\Temp>irb
irb(main):001:0> system('echo "hello world" > hello.txt')
=> false
irb(main):002:0> exit

E:\Temp>type hello.txt
The system cannot find the file specified.

Can it be because I also have cygwin installed?

Another option is to do the redirection yourself:

File.open('output.txt', 'w') do |file|
IO.popen('dir') do |pipe|
   pipe.each do |line|
     file.print line
   end
end
end

How can I pass arguments to the pipe?
E:\Temp>irb
irb(main):003:0> File.open('output.txt', 'w') do |file|
irb(main):004:1* IO.popen('echo "Hello world"') do |pipe|
irb(main):005:2* pipe.each do |line|
irb(main):006:3* file.print line
irb(main):007:3> end
irb(main):008:2> end
irb(main):009:1> end
Errno::ENOENT: No such file or directory - echo "Hello world"
         from (irb):4:in `popen'
         from (irb):4
         from (irb):3

Ryan

Thanks,
Aditya

···

On Sun, 7 May 2006, Ryan Leavengood wrote:

On 5/7/06, Aditya Mahajan <adityam@umich.edu> wrote:

I am using the one-click installer on windows (RC2). I can not get
output redirection to work with Kernel.system. Consider the following code

F:\tmp\cont-test\test>cat test.rb
def test(command)
   puts command
   puts Kernel.system(command)
   puts "---------------------------"
end

test("mpto mp-test-01.mp")
test("mpto mp-test-01.mp > mp-test-01-test.tex")

mpto.exe is a helper program that comes with metapost and is part of most tex
distributions. It is included in my path environment. I have a file
mp-test-01.mp in the current directory. mpto scans the .mp file and outputs on
stdout. I want to redirect that output to a file. But > does not work.

Running this program gives

F:\tmp\cont-test\test>ruby test.rb
mpto mp-test-01.mp
\gdef\mpxshipout{\shipout\hbox\bgroup
   \setbox0=\hbox\bgroup}
\gdef\stopmpxshipout{\egroup \dimen0=\ht0 \advance\dimen0\dp0
   \dimen1=\ht0 \dimen2=\dp0
   \setbox0=\hbox\bgroup
     \box0
     \ifnum\dimen0>0 \vrule width1sp height\dimen1 depth\dimen2
     \else \vrule width1sp height1sp depth0sp\relax
     \fi\egroup
   \ht0=0pt \dp0=0pt \box0 \egroup}
\mpxshipout% line 2 mp-test-01.mp
This is a test\stopmpxshipout
\end{document}
true
---------------------------
mpto mp-test-01.mp > mp-test-01-test.tex
false
---------------------------

system "mpto ..." executes correctly (the true after \end{document})
while system "mpto ... > ...." fails. No mp-test-01-test.tex file is created.

When I give the same commands from cmd.exe they work. I am confused why
something as simple as output redirection is not working. What is the correct
way to do this?

Thanks,
Aditya

--
Aditya Mahajan, EECS Systems, University of Michigan
http://www.eecs.umich.edu/~adityam || Ph: 7342624008

Strange, I get

E:\Temp>irb
irb(main):001:0> system('echo "hello world" > hello.txt')
=> false

This false result means that system could not find echo.

Can it be because I also have cygwin installed?

It may be related. The problem is that the system method cannot
execute what you are trying to run. It might be related to your path
or who knows what.

Errno::ENOENT: No such file or directory - echo "Hello world"

Again this seems to be a problem with your environment.

Ryan

···

On 5/7/06, Aditya Mahajan <adityam@umich.edu> wrote: