1

I am trying to do something like:

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")

Where the intended output would be:

ls -a /Users/david (command output!)

But I can't see any way for the result type of execute to "escape" ksprintf. Is there any way for me to capture the output of execute?

4

1 回答 1

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
于 2013-01-09T05:19:10.963 回答