Javascript Gnome外壳扩展:如何使用管道运行命令

Javascript Gnome外壳扩展:如何使用管道运行命令,javascript,glib,gnome,gnome-3,gnome-shell-extensions,Javascript,Glib,Gnome,Gnome 3,Gnome Shell Extensions,所以我在做一个Gnome外壳扩展。我希望能够用管道运行一些命令。(该命令实际上是“xrandr--query | awk'something'”,但这与主题无关) 到目前为止,我所做的是 GLib.spawn_async_with_pipes(null, ['/usr/bin/xrandr', '--query', '|', 'awk...'], null, GLib.SpawnFla

所以我在做一个Gnome外壳扩展。我希望能够用管道运行一些命令。(该命令实际上是
“xrandr--query | awk'something'”
,但这与主题无关)

到目前为止,我所做的是

GLib.spawn_async_with_pipes(null,
                            ['/usr/bin/xrandr', '--query', '|', 'awk...'], null,
                            GLib.SpawnFlags.DO_NOT_REAP_CHILD, null);
但它不起作用! 我找不到任何在gnome扩展中使用管道运行命令的示例


我是否必须像以前那样在命令中编写
“|”

使用管道生成异步\u
不会做你想做的事情(以一种简单的方式)。它将管道返回以进行处理。你可以通过两次呼叫和连接来完成,但这会有点复杂

保持精确语法的一个简单方法是调用一个shell,该shell将执行管道处理 借助它提供了一种调用命令的方法,我编写了以下代码,使用正确的参数调用shell(本例中为bash)

const Util = imports.misc.util;
Util.spawn(['/bin/bash', '-c', "xrandr --query | awk 'something'"])

不久前,我在肉桂小程序中实现了TerminalReader类:

这个类现在也在其他地方使用,所以你有更多的例子来解释它:

下面是该类的源代码:

function TerminalReader() {
    this._init.apply(this, arguments);
}

TerminalReader.prototype = {
_init: function(command, callback) {
    this._callbackPipe = callback;
    this._commandPipe = command;
    this.idle = true;
    this._childWatch = null;
},

executeReader: function() {
    if(this.idle) {
        this.idle = false;
        try {
            let [success, argv] = GLib.shell_parse_argv("sh -c '" + this._commandPipe + "'");
            if(success) {
                let [exit, pid, stdin, stdout, stderr] =
                  GLib.spawn_async_with_pipes(
                      null, // cwd
                      argv, // args
                      null, // env
                      GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD, //Use env path and no repet
                      null // child_setup
                  );

                this._childPid = pid;
                this._stdin = new Gio.UnixOutputStream({ fd: stdin, close_fd: true });
                this._stdout = new Gio.UnixInputStream({ fd: stdout, close_fd: true });
                this._stderr = new Gio.UnixInputStream({ fd: stderr, close_fd: true });

                // We need this one too, even if don't actually care of what the process
                // has to say on stderr, because otherwise the fd opened by g_spawn_async_with_pipes
                // is kept open indefinitely
                this._stderrStream = new Gio.DataInputStream({ base_stream: this._stderr });
                this._dataStdout = new Gio.DataInputStream({ base_stream: this._stdout });
                this._cancellableStderrStream = new Gio.Cancellable();
                this._cancellableStdout = new Gio.Cancellable();

                this.resOut = 1;
                this._readStdout();
                this.resErr = 1;
                this._readStderror();

                this._childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
                    GLib.source_remove(this._childWatch);
                    this._childWatch = null;
                    this._stdin.close(null);
                    this.idle = true;
                }));
            }
            //throw
        } catch(err) {
            if(err.code == GLib.SpawnError.G_SPAWN_ERROR_NOENT) {
                err.message = _("Command not found.");
            } else {
                // The exception from gjs contains an error string like:
                //   Error invoking GLib.spawn_command_line_async: Failed to
                //   execute child process "foo" (No such file or directory)
                // We are only interested in the part in the parentheses. (And
                // we can't pattern match the text, since it gets localized.)
                err.message = err.message.replace(/.*\((.+)\)/, '$1');
            }
            throw err;
        }
    }
},

destroy: function() {
    try {
        if(this._childWatch) {
            GLib.source_remove(this._childWatch);
            this._childWatch = null;
        }
        if(!this._dataStdout.is_closed()) {
            this._cancellableStdout.cancel();
            this._stdout.close_async(0, null, Lang.bind(this, this.closeStdout));
        }
        if(!this._stderrStream.is_closed()) {
            this._cancellableStderrStream.cancel();
            this._stderrStream.close_async(0, null, Lang.bind(this, this.closeStderrStream));
        }
        this._stdin.close(null);
        this.idle = true;
    }
    catch(e) {
        Main.notify("Error on close" + this._dataStdout.is_closed(), e.message);
    }
},

closeStderrStream: function(std, result) {
    try {
        std.close_finish(result);
    } catch(e) {
        std.close_async(0, null, Lang.bind(this, this.closeStderrStream));
    }
},

closeStdout: function(std, result) {
    try {
        std.close_finish(result);
    } catch(e) {
        std.close_async(0, null, Lang.bind(this, this.closeStderrStream));
    }
},

_readStdout: function() {
    this._dataStdout.fill_async(-1, GLib.PRIORITY_DEFAULT, this._cancellableStdout, Lang.bind(this, function(stream, result) {
        try {
            if(!this._dataStdout.is_closed()) {
                if(this.resOut != -1)
                    this.resOut = this._dataStdout.fill_finish(result);// end of file
                if(this.resOut == 0) {
                    let val = stream.peek_buffer().toString();
                    if(val != "")
                        this._callbackPipe(this._commandPipe, true, val);
                    this._stdout.close(this._cancellableStdout);
                } else {
                    // Try to read more
                    this._dataStdout.set_buffer_size(2 * this._dataStdout.get_buffer_size());
                    this._readStdout();
                }
            }
        } catch(e) {
            global.log(e.toString());
        }
    }));
},

_readStderror: function() {
    this._stderrStream.fill_async(-1, GLib.PRIORITY_DEFAULT, this._cancellableStderrStream, Lang.bind(this, function(stream, result) {
        try {
            if(!this._stderrStream.is_closed()) {
                if(this.resErr != -1)
                    this.resErr = this._stderrStream.fill_finish(result);
                if(this.resErr == 0) { // end of file
                    let val = stream.peek_buffer().toString();
                    if(val != "")
                        this._callbackPipe(this._commandPipe, false, val);
                    this._stderr.close(null);
                } else {
                    this._stderrStream.set_buffer_size(2 * this._stderrStream.get_buffer_size());
                    this._readStderror();
                }
            }
        } catch(e) {
            global.log(e.toString());
        }
    }));
}
};

使用管道()调用
spawn\u async\u对您没有帮助,因为它会返回一个对象,其中包含stdin/stdout/stderr的可用管道,而不是通过管道相互调用。
除了调用一个shell实例并让它执行命令外,唯一的方法是坚持使用扩展本身,让GNOME使用临时文件和默认shell(如果已经存在,则会覆盖自身)处理所有事情

对于示例代码,让我们在CLI上执行与
journalctl | grep-i js
相同的操作:

//Gio for File I/O and GLib for command execution
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
//Create an object for the temp file
let file = Gio.file_new_tmp(null);
//Now write the output of journalctl to the file
file[1].get_output_stream().write(GLib.spawn_command_line_sync("journalctl")[1], null);
//Execute the command and save the result, this locks the thread though,
//so don't use indefinite commands like journalctl -f
//This returns [Boolean success, String stdout, String stderr, Number exit_status]
let journalJS = GLib.spawn_command_line_sync('grep -i js ' + file[0].get_path())[1]
现在,您可以随意处理您拥有的数据,因为在命令完成后,所有内容都会退出


不要忘记使用
文件[1].close()关闭文件并在完成时将所有剩余变量设置为
null

可以包含指向
spawn\u async\u和\u管道的链接
文档?尝试不使用逗号
['/usr/bin/xrandr--query | awk…']
?如果没有逗号,它根本不起作用。为什么不将所有管道放在bash脚本中,然后直接使用bash脚本?尝试使用
awk
的完整路径?“默认情况下,程序名称必须是完整路径。”您需要使用管道调用
spawn\u async\u两次,一次用于
xrandr
命令,一次用于
awk
命令。从第一个中捕获标准,并将其作为参数传递给第二个。不能使用
|
作为参数来运行shell管道。太好了!它奏效了,应该是公认的答案!