简体   繁体   English

通过Mono上的System.Diagnostics.Process执行管道命令

[英]Executed piped commands via System.Diagnostics.Process on Mono

Is it possible to execute the following piped commands via System.Diagnostics.Process ? 是否可以通过System.Diagnostics.Process执行以下管道命令?

echo "test" | sudo -S shutdown -r +1

I've tried setting the file name to "/bin/bash", with the above as arguments, with no success. 我已经尝试将文件名设置为“/ bin / bash”,以上为参数,但没有成功。

...
var processStartInfo = new ProcessStartInfo { FileName = "/bin/bash", Arguments = "echo \"test\" | sudo -S shutdown -r +1" };
process.StartInfo = processStartInfo;
...

bash is interpreting your command as a file name followed by arguments, meaning it invokes echo and passes all the rest (including the pipe | ) to it for printing so you will get test | sudo -S shutdown -r +1 bash的解释你的命令为文件名后面的参数,这意味着它调用echo并将所有其他(包括管道| )将其打印所以你会得到test | sudo -S shutdown -r +1 test | sudo -S shutdown -r +1 echoed and sudo won't be executed. test | sudo -S shutdown -r +1 echoed和sudo将不会被执行。

You should use the -c option to execute a command. 您应该使用-c选项来执行命令。 Furthermore, you should quote the command itself so that it gets passed as a single argument. 此外,您应该引用命令本身,以便它作为单个参数传递。 Something like this should work: 这样的事情应该有效:

var processStartInfo = new ProcessStartInfo
{
    FileName = "/bin/bash",
    Arguments = "-c \"echo test | sudo -S shutdown -r +1\""
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM