bash unity c# 运行shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26236155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:29:31  来源:igfitidea点击:

Unity c# run shell script

c#macosbashterminalunity3d

提问by roady

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

使用 Unity3D 和编辑器脚本尝试在 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.

当从终端运行 test.sh 时,GDCL 应用程序执行它的操作,然后输出参数。但是如果我从 Unity3D 编辑器运行脚本,我只会在输出中得到参数。GDCL 不运行。

How can I get Unity3D to run terminal scripts?

如何让 Unity3D 运行终端脚本?

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

运行 test.sh 的 C# 脚本(仅提供输出)

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)

test.sh 脚本有 chmod 777 (GDCL 仅适用于终端)

#!/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

回答by ayvazj

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

尝试将 UseShellExecute 设置为 true 或尝试直接运行 shell 并将脚本作为第一个参数传递。

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";

回答by jayant singh

try which GDLC in terminal, get the full path and instead of GDLC in test.sh use full path then it will work

尝试终端中的哪个 GDLC,获取完整路径,而不是 test.sh 中的 GDLC 使用完整路径,然后它将起作用