3

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
Beloudest
  • 339
  • 1
  • 5
  • 18

2 Answers2

3

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)

Community
  • 1
  • 1
Dmitriy Khudorozhkov
  • 1,624
  • 12
  • 24
  • 2
    First he must redirect the standard input. You should add that to your answer. – Visual Vincent Jun 04 '16 at 09:42
  • Thanks for this I solved it, I will add the full mantra to my question. You made my day and improved my code so much. I was writing values to text files to retrieve before hand, this way is much better and was unknown to me. – Beloudest Jun 04 '16 at 10:00
  • @DmitriyKhudorozhkov I have had issues since trying to the solution, I eventually discovered RedirectStandardError which enlightened me to the reason why I could not successfully trigger my command with the following error: "errorline:/bin/bash: line 1: convert: command not found" Do you know why this command does not work but will work fine when inputted directly into the bash terminal? – Beloudest Jun 05 '16 at 08:04
0

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("..");
Edgars Pivovarenoks
  • 1,526
  • 1
  • 17
  • 31