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")

2

2 Answers 2

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();
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.