home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeBook TitleSearch this book

A.5. tksh

Back in 1996, while a computer science graduate student at Princeton University, Dr. Jeffrey L. Korn[150] wrote tksh. This is an integration of ksh93 with Tcl/Tk. The following quote (from Dr. Korn's research web page) summarizes it well:

[150] Yes, David Korn's son. He now works in the same research center as his father at AT&T Laboratories, although in a different area.

Tksh is a graphical language (similar to Visual Basic or Tcl/Tk) that uses KornShell (ksh93) for scripting and Tk for graphical user interface. Tksh is implemented as a ksh93 extension, and allows Tcl libraries such as Tk to run on top of ksh93 unchanged. ksh93 is well suited for graphical scripting because it is backward compatible with sh, making it both easy to learn and easy to extend existing scripts to provide user interface. Tksh also allows Tcl scripts to run without modification, making it possible to mix and match components written in either Tcl or ksh93.

The tksh home page is still at Princeton: http://www.cs.princeton.edu/~jlk/tksh/. It has links to papers and documentation that can be downloaded and printed. However, the link to tksh executables is out-of-date. The tksh source is available from AT&T Research as part of the ast-open package, which also contains ksh93 and reimplementations of many other Unix tools. See Appendix C for more information.

The following example script, from the USENIX paper on tksh, is called watchdir:

# Tksh Demo
# Jeff Korn
# This script keeps track of visited directories and shows the files
# in the current directory.   You can double-click on files and 
# directories.  The script should be used interactively, so to run:
#   $ tksh
#   $ . scripts/watchdir

function winsetup
    pack $(frame .f)
    frame .f.dirname -relief raised -bd 1
    pack .f.dirname -side top -fill x
    pack $(frame .f.ls) $(frame .f.dirs) -side left 
    label .f.dirname.label -text "Current directory: "
    label .f.dirname.pwd -textvariable PWD
    pack .f.dirname.label .f.dirname.pwd -side left

    scrollbar .f.ls.scroll -command ".f.ls.list yview"
    listbox .f.ls.list -yscroll ".f.ls.scroll set" -width 20 -setgrid 1
    pack $(label .f.ls.label -text "Directory Contents") -side top
    pack .f.ls.list .f.ls.scroll -side left -fill y -expand 1

    scrollbar .f.dirs.scroll -command ".f.dirs.list yview"
    listbox .f.dirs.list -yscroll ".f.dirs.scroll set" -width 20 -setgrid 1
    pack $(label .f.dirs.label -text "Visited Directories") -side top
    pack .f.dirs.list .f.dirs.scroll -side left -fill y -expand 1
    bind .f.dirs.list "<Double-1>" 'cd $(selection get)'
    bind .f.ls.list "<Double-1>" 'tkfileselect $(selection get)'

function tkfileselect
    [[ -d "$1" ]] && tkcd "$1"
    [[ -f "$1" ]] && ${EDITOR-${VISUAL-emacs}} "$1"

function tkcd
    cd $1 > /dev/null || return
    .f.ls.list delete 0 end
    set -o markdirs
    .f.ls.list insert end .. *
    [[ ${VisitedDir["$PWD"]} == "" ]] && .f.dirs.list insert end "$PWD"

typeset -A VisitedDir
winsetup > /dev/null
alias cd=tkcd
tkcd .

What's nice about tksh, besides the interesting blend of complementary technologies, is that it brings Tk programming out to the shell level. Graphics programming with Tk is much higher level than with the Motif toolkit; thus the learning curve is easier to climb, and the scripts are easier to read and write.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.