Using pipes in the Gambas Exec command.
Posted
#1
(In Topic #108)
Trainee
Code
Exec ["tmux", "send-keys", "cat foo | grep bar" & "\r\n"] 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.
Posted
Trainee
Code
Exec ["tmux", "send-keys", "foo" & "\r\n"]
Exec ["sleep", "1"]
Exec ["tmux", "send-keys", "bar" & "\r\n"]
Posted
Regular

Posted
Guru

As you need to open a Terminal anyway why not just use the Shell command which will allow the use of the "|" pipe?
Code
Shell "ls -a | grep loc" Code
Shell "ls -a | grep loc" WaitCode
Shell "ls -a | grep loc" to sVariableOr did I miss something?
1 guest and 0 members have just viewed this.


