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 开发者_Python百科the command? If the command exited successfully I want to delete the archive else not.
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.
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"
精彩评论