Bra*_*ams 4 c# bash shell ubuntu mono
我正在尝试使用此代码创建一个目录,以查看代码是否正在执行,但由于某种原因,它执行时没有错误,但从未创建过该目录。我的代码中是否有错误?
var startInfo = new
var startinfo = new ProcessStartInfo();
startinfo.WorkingDirectory = "/home";
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c cd Desktop && mkdir hey";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();
Console.WriteLine ("Shell has been executed!");
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
这对我来说最有效,因为现在我不必担心转义引号等......
using System;
using System.Diagnostics;
class HelloWorld
{
static void Main()
{
// lets say we want to run this command:
// t=$(echo 'this is a test'); echo "$t" | grep -o 'is a'
var output = ExecuteBashCommand("t=$(echo 'this is a test'); echo \"$t\" | grep -o 'is a'");
// output the result
Console.WriteLine(output);
}
static string ExecuteBashCommand(string command)
{
// according to: /sf/answers/1068341361/
// thans to this we will pass everything as one command
command = command.Replace("\"","\"\"");
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = "-c \""+ command + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.WaitForExit();
return proc.StandardOutput.ReadToEnd();
}
}
Run Code Online (Sandbox Code Playgroud)
这对我有用:
Process.Start("/bin/bash", "-c \"echo 'Hello World!'\"");
Run Code Online (Sandbox Code Playgroud)
我的猜测是您的工作目录不是您期望的位置。
有关工作目录的更多信息,请参见此处Process.Start()
另外你的命令似乎是错误的,使用&&
执行多个命令:
proc.StartInfo.Arguments = "-c cd Desktop && mkdir hey";
Run Code Online (Sandbox Code Playgroud)
第三,您的工作目录设置错误:
proc.StartInfo.WorkingDirectory = "/home";
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15086 次 |
最近记录: |