A long time ago, the average Unix systems manager didn't have to worry much about scripting languages other than the Bourne shell and awk . Shell scripts (discussed in Chapter 7) continue to be an important part of Unix, but as stated earlier, awk has faded somewhat from the scripting arena. However, many powerful successors have arrived, and many systems programs have actually switched from C to scripting languages (such as the sensible version of the whois program). Let's look at some scripting basics.
The very first thing that you need to know about any scripting language is that the first line of a script looks similar to a Bourne shell script. For example, a Perl script starts out like this:
In Unix, any executable text file that starts with #! is a script. The pathname following this prefix is the scripting language interpreter executable. When Unix tries to run an executable file that starts with #! , it runs the program following the #! , and then sends the script file to the new program's standard input. Therefore, even this is a script:
#!/bin/tail +2 This program prints this line... and this line, too.
The first line of a shell script often contains one of the most common basic script problems ” an invalid path to the scripting language interpreter. Let's say that you named the previous script myscript . Now, what if tail were actually in /usr/bin on your system (not /bin )? When you tried to run myscript , you would get this error:
myscript: No such file or directory
Most Unix error messages aren't confusing, misleading, or otherwise incomplete, but this one certainly qualifies on all three counts. First of all, myscript does exist ” you can see it right in front of your eyes with ls . Is there a problem with your filesystem? No. The system really meant to say that it couldn't find /bin/tail , but it just had a small communication breakdown.
The worst case occurs when a script is in some system binary directory. You can locate the script with a utility like which or find , and you think that the program is in your path, yet you get that error message. The only real clue you have is the error message: "No such file or directory." If the script were actually missing from your path, the error message would be "command not found."
Another script "gotcha" is that you shouldn't expect any more than one argument in the script's first line to work. That is, the +2 in the preceding example may work, but if you add another argument, the system might decide to treat the +2 and the new argument as one big argument, spaces and all. This can vary from system to system; don't test your patience on something as insignificant as this.
The most important third-party Unix scripting language today is Perl ” Larry Wall's Practical Extraction and Report Language. This tool has been called the "Swiss Army Chainsaw" of programming tools, and it is capable of an incredible variety of tasks , from networking to database access. It excels at text processing, conversion, and file manipulation.
It's hard to describe what Perl code looks like; it's kind of like an imperative-style mix of C, Bourne shell, sed , awk , LISP, and others. It can be a little hairy (using every single special character on your keyboard), but it is as fast and powerful as it is ugly. If you want to be on top of your Unix system, you need to know a little bit of this language. Have a look at Learning Perl [Schwartz] for a tutorial-style introduction; the larger reference is Programming Perl [Wall]. Getting a grasp of Perl is well worth it, not just because you will be able to do remarkable things with text files, but also because many of the concepts (such as associative arrays) carry over to other scripting languages.
Nearly all Unix and Linux systems in the world have Perl installed somewhere. Most administrators maintain it as /usr/bin/perl , even if they insist on keeping all other third-party software away from /usr . There is extensive online documentation in the manual pages; the perl(1) page has an index of the other pages.
If you're having trouble with a Perl script, here are some suggestions:
Look at it to see if you can tell when the script was written. It might require a newer version of Perl than you have.
If it's an old script, you may have to put backslashes in front of the @ characters in strings and comments.
It might require a module that you do not have (in particular, a database module).
When all else fails, run perl -w script to turn on warning mode.
Python is a newer scripting language with a fairly strong following, enough to make it the implementation language for packages such as GNU Mailman. It offers many of the same system features as Perl, but it has a powerful interactive mode and a somewhat more organized object model.
Python's executable is python , but there's no telling where it is on your system. Therefore, when you get a Python program, you may have to modify the first line in the script to match your Python installation.
Although it was written for slightly older versions, Python Essential Reference [Beazley] is a good reference with a small tutorial at the beginning to get you started.
There are some other scripting languages that you might encounter:
PHP This is a powerful hypertext processing language often found in dynamic Web scripts. Some people use PHP for stand-alone scripts. The PHP Web site is at http://www.php.net/.
Tcl Tcl (tool command language) is a simple scripting language usually associated with the Tk graphical user interface toolkit and Expect, an automation utility. Although Tcl does not enjoy the wide-spread use that it once did, you should not discount its power. Many veteran developers prefer Tcl, especially for its embedded capabilities. Refer to http://www.tcl.tk/ for more information on Tcl.
m4 This is a macro processing language.
Ruby Object-oriented fanatics enjoy programming in this language (http://www.ruby-lang.org/).
Matlab This is a commercial matrix and mathematical programming language and library. There is a very similar open source project called octave .
Mathematica, Maple These are more commercial mathematical programming languages with libraries.