How to return exit status and output of system commands in Racket?

匆匆过客 提交于 2019-12-12 04:35:38

问题


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.

How do I do this in Racket?

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.


回答1:


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.

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.

For example:

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


来源:https://stackoverflow.com/questions/42126030/how-to-return-exit-status-and-output-of-system-commands-in-racket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!