I can issue bash commands with Racket with (system "some command")
, but the function returns #t instead of the resulting output from the command-line, which it only prints. How can I get the result of the command to be returned with the function?
How to return command line result in Racket?
Asked Answered
The system
procedure sets stdout
to the value of the paramter current-output-port
. This means that we can collect everything written to current-output-port
to a string and return that. The construct with-output-to-string
sets current-output-port
to a port that doesn't print anything, but eventually returns whatever written to the port as a string.
> (with-output-to-string (lambda () (system "date")))
"Sat Jun 25 12:20:12 CEST 2016\n"
© 2022 - 2024 — McMap. All rights reserved.