haskell – 以编程方式检查可执行文件是否存在而不运行它或使用`which`

我们可以在System.Process.proc的帮助下运行进程foo,如下所示:

createProcess $proc "foo" []

但是我们提前不知道是否可以找到foo.我们可以运行它并捕获异常,但这很糟糕.我们也可以先打电话给哪个,但也很糟糕.在流程库中我们找到RawCommand,截至今天所说

The FilePath argument names the executable, and is interpreted
according to the platform’s standard policy for searching for
executables. Specifically:

  • on Unix systems the execvp(3) semantics is used, where if the executable filename does not contain a slash (/) then the PATH
    environment variable is searched for the executable.

  • on Windows systems
    the Win32 CreateProcess semantics is used. Briefly: if the filename
    does not contain a path, then the directory containing the parent
    executable is searched, followed by the current directory, then some
    standard locations, and finally the current PATH. An .exe extension is
    added if the filename does not already have an extension. For full
    details see the documentation for the Windows SearchPath API.

这正是我想要做的,但似乎没有任何暴露的功能.我只是错过了它,是否有更规范/更好的方式,还是我必须深入了解过程内部?解决方案应该是可移植的(Windows / Linux).

最佳答案
看起来你想要here的findExecutable函数.

Prelude System.Directory> findExecutable "gcc"
Just "/usr/bin/gcc"

转载注明原文:haskell – 以编程方式检查可执行文件是否存在而不运行它或使用`which` - 代码日志