开发者

Executing system command in Vala

I would like to execute a command (like ls开发者_JAVA技巧) in Vala, like the Python os.system function, or, better, the popen function. Any idea ?


OK, got it : Glib.Process.spawn_command_line_sync.


It's best to use the package posix. Then, just do Posix.system("command") which returns an int.

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


You can use the GLib.Process.spawn_command_line_sync as:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜