我试图用以下代码检查system命令是否存在:
require 'open3'
Open3.popen3('non-existing command') do |stdin, stdout, stderr, thread|
exit_error = stderr.readlines
if exit_error["No such file or directory"]
puts "command not found"
end
end但是,它只是带着下面的错误消息崩溃,没有继续:
/home/pavel/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open3.rb:211:in `spawn': No such file or directory - non-existing (Errno::ENOENT)为什么以及如何修复它?
发布于 2013-10-17 12:29:52
如果Open3.popen3找不到命令,它就会引发Errno::ENOENT异常;因此,您只需从该异常中拯救:
require 'open3'
begin
Open3.popen3('non-existing command') do |stdin, stdout, stderr, thread|
end
rescue Errno::ENOENT
puts "command not found"
end
#=> outputs "command not found"https://stackoverflow.com/questions/19425909
复制相似问题