Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run bash cmd in dot.net core

Tags:

bash

.net-core

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

like image 249
Neo Yap Avatar asked Dec 06 '16 04:12

Neo Yap


People also ask

How do I run a bash command in terminal?

This method is quite easy to run a bash script, and all of them are quite simple. We just need to type in “source” before the file/script name with an extension. In a terminal, run the following code by replacing the filename with your bash script filename. The script will simply get executed after “sourcing” the file.

What is !/ Bin bash?

The shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts.

What is dotnet Run command?

The dotnet run command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line. The command depends on the dotnet build command to build the code.


2 Answers

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();
like image 52
user9501414 Avatar answered Sep 19 '22 14:09

user9501414


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

like image 35
TerribleDev Avatar answered Sep 19 '22 14:09

TerribleDev