UNIX Tcl Scripts

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 2.  Getting Started


On UNIX you can create a stand alone Tcl or Tcl/Tk script much like an sh or csh script. The trick is in the first line of the file that contains your script. If the first line of a file begins with #!pathname, then UNIX uses pathname as the interpreter for the rest of the script. The "Hello, World!" program from Chapter 1 is repeated in Example 2-1 with the special starting line:

Example 2-1 A standalone Tcl script on UNIX.
 #!/usr/local/bin/tclsh puts stdout {Hello, World!} 

Similarly, the Tk hello world program from Chapter 21 is shown in Example 2-2:

Example 2-2 A standalone Tk script on UNIX.
 #!/usr/local/bin/wish button .hello -text Hello -command {puts "Hello, World!"} pack .hello -padx 10 -pady 10 

The actual pathnames for tclsh and wish may be different on your system. If you type the pathname for the interpreter wrong, you receive a confusing "command not found" error. You can find out the complete pathname of the Tcl interpreter with the info nameofexecutable command. This is what appears on my system:

 info nameofexecutable => /home/welch/install/solaris/bin/tclsh8.2 

graphics/tip_icon.gif

Watch out for long pathnames.


On most UNIX systems, this special first line is limited to 32 characters, including the #!. If the pathname is too long, you may end up with /bin/sh trying to interpret your script, giving you syntax errors. You might try using a symbolic link from a short name to the true, long name of the interpreter. However, watch out for systems like Solaris in which the script interpreter cannot be a symbolic link. Fortunately, Solaris doesn't impose a 32-character limit on the pathname, so you can just use a long pathname.

The next example shows a trick that works around the pathname length limitation in all cases. The trick comes from a posting to comp.lang.tcl by Kevin Kenny. It takes advantage of a difference between comments in Tcl and the Bourne shell. Tcl comments are described on page 16. In the example, the Bourne shell command that runs the Tcl interpreter is hidden in a comment as far as Tcl is concerned, but it is visible to /bin/sh:

Example 2-3 Using /bin/sh to run a Tcl script.
 #!/bin/sh # The backslash makes the next line a comment in Tcl \ exec /some/very/long/path/to/wish "$0" ${1+"$@"} #  ... Tcl script goes here ... 

You do not even have to know the complete pathname of tclsh or wish to use this trick. You can just do the following:

 #!/bin/sh # Run wish from the users PATH \ exec wish -f "$0" ${1+"$@"} 

The drawback of an incomplete pathname is that many sites have different versions of wish and tclsh that correspond to different versions of Tcl and Tk. In addition, some users may not have these programs in their PATH.

If you have Tk version 3.6 or earlier, its version of wish requires a -f argument to make it read the contents of a file. The -f switch is ignored in Tk 4.0 and higher versions. The -f, if required, is also counted in the 32-character limit on #! lines.

 #!/usr/local/bin/wish -f 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net