4
假設我想在Haskell中執行命令unrar x archivename
。如何獲取系統進程的退出代碼?
什麼是最好的方式來做到這一點,以及如何獲得命令的退出代碼?如果命令成功退出,我想刪除其他的檔案。
假設我想在Haskell中執行命令unrar x archivename
。如何獲取系統進程的退出代碼?
什麼是最好的方式來做到這一點,以及如何獲得命令的退出代碼?如果命令成功退出,我想刪除其他的檔案。
在process
函數庫中,您可以找到函數readProcessWithExitCode
,它可以執行所需操作。例如:
(e,_,_) <- readProcessWithExitCode "unrar" ["unrar", "x", "-p-", "archivename"] ""
if e == ExitSuccess then ... else ...
還有很多其他解決方案,例如system
命令。拿你的選擇。
System.Process
的readProcessWithExitCode
函數應該可以完成這項工作。
import Control.Monad
import System.Directory
import System.Exit
import System.Process
main = do
(exitCode, _, _) <- readProcessWithExitCode "unrar" ["x", "archivename"] ""
when (exitCode == ExitSuccess) $ removeFile "archivename"