【问题标题】:How to run shell script with C# on OSX?如何在 OSX 上使用 C# 运行 shell 脚本?
【发布时间】:2012-09-06 15:34:21
【问题描述】:

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

System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app","sunflow/sunflow.sh");

它当前打开终端,然后使用默认应用程序(在我的例子中是 Xcode)打开 shell 文件。更改默认应用程序不是一种选择,因为需要为其他用户安装此应用程序。

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

【问题讨论】:

  • 这里无法访问我的 mac,但尝试启动或打开命令

标签: c# macos unity3d


【解决方案1】:

我现在无法在 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
    
  • 我在 Test.cs 中的 C# 代码周围包裹了一个 Main 方法,并使用 dmcs Test.cs 编译,并使用 mono Test.exe 执行。

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

【讨论】:

    【解决方案2】:

    感谢亚当,这对我来说是一个很好的起点。但是,由于某种原因,当我尝试使用上面的代码(更改为我的需要)时,我遇到了错误

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      相关资源
      最近更新 更多