【问题标题】:Executing system command in Vala在 Vala 中执行系统命令
【发布时间】:2011-03-12 09:01:40
【问题描述】:

我想在 Vala 中执行一个命令(比如 ls),比如 Python os.system 函数,或者更好的是 popen 函数。有什么想法吗?

【问题讨论】:

    标签: function system command vala


    【解决方案1】:

    好的,明白了:Glib.Process.spawn_command_line_sync。

    【讨论】:

    • 你可以接受你自己的答案 ;) 还是有什么要澄清的?
    • 其实在windows下用起来好像很复杂,但另一方面我已经不需要了。
    • 这正是我对答案的评论。 ;) 很好,你也发现了。
    【解决方案2】:

    最好使用包posix。 然后,只需执行Posix.system("command"),它会返回一个 int。

    http://www.valadoc.org/posix/Posix.system.html

    【讨论】:

      【解决方案3】:

      您可以将GLib.Process.spawn_command_line_sync 用作:

      public static int main (string[] args) {
          string ls_stdout;
          string ls_stderr;
          int ls_status;
      
          try {
              Process.spawn_command_line_sync ("ls",
                                          out ls_stdout,
                                          out ls_stderr,
                                          out ls_status);
      
              // Output: <File list>
              print ("stdout:\n");
              // Output: ````
              print (ls_stdout);
              print ("stderr:\n");
              print (ls_stderr);
              // Output: ``0``
              print ("Status: %d\n", ls_status);
          } catch (SpawnError e) {
              print ("Error: %s\n", e.message);
          }
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-29
        • 1970-01-01
        • 2021-09-07
        • 2014-04-16
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多