VilePoison
VilePoison

Reputation: 75

How to tell if a program is installed on Linux in Haskell

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:

Upvotes: 1

Views: 471

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152937

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)

Upvotes: 5

Related Questions