开发者

How to get the exit code of a system process?

开发者 https://www.devze.com 2023-03-31 05:31 出处:网络
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 successf

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" 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号