Reputation: 21
I am new to the TCL language and I would like to use scripts I write to automate some simple tasks that I would normally execute in the Linux terminal interface (or some other shell). My goal right now is to be able to simply start up a terminal (I am using ubuntu so the gnome-terminal command), and then be able to send commands to that terminal instance.
My initial thoughts were that I could simply do something like this :
set myTerminal [open |gnome-terminal r+]
puts $myTerminal "a command"
flush $myTerminal
and that this would flush the characters to the terminal. Nothing happens though and I am not sure how to proceed. Thanks much for your help!
Edit : I would specifically like to use the terminal to run programs that have a command line interface. The idea being that I could start up a terminal using tcl, then give the terminal the command to run that program, and finally use the terminal as an intermediary between the tcl script and the command line program. To surmise, I am using tcl to drive a terminal, which is in turn driving a command line program.
Upvotes: 2
Views: 1607
Reputation: 56118
This is going to be a lot more complicated than it seems like it ought to be. In order to achieve what it looks like you're trying to achieve now you will need to create a program that starts a pseudo-terminal and runs a shell inside it. Then you will have to make gnome-terminal
open up a window that's running your program.
This will be a rather complex operation, and you will need to write a helper program to do it. You will also likely need to use expect, but you will have to use it to launch the helper program.
I wouldn't be surprise to learn that a program that does what the helper program already exists, but I wouldn't be surprised to learn that it doesn't. You might want to read up on openpty
and forkpty
if you want to write it yourself. I don't think you can usefully call those from TCL, but you can from perl or Python.
Upvotes: 1
Reputation: 53921
You should be using expect. It is the standard. It hides most of the shell quirks you will hit if you do it by hand.
It is written in TCL and was made to do exactly what you want.
Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal.
Upvotes: 8