如何判断在Haskell中的Linux上是否安装了程序

发布于 2025-02-08 12:24:21 字数 800 浏览 1 评论 0原文

我正在创建一个使用与服务器交互的外部程序的脚本。 我想首先测试该程序在尝试使用之前已安装。

我查找了判断是否安装程序并找到此帖子的首选方法: 如何检查程序是否存在从bash脚本?

tldr:它推荐“命令-v< prog -name>”在“< prog-name>”上因为它是POSIX兼容的。命令应返回0,如果找到程序,> 0否则。

因此,我从system.process中使用readProcessWithExitCode如下所示,

readProcessWithExitCode "command" ["-v", "<some-program>"] ""

我在GHCI中进行测试时会收到以下错误

:readCreatCreateProcessWithExitCode:posix_spawnp:posix_spawnp:不存在(没有这样的文件或目录),

我尝试使用哪个' “命令”。它告诉我它不存在,尽管我可以使用它,并且它可以按照我终端中的人页页面所述工作。

这里发生了什么,我如何看到使用Haskell安装的东西?

一些系统信息:

  • GHC:9.0.2
  • 解析器:LTS-1911
  • “我使用Arch btw”

I'm creating a script that uses an external program that interacts with a server.
I want to test first that the program is installed before trying to use it.

I looked up the preferred way to tell if a program was installed and found this post:
How can I check if a program exists from a Bash script?

TLDR: It recommends "command -v <prog-name>" over "which <prog-name>" since it is POSIX compatible. The command should return 0 if the program was found, >0 otherwise.

So I used readProcessWithExitCode from System.Process as follows

readProcessWithExitCode "command" ["-v", "<some-program>"] ""

I get the following error when testing in GHCI

Exception: command: readCreateProcessWithExitCode: posix_spawnp: does not exist (No such file or directory)

I tried to use 'which' on 'command'. It tells me it does not exist although I can use it and it works as described in the man pages in my terminal.

What's going on here and how do I see if something is installed using Haskell?

Some system information:

  • GHC: 9.0.2
  • resolver: lts-19.11
  • "I use Arch btw"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2025-02-15 12:24:21

我建议您只需运行要运行的程序,并在不可用的情况下捕获例外。像这样:

catch
    (callProcess "lol-this-does-not-exist" []) -- or whatever
    (\e -> if isDoesNotExistError e then putStrLn "yikes" else throw e)

I recommend that you simply run the program you want to run, and catch the exception you get if it isn't available. Like this:

catch
    (callProcess "lol-this-does-not-exist" []) -- or whatever
    (\e -> if isDoesNotExistError e then putStrLn "yikes" else throw e)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文