Using pipes in the Gambas Exec command.
Posted: Monday 13th February 2017 9:34am
For CLI utilities written in Gambas, there is a hack that allows the Unix pipe command in an Exec statement by sending keys to tmux.
The above command lists the contents of the file named 'foo' to the terminal while piping the output to grep to list all locations of 'bar'. I do this exact thing hundreds of times daily, since it takes a fraction of a second to list a > 10,000 line file and to display the occurrences of the text I am searching for. I can now build a powerful set of CLI commands that automates things and eliminates tedious typing.
For this to work, the Gambas CLI executable app has to be executed from a shell (CLI) with TMUX already active.
The last two characters, "\r\n" inserts a carriage return and newline at the end of the string sent to tmux to simulate the user pressing the Enter key.
While this looks trivial, it means Gambas + tmux can be used to manipulate the terminal in just about any possible way which allows for extremely powerful scripts to be written in Gambas. This will allow actions such as changing the active directory in the shell that called the script. Such a directory change will then survive the script termination, something that is impossible by design in the Linux security model.
Code: Select all
Exec ["tmux", "send-keys", "cat foo | grep bar" & "\r\n"]
For this to work, the Gambas CLI executable app has to be executed from a shell (CLI) with TMUX already active.
The last two characters, "\r\n" inserts a carriage return and newline at the end of the string sent to tmux to simulate the user pressing the Enter key.
While this looks trivial, it means Gambas + tmux can be used to manipulate the terminal in just about any possible way which allows for extremely powerful scripts to be written in Gambas. This will allow actions such as changing the active directory in the shell that called the script. Such a directory change will then survive the script termination, something that is impossible by design in the Linux security model.