我正在尝试做类似的事情:
let execute command =
System.Diagnostics.Process.Start (command)
sprintf "%s (command output!)" command
let shell fmt = Printf.ksprintf execute fmt
printfn "%s" (shell "ls -a %s" "/Users/david")
预期输出为:
ls -a /Users/david (command output!)
但我无法看到execute
的结果类型有任何方法可以“逃避”ksprintf
。有没有办法让我捕获execute
的输出?
答案 0 :(得分:2)
您需要捕获进程的输出 - 默认情况下,它只使用与包含应用程序相同的终端。类似的东西(取自http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx)
let execute command =
let p = new System.Diagnostics.Process(command)
p.UseShellExecute <- false;
p.RedirectStandardOutput<-true //you might want to do stderr as well
p.Start()
let output = p.StandardOutput.ReadToEnd()
p.WaitForExit()
sprintf "%s %s" command output