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

时间:2011-03-04 14:29:54

标签: shell xul xulrunner

我正在使用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”输出?

- 更新

我使用的是Xulrunner 1.9.2.15的Ubuntu 10.04

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

我会尝试两件事

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