如何在Racket中返回退出状态和系统命令输出? [英] How to return exit status and output of system commands in Racket?

查看:91
本文介绍了如何在Racket中返回退出状态和系统命令输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不仅想捕获命令的输出

I'd like to not only capture the output of a command like with

(with-output-to-string (lambda () (system "ls -la")))

但也希望能够访问退出代码,这样我就不必解析输出即可知道命令是否成功,并且可以对此做出相应的反应。

But also would like to be able to access the exit code, so that I do not have to parse the output to know whether the command was successful or not and can react on it accordingly.

我该如何

我找到了有关子进程,但我不知道如何提供标准输出等所有参数。我想看一些综合的示例,如果命令成功执行,如果命令执行不成功,则使用输出,对退出代码应该有一些反应。

I found the documentation about subprocess, but I don't know how to provide all the arguments like standard out. I'd like to see some comprehensive example, in which the output is used if the command was successful and the if the command was unsuccessful, there should be some reaction to the exit code.

推荐答案

球拍提供了 system / exit-code 过程,其中就像 system 一样,但是返回退出代码而不是布尔成功指示符。否则,它的行为与 system 完全相同。

Racket provides a system/exit-code procedure, which is like system but returns the exit code instead of a boolean success indicator. It otherwise behaves exactly like system.

如此说来,如果您只需要知道命令是否已运行成功,并且不需要实际的退出代码, system 实际上就足够了。如文档所述,如果成功,则返回true,否则返回false。

In so saying, if all you need to know is whether the command ran successfully, and don't need the actual exit code itself, system is actually sufficient. As the documentation says, it returns true if successful and false otherwise.

例如:

(with-output-to-string
  (lambda ()
    (unless (system "ls -la")
      ;; handle error here
      )))

这篇关于如何在Racket中返回退出状态和系统命令输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆