如何在Racket中返回命令行结果?

Joh*_*alt 5 scheme command-line racket

我可以使用Racket发出bash命令(system "some command"),但该函数返回#t而不是命令行的结果输出,它只打印.如何通过该函数获取返回命令的结果?

soe*_*ard 7

system过程设置stdout为参数的值current-output-port.这意味着我们可以收集写入current-output-port字符串的所有内容并返回该字符串.该构造with-output-to-string设置current-output-port为一个不打印任何内容的端口,但最终会返回以字符串形式写入端口的内容.

> (with-output-to-string (lambda () (system "date")))
"Sat Jun 25 12:20:12 CEST 2016\n"
Run Code Online (Sandbox Code Playgroud)