5

Execute a Shell Process in F#

 2 years ago
source link: https://isthisit.nz/posts/2021/execute-a-shell-process-in-fsharp/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Execute a Shell Process in F#

Synchronously execute a process and wait for it to return; capturing the exit code, stdout, and stderr. It’s a snippet that I often reach for but I can never find it on the internet. Dotnet makes it more difficult than it really should be.

let result = executeProcess "uptime" "-p"

printfn "Exit code: %d" result.ExitCode
printfn "StdOut: %s" result.StdOut

(*
  Exit code: 0
  StdOut: up 2 days, 20 hours, 55 minutes
*)

F# reserves the symbol process so names are slightly awkward.

open System

type ProcessResult = { 
    ExitCode : int; 
    StdOut : string; 
    StdErr : string 
}

let executeProcess (processName: string) (processArgs: string) =
    let psi = new Diagnostics.ProcessStartInfo(processName, processArgs) 
    psi.UseShellExecute <- false
    psi.RedirectStandardOutput <- true
    psi.RedirectStandardError <- true
    psi.CreateNoWindow <- true        
    let proc = Diagnostics.Process.Start(psi) 
    let output = new Text.StringBuilder()
    let error = new Text.StringBuilder()
    proc.OutputDataReceived.Add(fun args -> output.Append(args.Data) |> ignore)
    proc.ErrorDataReceived.Add(fun args -> error.Append(args.Data) |> ignore)
    proc.BeginErrorReadLine()
    proc.BeginOutputReadLine()
    proc.WaitForExit()
    { ExitCode = proc.ExitCode; StdOut = output.ToString(); StdErr = error.ToString() }

Originally based on this implementation.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK