美文网首页
Mac环境下C#调用shell命令

Mac环境下C#调用shell命令

作者: 临川慕容吹雪 | 来源:发表于2019-01-03 16:36 被阅读51次

正在优化unity打包工程。想用c#代码调用shell脚本来实现打包xcode导出ipa.由于对C#代码不是很熟悉。调用shell脚本的时候遇到一些问题。现在把调用shell脚本C#代码在此记录下。

使用terrminal调用shell脚本,可以使用终端直观看到执行结果。

string command = "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal"; 
string shell = ProjectPath +"/xcodebuild.sh";
string argss =  shell;
System.Diagnostics.Process.Start(command,argss);
UnityEngine.Debug.Log(argss);

但是发现这个方法我暂时无法给shell命令传递参数进去。不知道有没有人知道怎么办。
不使用terrminal的话。倒是可以。

//用于做测试 shell传递参数需要用下面方法
string shell = ProjectPath +"/shell.sh";
string arg1 =  "unity";
string arg2 =  ProjectPath +"/test.log";
string argss =  shell +" "+ arg1 +" " + arg2;
System.Diagnostics.Process.Start("/bin/bash", argss);
//shell.sh里面内容很简单。#!/bin/bash  echo $1 >> $2 
就是把第一个命令参数的值拷贝到第二个参数/test.log文件里

还可以类似这样写:

string shell = "shell.sh";
Process process = new Process ();
process.StartInfo.CreateNoWindow = false;
process.StartInfo.ErrorDialog = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = shell;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.WorkingDirectory = ProjectPath;
process.Start();
UnityEngine.Debug.Log( "process start rojectPath = !!" +ProjectPath);
string output = process.StandardOutput.ReadToEnd();
File.AppendAllText (ProjectPath + "/log.text", output); //将得到的信息写入文件中。
process.WaitForExit();
process.Close();

其中注意一点就是如果我们需要指定shell命令当前运行的目录,
可以设置StartInfo.WorkingDirectory属性值。

参考:解决Unity不能调用shell脚本传递参数的问题

相关文章

网友评论

      本文标题:Mac环境下C#调用shell命令

      本文链接:https://www.haomeiwen.com/subject/ddxqrqtx.html