我从ruby脚本运行net share
以删除Windows网络共享。
如果共享中的文件正在使用中,net share
将询问用户是否要继续删除,因此我的脚本需要检查命令的输出,并写出{{1}如果它检测到Y
要求输入。
为了能够写出进程,我使用访问标记net share
打开它。
尝试使用"r+"
写入流程时,出现错误:
IO#puts
我在这里做错了什么? (错误发生在Errno::EPIPE: Broken pipe
行)
(由net_share.puts "Y"
写出的问题文字后面没有换行符,因此我使用net share
来读取输出。)
IO#readpartial
答案 0 :(得分: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.
答案 1 :(得分:0)
尝试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.
答案 2 :(得分:-3)
使用net_share.readlines
代替begin / rescue / end。
Google搜索ruby io.popen
的第二个结果。