4

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.

hammar
  • 138,522
  • 17
  • 304
  • 385
Chris
  • 43
  • 2

2 Answers2

3

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.

dave4420
  • 46,404
  • 6
  • 118
  • 152
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
3

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" 
hammar
  • 138,522
  • 17
  • 304
  • 385