在 Xulrunner 中获取脚本(shell 脚本)输出的最佳方法是什么?

发布于 10-20 17:11 字数 1239 浏览 5 评论 0原文

我正在使用 nsIProcess.run() 运行脚本,我发现获得输出的唯一方法是将输出写入文件,然后从 javascript 读取该文件。

但由于某种原因,当我从 xulrunner 应用程序执行它时,它不会生成带有输出的文件。这是我的函数:

function runProcess() {
    // create an nsILocalFile for the executable
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces["nsILocalFile"]);
    file.initWithPath("/home/me/my-script.sh");

    write("FILE EXISTS = " + file.exists()); // it is printing TRUE, good!


    // create an nsIProcess
    var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
    process.init(file);

    // Run the process.
    // If first param is true, calling thread will be blocked until
    // called process terminates.
    // Second and third params are used to pass command-line arguments
    // to the process.
    process.run(true, [], 0);
}

my-script.sh:

echo ok > /tmp/result.txt

是否有更好的(且可行的)方法来从 my-script.sh 获取此“ok”输出?

--update

我使用的是 Ubuntu 10.04 和 Xulrunner 1.9.2.15

I'm running a script with nsIProcess.run(), and the only way I found to get the output is to write the output to a file, and then read the file from javascript.

But for some reason, when I execute it from the xulrunner application, it does not generate the file with the output. Here's my function:

function runProcess() {
    // create an nsILocalFile for the executable
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces["nsILocalFile"]);
    file.initWithPath("/home/me/my-script.sh");

    write("FILE EXISTS = " + file.exists()); // it is printing TRUE, good!


    // create an nsIProcess
    var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
    process.init(file);

    // Run the process.
    // If first param is true, calling thread will be blocked until
    // called process terminates.
    // Second and third params are used to pass command-line arguments
    // to the process.
    process.run(true, [], 0);
}

my-script.sh:

echo ok > /tmp/result.txt

Is there a better (and working) approach to get this "ok" output from my-script.sh?

--update

I'm on Ubuntu 10.04 with Xulrunner 1.9.2.15

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

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

发布评论

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

评论(2

枉心2024-10-27 17:11:55

Enigmail 使用了一个第三方“ipc”库,它允许您捕获命令的输出。它甚至可能在某个时候成为 XULrunner 的一部分。

编辑:正如评论中所讨论的, nsIProcess.run 使用 exec 而不是系统,因此脚本需要有一个 #!以便内核可以生成 shell。

There is a third-party "ipc" library used e.g. by Enigmail that allows you to capture the output of a command. It might even become part of XULrunner at some point.

EDIT: As discussed in the comments, nsIProcess.run uses exec rather than system so the script needs to have a #! line so that the kernel can spawn the shell.

星星的轨迹2024-10-27 17:11:55

从上面的讨论来看,听起来这个过程可能根本就没有开始。

我会尝试两件事

  • 尝试调用其他进程/可执行文件
  • 将 null 更改为 [],也许它默默地失败,期望这是一个数组(即使它是一个空数组)

From the discussion above, it sounds like the process may not be getting kicked off at all.

I would try two things

  • Try calling some other process/executable
  • change null to [], perhaps it is silently failing, expecting this to be an array (even if it is an empty array)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文