7

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

4

3 回答 3

18

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

于 2010-07-06T15:33:03.247 回答
13

最好使用包posix。然后,只需执行Posix.system("command")返回 int 的操作。

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

于 2010-08-01T14:48:46.837 回答
1

您可以将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;
}
于 2020-06-21T07:54:57.500 回答