Reputation: 43
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
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
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