I would like to execute a command (like ls) in Vala, like the Python os.system function, or, better, the popen function. Any idea ?
Executing system command in Vala
It's best to use the package posix
.
Then, just do Posix.system("command")
which returns an int.
OK, got it : Glib.Process.spawn_command_line_sync.
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
It's best to use the package posix
.
Then, just do Posix.system("command")
which returns an int.
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;
}
© 2022 - 2024 — McMap. All rights reserved.