Created
November 14, 2023 16:55
-
-
Save instance-id/5b85b1debf30cc9aea2f6731e25a3100 to your computer and use it in GitHub Desktop.
Shell call example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dotnet-script | |
#r "System.Console" | |
#r "System.Diagnostics.Process" | |
using System; | |
using System.Diagnostics; | |
public interface IShell | |
{ | |
string Execute(string cmd); | |
string ls(params string[] args); | |
} | |
public class Pwsh : IShell | |
{ | |
private System.Diagnostics.Process process; | |
public string ls(params string[] args) => Execute("ls " + string.Join(" ", args)); | |
private string EscapeArgs(string cmd) => cmd.Replace("\"", "\\\""); | |
public Pwsh() | |
{ | |
process = new Process() | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "/bin/pwsh", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}; | |
} | |
public string Execute(string cmd) | |
{ | |
process.StartInfo.Arguments = $"-c \"source /usr/local/bin/kv-sh/kv-sh; {EscapeArgs(cmd)}\""; | |
process.Start(); | |
string result = process.StandardOutput.ReadToEnd(); | |
process.WaitForExit(1000); | |
return result; | |
} | |
} | |
public class Bash : IShell | |
{ | |
private System.Diagnostics.Process process; | |
public string ls(params string[] args) => Execute("ls " + string.Join(" ", args)); | |
private string EscapeArgs(string cmd) => cmd.Replace("\"", "\\\""); | |
public Bash() | |
{ | |
process = new Process() | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "/bin/bash", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}; | |
} | |
public string Execute(string cmd) | |
{ | |
process.StartInfo.Arguments = $"-c \"source /usr/local/bin/kv-sh/kv-sh; {EscapeArgs(cmd)}\""; | |
process.Start(); | |
string result = process.StandardOutput.ReadToEnd(); | |
process.WaitForExit(1000); | |
return result; | |
} | |
public string Execute(string description, string cmd) | |
{ | |
process.StartInfo.Arguments = $"-c \"{EscapeArgs(cmd)}\""; | |
DateTime startTime = DateTime.Now; | |
process.Start(); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine("----- STARTED [{0} - {1}] : {2} -----", startTime.ToShortDateString(), startTime.ToShortTimeString(), description); | |
Console.ForegroundColor = ConsoleColor.White; | |
string result = process.StandardOutput.ReadToEnd(); | |
do | |
{ | |
process.Refresh(); | |
Console.Write("Duration: {0} seconds", DateTime.Now.Subtract(startTime).TotalSeconds); | |
} while (!process.HasExited); | |
Console.WriteLine(); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine("----- FINISHED [{0} - {1}] : {2} -----", DateTime.Now.ToShortTimeString(), DateTime.Now.ToShortTimeString(), description); | |
Console.ForegroundColor = ConsoleColor.White; | |
return result; | |
} | |
} | |
public static string pwsh_to_bash(this string cmd) | |
{ | |
var escapedArgs = cmd.Replace("\"", "\\\""); | |
var process = new Process() | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "usr/bin/bash", | |
Arguments = $"-c \"source /usr/local/bin/kv-sh/kv-sh; {escapedArgs}\"", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}; | |
process.Start(); | |
string result = process.StandardOutput.ReadToEnd(); | |
process.WaitForExit(); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#load "../shell/shell.csx" | |
static IShell _bash = new Bash(); | |
private static string bash(string command) => _bash.Execute(command); | |
bash($"echo 'testing'"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment