如何从C#应用程序执行linux shell脚本?

时间:2017-05-25 08:22:40

标签: c# linux bash shell cygwin

我有一个linux shell脚本,我可以在Windows中使用CygWin终端手动执行。

sh E://Work/sync.sh E://Work/Voice/Temp

但是当我尝试使用相同的参数在我的C#应用​​程序中运行shell脚本时,我遇到了一些困难。

string command = "E://Work/sync.sh E://Work/Voice/Temp";
ProcessStartInfo psi = new ProcessStartInfo(@"C:\cygwin64\bin\bash.exe", command);
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
Console.WriteLine(p.StandardError.ReadToEnd());
Console.WriteLine(p.StandardOutput.ReadToEnd());

它给了我错误:

E://Work/sync.sh: line 47: rm: command not found
E://Work/sync.sh: line 48: mkdir: command not found
E://Work/sync.sh: line 50: ls: command not found
E://Work/sync.sh: line 59: ls: command not found
E://Work/sync.sh: line 60: rmdir: command not found

然后我尝试了另一种方法。

ProcessStartInfo psi = new ProcessStartInfo(@"C:\cygwin64\bin\bash.exe");
psi.Arguments = "--login -c sh E://Work/sync.sh  E://Work/Voice/Temp";
Process p = Process.Start(psi);

这会启动一个CygWin命令提示符,如下所示: enter image description here

然后如果我手动输入我的命令就行了。 E://Work/sync.sh E://Work/Voice/Temp

如何在没有任何手动输入的情况下从我的应用程序自动执行脚本?

1 个答案:

答案 0 :(得分:0)

经过一些谷歌搜索,我找到了一种方法来做到这一点。我所要做的就是将"--login -c"更改为"--login -i"

ProcessStartInfo psi = new ProcessStartInfo(@"C:\cygwin64\bin\bash.exe");
psi.Arguments = "--login -i E://Work/sync.sh  E://Work/Voice/Temp";
Process p = Process.Start(psi);
相关问题