So I'm making a Gnome Shell extension. And I want to be able to run some command with a pipe. (The command is actually "xrandr --query | awk 'something'"
, but that is off topic)
So, what I have done so far is
GLib.spawn_async_with_pipes(null,
['/usr/bin/xrandr', '--query', '|', 'awk...'], null,
GLib.SpawnFlags.DO_NOT_REAP_CHILD, null);
But it doesn't work! I can't find any example of running a command in a gnome extensions with a pipe.
Do I have to write "|"
in the command like I did ?
calling
spawn_async_with_pipes()
isn't going to help you, as it gives you back an object with availlable pipes for stdin/stdout/stderr rather than giving you calls that are piped to one another. Except for just calling a shell instance and letting it execute commands, the only way is to stick to the extension itself, letting GNOME handle everything using a temp file and default shell (one that overwrites itself if it already exists).For the example code, lets do the same as
journalctl | grep -i js
would do on a CLI:Now feel free to do with the data you have, as everything exits after the command is finished.
Don't forget to close the file using
file[1].close();
and setting any remaining variables tonull
when finished.I implemented a TerminalReader class some time ago in a Cinnamon Applet: https://github.com/lestcape/Configurable-Menu/blob/OwnAPI/configurableMenu%40lestcape/pakagesManager.js#L31
This class is now used in other places also, so you have more examples to underestand it better: https://github.com/search?l=JavaScript&q=TerminalReader&type=Code&utf8=%E2%9C%93
Here is the source code of the class:
spawn_async_with_pipes
doesn't do what you want (in a simple way). It return the pipes to process with it. You could do it with two call and connecting but it will be a little bit complex.A simple way to keep the exact syntax is to call a shell which will do the pipe processing with the help of this answer which give a way to call a command, I wrote the following code which call the shell (bash for this case) with correct arguments