Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return command line result in Racket?

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?

like image 652
John Galt Avatar asked Jun 25 '16 08:06

John Galt


1 Answers

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"
like image 105
soegaard Avatar answered Oct 13 '22 19:10

soegaard