问题描述:

I have been messing around with triggering a bash script via C#. This all works fine when I first call the "open" command with arguments which in turn opens my .command script via Terminal.

Once the "open" command is used once Terminal or iTerm will remain open in the background, at which point calling the "open" command with arguments then has no further effect. I sadly have to manually quit the application to trigger my script again.

How can I pass arguments to an already open terminal application to restart my script without quitting?

I've searched online ad can't seem to work it out, it already took a good amount of time solve the opening code. Your help is much appreciated.

Here is the C# code I'm using to start the process:

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();

Thanks

EDIT:

Both answers were correct, this might help others:

 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

网友答案:

You can try:

before calling p.Start():

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
// for the process to take commands from you, not from the keyboard

and after:

if (p != null)
{
    p.StandardInput.WriteLine("echo helloworld");
    p.StandardInput.WriteLine("executable.exe arg1 arg2");
}

(taken from here)

网友答案:

This is what you may be looking for :

Gets a stream used to write the input of the application.

MSDN | Process.StandardInput Property

// This could do the trick
process.StandardInput.WriteLine("..");
相关阅读:
Top