Call bash or posh from a fsx

I was wondering how to call from a .net50 fsx bash on linux and powershell from windows. Here we go:

Inspired by an article from loune.net, here is how to call bash:

let buildStartBash (cmd : string) =
let escapedArgs = cmd.Replace("\"", "\\\"")
let startInfo = new ProcessStartInfo("/bin/bash", $"-c \"{escapedArgs}\"")
startInfo.RedirectStandardOutput <- true
startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
startInfo

I got the powershell logic from Stack Overflow:

let buildStartPosh cmd = 
let startInfo = new ProcessStartInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", cmd)
startInfo.WorkingDirectory <- Environment.CurrentDirectory
startInfo.RedirectStandardOutput <- true
// startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
startInfo

Besides if you want to know if you are on windows or elsewhere, you can rely on:

let isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)

Mac comes with bash so no worry on this side.

The complete snippet:

open System
open System.Diagnostics
open System.Runtime.InteropServices

let buildStartBash (cmd : string) =
let escapedArgs = cmd.Replace("\"", "\\\"")
let startInfo = new ProcessStartInfo("/bin/bash", $"-c \"{escapedArgs}\"")
startInfo.RedirectStandardOutput <- true
startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
startInfo

let buildStartPosh cmd =
let startInfo = new ProcessStartInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", cmd)
startInfo.WorkingDirectory <- Environment.CurrentDirectory
startInfo.RedirectStandardOutput <- true
// startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
startInfo

let startProcess (startInfo: ProcessStartInfo) :string =
let diagnosticsProcess = new Process()
diagnosticsProcess.StartInfo <- startInfo
diagnosticsProcess.Start() |> ignore
let result = diagnosticsProcess.StandardOutput.ReadToEnd()
diagnosticsProcess.WaitForExit()
result

let isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)

let shrap cmd =
cmd
|> (if isWindows then buildStartPosh else buildStartBash)
|> startProcess
|> printfn "%s"


Tags: dotnet, shell, bash, powershell, fsharp, script

← Back home