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

标签 haskell

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

createProcess $ proc "foo" []

但是我们事先并不知道是否可以找到foo。我们可以运行它并捕获异常,但这很糟糕。我们也可以先调用 which 但这也很糟糕。在 process 库中我们找到 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`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47020908/

相关文章:

haskell - Cabal 1.18 在 OS X 上的几乎所有东西上都给出了总线错误 10

parsing - Haskell 解析为自定义数据类型

haskell - Threenpenny gui - 单击时捕获鼠标坐标并使用它们构建一些状态

haskell - 在 Haskell 中, "higher-kinded types"*真的*类型吗?或者它们仅仅表示*具体*类型的集合而仅此而已?

list - 平面列表和自由单子(monad)

haskell - 为什么类型是 "Main.main", "IO ()"而不是 "IO a"?

haskell - 反转折叠

Haskell 单子(monad) : etymology versus meaning?

parsing - 在 Haskell 中派生 Read(和 Show)时避免反斜杠编码 utf8 字符

haskell - 多态数据类型的函数