Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Running external processes in Tcl and Tcl/Tk
If you want to run external processes from a Tcl based program (Tcl, Tcl/Tk, expect), there are various ways of doing it.

Firstly, the whole purpose of the Expect extension is to allow you to control other processes via its three major commands of spawn which starts another process, send which sends characters to that other process, and expect which causes your program to wait until it gets a particular response, or one of a series of possible responses.

If you don't need to go to these lengths, though, there are three other options at least.

You can use an exec command to run an external process and capture the result returned in a variable:

set ff [exec ls]
puts $ff


You can use the open command and open a process by preceeding it with a "pipe" character in place of its default behaviour which would be to open a file. This lets you handle a process that generates a lot of output line by line, and by using r+, w+ modes and flush you can also set it up for both read and write. Here's a simple example of opening a piped process for read:

set fh [open |df r]
while {[gets $fh line] >= 0} {
puts $line
}


Finally, you can talk to a remote process via the network, using socket to open a remote connection. Like the open command, the socket command opens a channel so once the channel is set up you can handle the data stream exactly as if it was a file or pipe. Again, don't forget flush. Example (getting a file off a web server):

# Through a socket - a remote process

set fh [socket 192.168.200.66 80]
puts $fh "GET /index.html HTTP/1.0\n"
flush $fh
while {[gets $fh line] >= 0} {
puts $line
}

(written 2006-06-29 11:06:20)

 
Associated topics are indexed under
T209 - Tcl/Tk - File and Directory Handling
T244 - Tcl/Tk - Socket Programming

Back to
Which way to turn?
Previous and next
or
Horse's mouth home
Forward to
First Light, Bootle Docks, Liverpool

Some other Articles
Hot answers in PHP
New - Conditional expressions in Python 2.5
Tk - laying out your GUI with frames, pack and grid
First Light, Bootle Docks, Liverpool
Running external processes in Tcl and Tcl/Tk
Which way to turn?
Good follow up ... my thanks
Converting between Hex and Decimal in Tcl
Tcl - lappend v concat
Separation and Integration
1637 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho