Invoking tclsh or wish from Bourne Shell

Here is a template for a Bourne shell script I used that accomplishes the following:

  • Allows me to specify a specific version of Tcl and Tk to use (and assumes that the versions are identical).
  • Allows me to specify the paths to those different versions such that I insure I use those versions and not other versions that happen to reside on my system (Linux).
  • Sets the TCLLIBPATH environment variable to the directory or directories where extra packages are to be found. Currently set to "." which will have to be changed for your use.
  • Is able to be used for calling wish or tclsh.

Here is the script:

  #!/bin/sh
  # -*-mode: Shell-script; indent-tabs-mode: nil; -*-

  uname_s=`uname -s | sed 's,[. -],_,g'`
  uname_r=`uname -r`; uname_n=`uname -n`
  OS_REV=`echo ${uname_s}_${uname_r} | sed 's,[. -],_,g'`

  # Customize as needed:  
  tcl_partial_version=8.4
  tcl_full_version=8.4.1
  # directory above the Tcl "bin" directory:
  tcl_head_dir=/scratch1/tcl${tcl_full_version}/$OS_REV
  # directory above the Tk "bin" directory:
  tk_head_dir=/scratch1/tk${tcl_full_version}/$OS_REV

  tcl_bin=$tcl_head_dir/bin
  tk_bin=$tk_head_dir/bin

  TCL_LIBRARY=$tcl_head_dir/lib; export TCL_LIBRARY
  TK_LIBRARY=$tk_head_dir/lib; export TK_LIBRARY


  if [ ! -d $TCL_LIBRARY ]; then
      echo "ERROR: TCL_LIBRARY of $TCL_LIBRARY does not exist as a directory"
      exit 1
  fi

  if [ ! -d $TK_LIBRARY ]; then
      echo "ERROR: TK_LIBRARY of $TK_LIBRARY does not exist as a directory"
      exit 1
  fi

  # echo TCL_LIBRARY $TCL_LIBRARY
  # echo TK_LIBRARY $TK_LIBRARY
  # echo TCLLIBPATH $TCLLIBPATH

  # I do not know if modifying the PATH is a good idea, but I did it just to be safe since on my system there
  # are multiple versions of Tcl and Tk executables in the PATH, and I want to be sure to be using a specific version:
  PATH="${tcl_bin}:${tk_bin}:$PATH"; export PATH


  # Customize as needed:  
  # TCLLIBPATH must be set to the path of where our packages are, and
  # the pkg_mkIndex has to be called to create a file in that
  # directory all for the sake of being able to call "package require FooBar 1.0":
  TCLLIBPATH=.; export TCLLIBPATH

  # Call tclsh for just Tcl scripts:  
  ### tclsh${tcl_partial_version} a_script_that_uses_FooBar.tcl "$@"

  # Call wish for just Wish scripts:  
  wish${tcl_partial_version} a_script_that_uses_FooBar.tcl "$@"

RS: Note that since 8.4, it is no longer necessary to keep a separate wish - best always call tclsh, and start Tk-ish scripts with

 package require Tk

See also Bsh