How to return command line result in Racket?
Asked Answered
H

1

6

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?

Headship answered 25/6, 2016 at 8:30 Comment(0)
E
8

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"
Evesham answered 25/6, 2016 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.