[ Team LiB ] Previous Section Next Section

UNIX Tcl Scripts

On UNIX you can create a standalone 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!}

The Tk hello world program from Chapter 23 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/linux-ix86/bin/tclsh8.4

graphics/common_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 older versions of 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 exec 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. The exec command (in /bin/sh) replaces the current program, so that is all that the Bourne shell processes; Tcl interprets the rest of the script.

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.

You can hide more than one Bourne shell command in a script with this trick. For example, you might need to set environment variables:

#!/bin/sh
# \
export LD_LIBRARY_PATH=/usr/local/lib
# \
exec /usr/local/bin/tclsh "$0" ${1+"$@"}
    [ Team LiB ] Previous Section Next Section