【发布时间】:2016-06-04 08:53:52
【问题描述】:
我一直在搞乱通过 C# 触发 bash 脚本。当我第一次使用参数调用“open”命令时,这一切都很好,这反过来又通过终端打开了我的 .command 脚本。
一旦使用“open”命令,Terminal 或 iTerm 将在后台保持打开状态,此时使用参数调用“open”命令将没有进一步的效果。遗憾的是,我不得不手动退出应用程序才能再次触发我的脚本。
如何将参数传递给已经打开的终端应用程序以在不退出的情况下重新启动我的脚本?
我搜索了在线广告似乎无法解决问题,解决打开代码已经花费了很多时间。非常感谢您的帮助。
这是我用来启动进程的 C# 代码:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "open";
p.StartInfo.WorkingDirectory = installFolder;
p.StartInfo.Arguments = "/bin/bash --args \"open \"SomePath/Commands/myscript.command\"\"";
p.Start();
谢谢
编辑: 两个答案都是正确的,这可能对其他人有帮助:
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
startInfo.WorkingDirectory = installFolder;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("echo helloworld");
process.StandardInput.WriteLine("exit"); // if no exit then WaitForExit will lockup your program
process.StandardInput.Flush();
string line = process.StandardOutput.ReadLine();
while (line != null)
{
Debug.Log("line:" + line);
line = process.StandardOutput.ReadLine();
}
process.WaitForExit();
//process.Kill(); // already killed my console told me with an error
【问题讨论】: