I would like to execute a command (like ls) in Vala, like the Python os.system function, or, better, the popen function. Any idea ?
Asked
Active
Viewed 3,467 times
3 Answers
18
OK, got it : Glib.Process.spawn_command_line_sync.
![](../../users/profiles/384686.webp)
NowhereToHide
- 353
- 2
- 7
-
you can accept your own answer ;) or is there something to clear? – elmarco Jul 06 '10 at 21:52
-
In fact, it seems to be complicated to use under Windows, but on the other hand I don't need it anymore. – NowhereToHide Jul 09 '10 at 13:21
-
that's exactly what I commented on my answer. ;) Good that you find out also. – Marcel Kohls Jul 15 '20 at 11:55
12
It's best to use the package posix
.
Then, just do Posix.system("command")
which returns an int.
![](../../users/profiles/234287.webp)
jcao219
- 2,510
- 3
- 19
- 22
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;
}
![](../../users/profiles/4963247.webp)
Marcel Kohls
- 1,092
- 9
- 19