0

我正在net share从 ruby​​ 脚本运行以删除 Windows 网络共享。

如果共享上的文件正在使用中,则会询问用户是否要继续删除,因此我的脚本需要检查命令的输出,并在检测到是否要求输入net share时写出。Ynet share

为了能够写出进程,我用 access flags 打开它"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
4

3 回答 3

1

尝试 w+ 而不是 r+

  Mode |  Meaning
  -----+--------------------------------------------------------
  "r"  |  Read-only, starts at beginning of file  (default mode).
  -----+--------------------------------------------------------
  "r+" |  Read-write, starts at beginning of file.
  -----+--------------------------------------------------------
  "w"  |  Write-only, truncates existing file
       |  to zero length or creates a new file for writing.
  -----+--------------------------------------------------------
  "w+" |  Read-write, truncates existing file to zero length
       |  or creates a new file for reading and writing.
  -----+--------------------------------------------------------
  "a"  |  Write-only, starts at end of file if file exists,
       |  otherwise creates a new file for writing.
  -----+--------------------------------------------------------
  "a+" |  Read-write, starts at end of file if file exists,
       |  otherwise creates a new file for reading and
       |  writing.
于 2010-05-26T11:34:09.013 回答
1

如果您希望net share ... /delete命令始终成功,您可以传递/y标志:

C:\>net share foo /delete /y
Users have open files on foo.  Continuing the operation will force the files closed.

foo was deleted successfully.
于 2010-06-21T20:48:19.287 回答
-3

使用net_share.readlines而不是开始/救援/结束。

谷歌搜索的第二个结果ruby io.popen

于 2010-05-25T18:23:01.517 回答