Capturing "command not found" errors from Ruby backlinks?

Is there a way to catch the "command not found" error in a Ruby script? For example, given:

output = `foo` 

How to block a situation when foo not installed? I expected that I could rescue exception, but this does not work on 1.8.7. Is there any other way to call a subprocess that will do what I want? Or is there a different approach?

Update

My apologies, I forgot to mention a hidden requirement: I would prefer that the interpreter does not skip the command line to the user (it may contain sensitive data), so the catch catch method is preferable. Apologize again for leaving this for the first time.

+7
source share
1 answer

Use return code!

 irb(main):001:0> `date` => "Mo 24. Jan 16:07:15 CET 2011\n" irb(main):002:0> $? => #<Process::Status: pid=11556,exited(0)> irb(main):003:0> $?.to_i => 0 irb(main):004:0> `foo` (irb):4: command not found: foo => "" irb(main):005:0> $?.to_i => 32512 

http://corelib.rubyonrails.org/classes/Process/Status.html

Redirecting STDERR to STDOUT will give you the result as a return value, rather than inflating it:

 irb(main):010:0> `foo 2>&1` => "sh: foo: not found\n" irb(main):011:0> $?.to_i => 32512 
+13
source

All Articles