这篇文章中提到了6种方法 http://blackanger.blog.51cto.com/140924/43730
实际运用中我们有时候需要交互输入(比如:执行passwd修改本地用户密码),这个时候就要用 open3 了,具体的代码如下:
linux下
#!/usr/bin/env ruby
#use_open3_for_linux.rb
require "open3"
stdin, stdout, stderr = Open3.popen3('passwd aaa')
stdin.puts("123")
stdin.puts("123")
p stdout.gets
p stderr.gets
windows下
#!/usr/bin/env ruby
#use_win32open3_for_windows.rb
require 'rubygems'
require 'win32/open3' #Win32 Utils
stdin, stdout, stderr = Open3.popen3('d:\command_line\md5.exe -d^')
stdin.puts("123")
p stdout.gets
p stderr.gets
后记:
windows下使用 open3 的话会报如下错误
open3.rb:51:in `fork': fork() function is unimplemented on this machine (NotImplementedError)
解决这个错误的方法是加载win32/process,代码如下
#!/usr/bin/env ruby
require 'rubygems'
require 'win32/process'
require 'open3'
stdin, stdout, stderr = Open3.popen3('ipconfig')
p stdout.gets
p stderr.gets
但是这样 stdout 始终是 nil ,所以windows下还是要用 win32-open3
Last modified by vkill on2010/06/02 18:21
实际运用中我们有时候需要交互输入(比如:执行passwd修改本地用户密码),这个时候就要用 open3 了,具体的代码如下:
linux下
#!/usr/bin/env ruby
#use_open3_for_linux.rb
require "open3"
stdin, stdout, stderr = Open3.popen3('passwd aaa')
stdin.puts("123")
stdin.puts("123")
p stdout.gets
p stderr.gets
windows下
#!/usr/bin/env ruby
#use_win32open3_for_windows.rb
require 'rubygems'
require 'win32/open3' #Win32 Utils
stdin, stdout, stderr = Open3.popen3('d:\command_line\md5.exe -d^')
stdin.puts("123")
p stdout.gets
p stderr.gets
后记:
windows下使用 open3 的话会报如下错误
Quotation
open3.rb:51:in `fork': fork() function is unimplemented on this machine (NotImplementedError)
解决这个错误的方法是加载win32/process,代码如下
#!/usr/bin/env ruby
require 'rubygems'
require 'win32/process'
require 'open3'
stdin, stdout, stderr = Open3.popen3('ipconfig')
p stdout.gets
p stderr.gets
但是这样 stdout 始终是 nil ,所以windows下还是要用 win32-open3
Last modified by vkill on2010/06/02 18:21
网友评论(0):


