在 Vala 中执行系统命令

发布于 2024-09-08 01:41:51 字数 73 浏览 2 评论 0原文

我想在 Vala 中执行命令(如 ls),如 Python os.system 函数,或者更好的是 popen 函数。有什么想法吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2024-09-15 01:41:51

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

OK, got it : Glib.Process.spawn_command_line_sync.

梅窗月明清似水 2024-09-15 01:41:51

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

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

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

遮云壑 2024-09-15 01:41:51

您可以使用 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文