Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Running bash script from unity - MacOS

Discussion in 'External Tools' started by summerjones96, May 1, 2018.

  1. summerjones96

    summerjones96

    Joined:
    Feb 23, 2018
    Posts:
    2
    Hi!

    I can't get any kind of bash working from my c# script

    I've tried lots of things and I understand this has come up before, I've been googling for hours. Below is an example of something I have tried from stack overflow. I would expect this to print 'is a' to the unity debug console but it doesn't.

    To clarify, what I want to eventually do is run a bash file e.g. test.sh which I am using to run a python file (I promise you it's necessary..)

    Thanks in advance!


    Code (CSharp):
    1. static void Main()
    2.     {
    3.         // lets say we want to run this command:  
    4.         //  t=$(echo 'this is a test'); echo "$t" | grep -o 'is a'
    5.         var output = ExecuteBashCommand("t=$(echo 'this is a test'); echo \"$t\" | grep -o 'is a'");
    6.  
    7.         // output the result
    8.         UnityEngine.Debug.Log
    9. (output);
    10.     }
    11.  
    12.     static string ExecuteBashCommand(string command)
    13.     {
    14.         // according to: https://stackoverflow.com/a/15262019/637142
    15.         // thans to this we will pass everything as one command
    16.         command = command.Replace("\"","\"\"");
    17.  
    18.         var proc = new Process
    19.         {
    20.             StartInfo = new ProcessStartInfo
    21.             {
    22.                 FileName = "/bin/bash",
    23.                 Arguments = "-c \""+ command + "\"",
    24.                 UseShellExecute = false,
    25.                 RedirectStandardOutput = true,
    26.                 CreateNoWindow = true
    27.             }
    28.         };
    29.  
    30.         proc.Start();
    31.         proc.WaitForExit();
    32.  
    33.         return proc.StandardOutput.ReadToEnd();
    34.     }
     
  2. nitrofurano

    nitrofurano

    Joined:
    Jun 7, 2019
    Posts:
    64
    perhaps something appears in the terminal if you run unity from there?