运行 bash 来自单声道的管道命令 linux

running bash piped comand from mono linux

我正在尝试 运行 bash 使用单进程启动的管道命令

这是我正在尝试的代码片段

单个命令有效,但是管道命令无法 运行,我在这里缺少什么?

            ProcessStartInfo oInfo = new ProcessStartInfo(command, args);    
            oInfo.UseShellExecute = false;   
            oInfo.CreateNoWindow = true;   
            oInfo.RedirectStandardOutput = true;    
            oInfo.RedirectStandardError = true;    
            StreamReader srOutput = null;   
            StreamReader srError = null;    
            Process proc = System.Diagnostics.Process.Start(oInfo);
            proc.WaitForExit();

我试过 运行宁 "ps -aux" 运行 没问题。但是 ps -aux | grep gnome 命令失败。

我试过这些场景 场景 1: 命令 = ps 参数 = -aux | grep 侏儒

场景 2: 命令 = ps argument = "-c ' -aux | grep gnome ' "

场景 3:

命令=ps 参数 = " -c \" -aux | grep 侏儒 \" "

所有这些都失败了

错误:垃圾选项

用法: ps[选项]

尝试'ps --help ' 或 'ps --help ' 获取更多帮助文本。

另外一个附带的问题,我尝试这样做的原因是要找出一个特定的守护进程已经 运行ning。有没有标准的方法来获取此信息。 例如在 windows 中,我们可以使用 ServiceController.GetServices() 查询 运行ning 服务。

是否可以直接在 mono/Linux 上获得类似的东西?

当您添加“|”时对于 bash 行,bash 解释器将命令分成两个进程并将输出从一个进程提供给另一个进程,当您使用 Process 调用该命令时,它按原样发送参数。

您可以实现的最接近的结果是自己启动这两个进程,并通过 input/output 流将另一个进程的输出提供给其中一个。

关于你的问题的第 2 部分,如果程序以特权运行,那么 Process.GetProcesses 将列出系统上的所有 运行 个进程,包括守护进程。