我的脚本很大程度上依赖于外部程序和脚本。我需要确保我需要调用的程序存在。手动地,我会在命令行中使用'which‘来检查它。
在$PATH
中有没有等同于File.exists?
的东西
(是的,我想我可以解析%x[which scriptINeedToRun]
,但这并不是非常优雅。
谢谢!yannick
更新:以下是我保留的解决方案:
def command?(command)
system("which #{ command} > /dev/null 2>&1")
end
更新2:一些新的答案出现了-至少其中一些提供了更好的解决方案。
更新3:ptools gem向File类添加了一个"which“方法。
https://stackoverflow.com/questions/2108727
复制相似问题