-->
Previous Table of Contents Next


Linux Scripting Languages

In addition to the programming languages discussed earlier, Linux offers even more, including a number of scripting languages. A scripting language is a lot like the language that comes with the UNIX shell. The main difference between a programming language and a scripting language is that scripting languages are usually interpreted instead of compiled, and scripting languages usually make it easier to launch Linux commands from within your programs—called scripts when you use a scripting language. As you can tell, the line between programming languages and scripting languages is blurry.

Of the scripting languages available on Linux, the two hottest languages are Tcl and Perl, while gawk continues to attract a lot of attention.

Tcl

Tcl, short for the Tool Command Language, is a very handy scripting language that runs on most UNIX platforms and Windows NT. Combined with Tcl’s X Window toolkit, called Tk, you can build a lot of neat X Window graphical programs without a lot of coding.

In addition, Tcl is made to be embedded in C programs, so you can use Tcl as a standard extension language for your spreadsheet, game, or other software you write.

We mostly use Tcl to create programs that have a friendly user interface, that look like Motif programs, and that can run on a wide number of systems. Tcl and the Tk toolkit present something akin to the Motif look and feel—not close enough for purists, but close enough for most users. This is a great benefit because the Motif libraries don’t ship with Linux, but Tcl does.

Tcl is a scripting language, much like the languages built into sh and ksh, the most common UNIX command shells. The language has some nice features for handling strings and lists (of strings—just about everything is a string in a Tcl program).

The Tk toolkit then acts as an add-on to Tcl, allowing you to easily build widgets and create an X Window user interface. The whole concept of widgets, though, is likely to be daunting unless you’ve programmed with one of the many X toolkits, such as Motif. Each widget acts as a part of your user interface, for example, a list of files, a push button to exit the program, and so on. If you have worked with Motif or the Athena widgets, you’ll catch on to the concepts of Tk pretty fast. Even if you haven’t worked with the Motif or Athena libraries, we found the basics of Tcl very easy to grasp. (There are some frustrating parts to Tcl, though.)

The Tk add-on to Tcl provides most of the standard set of widgets you’d expect. These widgets mirror most of the main widgets in the Motif toolkit, except for the handy option-menu, combo-box, and notebook widgets. Tcl exceeds Motif in a number of areas, too, especially with the canvas widget, which allows you to place graphic “objects” such as lines, rectangles, Béziér curves, and even other widgets inside the canvas.

Scripting with Tcl

Like most scripting languages, Tcl uses a dollar sign, $, to get the value of a variable. Everything in Tcl is a text string, so it needs a special character to differentiate a string from the value held within a variable. Thus:


     variable

is just the literal string variable, while


     $variable

returns the value stored in the variable named, appropriately enough, variable. This is the same as most shell scripting languages. (There are some tricky aspects to this, though. We found that simple typos—such as forgetting the $—were responsible for most of our Tcl errors.)

For example, if you have a directory name in the variable dir and you want to use the cd command to change to that directory, you issue the following Tcl command:


     cd $dir

The basic syntax for Tcl seems like a cross between Lisp and C. The basic function, called proc, looks much like a C function, for example:


     proc add_one { value } {



         return [expr $value+1]

     }

The braces give it a definite C feeling. The Lispishness comes from the use of the set command, instead of assignment. That is, instead of a C statement like:


     a = b;

in Tcl you code this as:


     set a $b

(Remembering all the while that the $ can trip you up at first.)

One nice thing about Tcl is its ability to use variables at any time, without predeclaring them—except for arrays, which you need to indicate are arrays before using them with widget commands.


Previous Table of Contents Next