6
当我使用IO::popen
一个不存在的命令“未找到命令”,我得到打印到屏幕上的错误信息:拯救了IO :: popen方法
irb> IO.popen "fakefake"
#=> #<IO:0x187dec>
irb> (irb):1: command not found: fakefake
有什么办法,我可以抓住这个错误,所以我可以从我的脚本中检查?
当我使用IO::popen
一个不存在的命令“未找到命令”,我得到打印到屏幕上的错误信息:拯救了IO :: popen方法
irb> IO.popen "fakefake"
#=> #<IO:0x187dec>
irb> (irb):1: command not found: fakefake
有什么办法,我可以抓住这个错误,所以我可以从我的脚本中检查?
是:升级到红宝石1.9。如果你在1.9版本中运行该版本,则会增加一个Errno::ENOENT
,而你将可以使用rescue
它。
(编辑)这里是1.8做的hackish的方式:
error = IO.pipe
$stderr.reopen error[1]
pipe = IO.popen 'qwe' # <- not a real command
$stderr.reopen IO.new(2)
error[1].close
if !select([error[0]], nil, nil, 0.1)
# The command was found. Use `pipe' here.
puts 'found'
else
# The command could not be found.
puts 'not found'
end