Unity c# run shell script

11,495

Try setting UseShellExecute to true or try running your shell directly and passing the script as the first argument.

psi.UseShellExecute = true; 

Or

ProcessStartInfo psi = new ProcessStartInfo(); 
psi.FileName = "/bin/sh";
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.Arguments = Application.dataPath + "/test.sh" + " arg1 arg2 arg3";

Don't forget to import:

using System.Diagnostics;
Share:
11,495
roady
Author by

roady

Updated on June 24, 2022

Comments

  • roady
    roady almost 2 years

    Using Unity3D and from editor script trying to run a script in the terminal on osx.

    When running test.sh from terminal the GDCL application does its thing and then outputs the arguments. But if I run the script from Unity3D editor I only get the arguments in the output. GDCL doesn't run.

    How can I get Unity3D to run terminal scripts?

    C# script that runs test.sh (gives only output)

    ProcessStartInfo psi = new ProcessStartInfo(); 
    psi.FileName = Application.dataPath+"/test.sh";
    psi.UseShellExecute = false; 
    psi.RedirectStandardOutput = true;
    psi.Arguments = "arg1 arg2 arg3";
    
    //psi.Arguments = "test"; 
    Process p = Process.Start(psi); 
    string strOutput = p.StandardOutput.ReadToEnd(); 
    p.WaitForExit(); 
    UnityEngine.Debug.Log(strOutput);
    

    The test.sh script has chmod 777 (GDCL works only from terminal)

    #!/bin/sh
    GDCL ~/Documents/Unity/testproject/Assets/Font\ Normal.GlyphProject ~/Documents/Unity/testproject/Assets/Textures/fontNormal/font -fo PlainText-txt
    for arg in $*
    do
        echo $arg
    done
    
    • Clay Fowler
      Clay Fowler over 8 years
      Where is GDCL located on disk? Is it in the path?