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.
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
)))