救援“未找到命令";对于IO :: popen [英] Rescuing "command not found" for IO::popen
本文介绍了救援“未找到命令";对于IO :: popen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将IO::popen
与不存在的命令一起使用时,我会在屏幕上看到一条错误消息:
When I use IO::popen
with a non-existent command, I get an error message printed to the screen:
irb> IO.popen "fakefake"
#=> #<IO:0x187dec>
irb> (irb):1: command not found: fakefake
有什么办法可以捕获此错误,以便可以从脚本中进行检查?
Is there any way I can capture this error, so I can examine from within my script?
推荐答案
是:升级到ruby 1.9.如果在1.9中运行该命令,则会引发Errno::ENOENT
,并且可以rescue
.
Yes: Upgrade to ruby 1.9. If you run that in 1.9, an Errno::ENOENT
will be raised instead, and you will be able to rescue
it.
(编辑)这是在1.8版中做的一种怪异的方式:
(Edit) Here is a hackish way of doing it in 1.8:
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
这篇关于救援“未找到命令";对于IO :: popen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文