How to run bash cmd in dot.net core

I am using dotnet-core 1.1. centos bash

any way to run grep or wget and get the result?

like cmd in windows, but I need real-time grep log files

System.Diagnostics.Process.Start ("notepad.exe")

+5
source share
2 answers

I consider System.Diagnostics.Process.Start (..), which is located in System.Diagnostics.Process, the nuget package can take the type ProcessStartInfo as one of the overloads. This type has the following properties, if set to true, will redirect logs to the thread in the Process Type, which is returned by System.Diagnostics.Process

var proc = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true } ); proc.StandardError //stream with stderror proc.StandardInput //stream with stdin proc.StandardOutput //stream with stdout 

Shameless plugin, I also made a package that easily abstracts the opening of things on mac / win / linux, basically abstracting xdg-open (ubuntu), open (mac), cmd.exe (win), so you don't need to think about it

https://github.com/TerribleDev/Opener.Net

+2
source

You can start the process for grep and get the result, you can refer to the following code.

  System.Diagnostics.ProcessStartInfo procStartInfo; procStartInfo = new System.Diagnostics.ProcessStartInfo("/bin/bash", "-c \"cat myfile.log | grep -a 'dump f'\""); procStartInfo.RedirectStandardOutput = true; procStartInfo.RedirectStandardError = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = true; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); // Get the output into a string result = proc.StandardOutput.ReadToEnd(); 
0
source

Source: https://habr.com/ru/post/1260848/


All Articles