8

i am using dotnet-core 1.1. centos bash

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

like cmd in windows, but i need grep realtime log files

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

Neo Yap
  • 399
  • 1
  • 4
  • 9
  • 1
    Does https://www.nuget.org/packages/runtime.linux.System.Diagnostics.Process/4.1.0-beta-23516 add anything to capture the result? – Derek Beattie Dec 06 '16 at 18:31
  • [You can find the helpful information here](https://stackoverflow.com/questions/46419222/execute-linux-command-on-centos-using-dotnet-core) – Harit Kumar Oct 01 '17 at 16:49

2 Answers2

8

You can start a process to grep and retrieve the result, you can refer 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();
user9501414
  • 81
  • 1
  • 1
3

I believe System.Diagnostics.Process.Start(..) which is in the System.Diagnostics.Process nuget package can take a ProcessStartInfo type as one of the overloads. That type has the following properties when set to true will redirect the logs to a stream in the Process type that 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 plug, I also made a package that easily abstracts opening things on mac/win/linux basically abstracting xdg-open (ubuntu), open (mac), cmd.exe (win) so you don't have to think about it

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

TerribleDev
  • 2,195
  • 1
  • 19
  • 31