Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在OSX上使用C#运行shell脚本?_C#_Macos_Unity3d - Fatal编程技术网

如何在OSX上使用C#运行shell脚本?

如何在OSX上使用C#运行shell脚本?,c#,macos,unity3d,C#,Macos,Unity3d,我想用C#来执行shell脚本。 基于类似的问题,我得出了一个类似的解决方案 System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app","sunflow/sunflow.sh"); 它当前打开终端,然后用默认应用程序(在我的例子中是Xcode)打开shell文件。更改默认应用程序不是一个选项,因为其他用户需要安装此应用程序 理想情况下,该解决方案将允许shell文件的参数。我现在无法使用Mac进行测试,但以下

我想用C#来执行shell脚本。 基于类似的问题,我得出了一个类似的解决方案

System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app","sunflow/sunflow.sh");
它当前打开终端,然后用默认应用程序(在我的例子中是Xcode)打开shell文件。更改默认应用程序不是一个选项,因为其他用户需要安装此应用程序


理想情况下,该解决方案将允许shell文件的参数。

我现在无法使用Mac进行测试,但以下代码在Linux上运行,应该可以在Mac上运行,因为Mono与Microsoft的核心.NET接口非常接近:

ProcessStartInfo startInfo = new ProcessStartInfo()
{
    FileName = "foo/bar.sh",
    Arguments = "arg1 arg2 arg3",
};
Process proc = new Process()
{
    StartInfo = startInfo,
};
proc.Start();
关于我的环境的几点注意事项:

  • 我专门创建了一个测试目录来仔细检查这段代码
  • 我在子目录foo中创建了一个文件bar.sh,代码如下:

    #!/bin/sh
    for arg in $*
    do
        echo $arg
    done
    
  • 我将
    Main
    方法包装在Test.cs中上面的C#代码周围,用
    dmcs Test.cs
    编译,并用
    mono Test.exe
    执行

  • 最终输出为“arg1 arg2 arg3”,三个标记之间用换行符分隔

    • 谢谢亚当,这对我来说是一个很好的起点。然而,由于某些原因,当我尝试使用上述代码(根据我的需要进行更改)时,我得到了以下错误

      System.ComponentModel.Win32Exception: Exec format error
      
      请参阅下面给出上述错误的代码

      ProcessStartInfo startInfo = new ProcessStartInfo()
                     {
                             FileName = "/Users/devpc/mytest.sh",
                             Arguments = string.Format("{0} {1} {2} {3} {4}",  "testarg1", "testarg2", "testarg3", "testarg3", "testarg4"),
                             UseShellExecute = false,
                             RedirectStandardOutput = true,
                             CreateNoWindow = true
                         };
                        Process proc = new Process()
                        {
                          StartInfo = startInfo,
                        };
                         proc.Start();
                         while (!proc.StandardOutput.EndOfStream)
                         {
                             string result = proc.StandardOutput.ReadLine();
                             //do something here
                         }
      
      我花了一些时间,得出了下面的结论,它在我的案例中起到了作用——以防万一,如果有人遇到这个错误,请尝试下面的方法

      工作解决方案:

        var command = "sh";
          var scriptFile = "/Users/devpc/mytest.sh";//Path to shell script file
          var arguments = string.Format("{0} {1} {2} {3} {4}",  "testarg1", "testarg2", "testarg3", "testarg3", "testarg4");
          var processInfo = new ProcessStartInfo()
          {
              FileName = command,
              Arguments = arguments,
              UseShellExecute = false,
              RedirectStandardOutput = true,
              CreateNoWindow = true
      
          };
      
          Process process = Process.Start(processInfo);   // Start that process.
          while (!process.StandardOutput.EndOfStream)
          {
              string result = process.StandardOutput.ReadLine();
              // do something here
          }
          process.WaitForExit();
      

      此处无法访问我的mac,但请尝试启动或打开命令