【发布时间】:2010-05-25 09:41:04
【问题描述】:
我正在从 ruby 脚本运行 net share 以删除 Windows 网络共享。
如果共享上的文件正在使用中,net share 将询问用户是否要继续删除,因此我的脚本需要检查命令的输出,如果检测到则写出Y net share 要求它输入。
为了能够写出进程,我用访问标志"r+"打开它。
尝试使用IO#puts 写入进程时,出现错误:
Errno::EPIPE: Broken pipe
我在这里做错了什么? (错误出现在net_share.puts "Y"这一行)
(net share写出的问题文本后面没有换行,所以我使用IO#readpartial读入输出。)
def delete_network_share share_path
command = "net share #{share_path} /DELETE"
net_share = IO.popen(command, "r+")
text = ""
while true
begin
line = net_share.readpartial 1000 #read all the input that's available
rescue EOFError
break
end
text += line
if text.scan("Do you want to continue this operation? (Y/N)").size > 0
net_share.puts "Y"
net_share.flush #probably not needed?
end
end
net_share.close
end
【问题讨论】: