my scripts rely heavily on external programs and scripts. I need to be sure that a program I need to call exists. Manually, I\'d check this using \'which\' in the commandlin
def command?(name)
`which #{name}`
$?.success?
end
Initially taken from hub, which used type -t
instead of which
though (and which failed for both zsh and bash for me).
Not so much elegant but it works :).
def cmdExists?(c)
system(c + " > /dev/null")
return false if $?.exitstatus == 127
true
end
Warning: This is NOT recommended, dangerous advice!
There was a GEM called which_ruby
that was a pure-Ruby which implementation.
It's no longer available.
However, I found this pure-Ruby alternative implementation.
On linux I use:
exists = `which #{command}`.size.>(0)
Unfortunately, which
is not a POSIX command and so behaves differently on Mac, BSD, etc (i.e., throws an error if the command is not found). Maybe the ideal solution would be to use
`command -v #{command}`.size.>(0) # fails!: ruby can't access built-in functions
But this fails because ruby seems to not be capable of accessing built-in functions. But command -v
would be the POSIX way to do this.