如何获取系统进程的退出码?

发布于 2024-12-01 18:13:02 字数 113 浏览 1 评论 0原文

假设我想从 Haskell 中执行命令 unrar x archivename

最好的方法是什么以及如何获取命令的退出代码?如果命令成功退出,我想删除存档,否则不删除。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅淡淡 2024-12-08 18:13:02

process 库中,您将找到函数 < a href="http://hackage.haskell.org/packages/archive/process/1.0.1.5/doc/html/System-Process.html#v%3areadProcessWithExitCode" rel="nofollow">readProcessWithExitCode,它可以满足您的需求。类似的:

(e,_,_) <- readProcessWithExitCode "unrar" ["unrar", "x", "-p-", "archivename"] ""
if e == ExitSuccess then ... else ...

还有许多其他解决方案,例如 system 命令。随你挑选。

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.

长亭外,古道边 2024-12-08 18:13:02

readProcessWithExitCode System.Process 中的 函数应该可以完成这项工作。

import Control.Monad
import System.Directory
import System.Exit
import System.Process

main = do
    (exitCode, _, _) <- readProcessWithExitCode "unrar" ["x", "archivename"] ""
    when (exitCode == ExitSuccess) $ removeFile "archivename" 

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" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文