Chris
Chris

Reputation: 43

How to get the exit code of a system process?

Say I want to execute the command unrar x archivename from within Haskell.

What is the best way to do it and how do I get the exit code of the command? If the command exited successfully I want to delete the archive else not.

Upvotes: 4

Views: 1032

Answers (2)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

In the process library you will find the function readProcessWithExitCode, which does what you want. Something like:

(e,_,_) <- readProcessWithExitCode "unrar" ["unrar", "x", "-p-", "archivename"] ""
if e == ExitSuccess then ... else ...

There are also many other solutions, such as the system command. Take your pick.

Upvotes: 3

hammar
hammar

Reputation: 139840

The readProcessWithExitCode function from System.Process should do the job.

import Control.Monad
import System.Directory
import System.Exit
import System.Process

main = do
    (exitCode, _, _) <- readProcessWithExitCode "unrar" ["x", "archivename"] ""
    when (exitCode == ExitSuccess) $ removeFile "archivename" 

Upvotes: 3

Related Questions