Search Unity

Run terminal command and get output within Unity application(OSX)

Discussion in 'Editor & General Support' started by SweetMei25, May 23, 2019.

  1. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Dear Professionals.
    I would like to run this OSX Terminal command using Unity's Process, ProcessStartInfo and would like to get the output inside Unity application.

    I tried many examples, but all didn't work.

    This is my final code(Not working) and I 'd like any professional help on this script:


    Code (CSharp):
    1. using System;
    2. using System.Diagnostics;
    3. using UnityEngine;
    4.  
    5. public class GetDeviceInfo: MonoBehaviour
    6. {
    7.     private void Start()
    8.     {
    9.  
    10. #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
    11.         ExecuteProcessTerminal("ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'");
    12. #endif
    13.     }
    14.  
    15.     private void ExecuteProcessTerminal(string argument)
    16.     {
    17.         try
    18.         {
    19.             UnityEngine.Debug.Log("============== Start Executing [" + argument + "] ===============");
    20.             ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash")
    21.             {
    22.                 WorkingDirectory = "/",
    23.                 UseShellExecute = false,
    24.                 RedirectStandardOutput = true
    25.             };
    26.             Process myProcess = new Process
    27.             {
    28.                 StartInfo = startInfo
    29.             };
    30.             myProcess.StartInfo.Arguments = argument;
    31.             myProcess.Start();
    32.             string output = myProcess.StandardOutput.ReadToEnd();
    33.             UnityEngine.Debug.Log("Result for [" + argument + "] is : \n" + output);
    34.             myProcess.WaitForExit();
    35.             UnityEngine.Debug.Log("============== End ===============");
    36.         }
    37.         catch (Exception e)
    38.         {
    39.             print(e);
    40.         }
    41.     }
    42. }
    43.  
     
  2. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    What do you mean by "doesn't work"? Do you receive errors, simply no output, etc?
     
  3. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Dear @tsibiski Thanks for your kind reply :)
    So simply no output. No error messages.
     
  4. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    First, let's make sure our bash command is even executing as expected.

    Code (CSharp):
    1. ExecuteProcessTerminal("echo 'Testing' > /Users/YOUR_ACCOUNT/Desktop/testing.txt")
    Make sure to add your account name to that path. Let's see if that file is created.
     
  5. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Dear @tsibiski
    This is the result
    upload_2019-5-24_2-11-25.png
    No output.
    Nothing created.
     
    Last edited: May 23, 2019
  6. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    I just edited my post, sorry. Nothing created. The account name is correct.
     
  7. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    Edited. Didn't see your most recent post. I will think about this some more and get back to you.
     
  8. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Thank you so much @tsibiski , I hope you will help me to fix the problem :)
     
  9. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
  10. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Code (CSharp):
    1. private string ExecuteProcessTerminal(string argument)
    2.     {
    3.         try
    4.         {
    5.             UnityEngine.Debug.Log("============== Start Executing [" + argument + "] ===============");
    6.             ProcessStartInfo startInfo = new ProcessStartInfo()
    7.             {
    8.                 FileName = "/bin/bash",
    9.                 UseShellExecute = false,
    10.                 RedirectStandardError = true,
    11.                 RedirectStandardInput = true,
    12.                 RedirectStandardOutput = true,
    13.                 CreateNoWindow = true,
    14.                 Arguments = " -c \"" + argument + " \""
    15.             };
    16.             Process myProcess = new Process
    17.             {
    18.                 StartInfo = startInfo
    19.             };
    20.             myProcess.Start();
    21.             string output = myProcess.StandardOutput.ReadToEnd();
    22.             UnityEngine.Debug.Log(GetStringResult(output));
    23.             myProcess.WaitForExit();
    24.             UnityEngine.Debug.Log("============== End ===============");
    25.  
    26.             return output;
    27.         }
    28.         catch (Exception e)
    29.         {
    30.             print(e);
    31.             return null;
    32.         }
    33.     }
    This worked fine. Thanks
    I could not find any help from that thread... But luckily, I found this. Thank you so much!
     
    Last edited: May 24, 2019
    ben-rasooli and deus0 like this.
  11. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Arguments = " -c \"" + argument + " \""
    was the key point.
     
    twobob, JamesKaret and khaled24 like this.
  12. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    yeah. It's pretty much a linux type vibe. Glad you got it fixed
     
  13. kkl888

    kkl888

    Joined:
    Dec 6, 2014
    Posts:
    55
    I updated to Unity Hub v3 recently and some 3rd party commands such as Perforce command
    Code (CSharp):
    1. p4 counter change
    is no longer working. It keeps having error
    Code (CSharp):
    1. /bin/bash: p4: command not found
    in
    Code (CSharp):
    1. myProcess.StandardError
    . The same code used to work in Unity Hub 2.4.5. Looks like the new Unity Hub v3 does not include the local machine bash profile. Does anyone have the same issue? Is there a way to fix it?
     
    Last edited: Jan 17, 2022