Executing system command in Vala
Asked Answered
H

3

7

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

Hendry answered 6/7, 2010 at 15:7 Comment(0)
L
13

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

Loire answered 1/8, 2010 at 14:48 Comment(0)
H
18

OK, got it : Glib.Process.spawn_command_line_sync.

Hendry answered 6/7, 2010 at 15:33 Comment(3)
you can accept your own answer ;) or is there something to clear?Enravish
In fact, it seems to be complicated to use under Windows, but on the other hand I don't need it anymore.Hendry
that's exactly what I commented on my answer. ;) Good that you find out also.Aloise
L
13

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

Loire answered 1/8, 2010 at 14:48 Comment(0)
A
1

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;
}
Aloise answered 21/6, 2020 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.