在 Xulrunner 中获取脚本(shell 脚本)输出的最佳方法是什么?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
从上面的讨论来看,听起来这个过程可能根本就没有开始。
我会尝试两件事
From the discussion above, it sounds like the process may not be getting kicked off at all.
I would try two things