15.4 Scripting Languages
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 11) continue to be an important part of Unix, but awk has faded somewhat from the scripting arena. However, many powerful successors have emerged, 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 first thing you need to know about any scripting language is that the first line of a script looks like the shebang of a Bourne shell script. For example, a Python script starts out like this:
#!/usr/bin/python
Or this version, which runs the first version of Python in the command path instead of always going to /usr/bin:
#!/usr/bin/env python
As you saw in Chapter 11, an executable text file that starts with a #! shebang 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 #! with the rest of the file as the standard input. Therefore, even this is a script:
#!/usr/bin/tail -2
This program won't print this line,
but it will print 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. For example, say you named the previous script myscript. What if tail were actually in /bin instead of /usr/bin on your system? In that case, running myscript would produce this error:
bash: ./myscript: /usr/bin/tail: bad interpreter: No such file or directory
Don’t expect more than one argument in the script’s first line to work. That is, the -2 in the preceding example might work, but if you add another argument, the system could 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 this insignificant.
Now, let’s look at a few of the languages out there.
15.4.1 Python
Python is a scripting language with a strong following and an array of powerful features, such as text processing, database access, networking, and multithreading. It has a powerful interactive mode and a very organized object model.
Python’s executable is python, and it’s usually in /usr/bin. However, Python isn’t used just from the command line for scripts. It’s found everywhere from data analysis to web applications. Python Distilled, by David M. Beazley (Addison-Wesley, 2021), is a great way to get started.
15.4.2 Perl
One of the older third-party Unix scripting languages is Perl. It’s the original “Swiss army chainsaw” of programming tools. Although Perl has lost a fair amount of ground to Python in recent years, it excels in particular at text processing, conversion, and file manipulation, and you may find many tools built with it. Learning Perl, 7th edition, by Randal L. Schwartz, brian d foy, and Tom Phoenix (O’Reilly, 2016) is a tutorial-style introduction; a larger reference is Modern Perl, 4th edition, by chromatic (Onyx Neon Press, 2016).
15.4.3 Other Scripting Languages
You might also encounter these scripting languages:
PHP This is a hypertext-processing language often found in dynamic web scripts. Some people use PHP for standalone scripts. The PHP website is at http://www.php.net/.
Ruby Object-oriented fanatics and many web developers enjoy programming in this language (http://www.ruby-lang.org/).
JavaScript This language is used inside web browsers primarily to manipulate dynamic content. Most experienced programmers shun it as a standalone scripting language due to its many flaws, but it’s nearly impossible to avoid when you’re doing web programming. In recent years, an implementation called Node.js has become more prevalent in server-side programming and scripting; its executable name is node.
Emacs Lisp This is a variety of the Lisp programming language used by the Emacs text editor.
MATLAB, Octave MATLAB is a commercial matrix and mathematical programming language and library. Octave is a very similar free software project.
R This is a popular free statistical analysis language. See http://www .r-project.org/ and The Art of R Programming by Norman Matloff (No Starch Press, 2011) for more information.
Mathematica This is another commercial mathematical programming language with libraries.
m4 This is a macro-processing language, usually found only in the GNU autotools.
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 widespread use that it once did, don’t discount its power. Many veteran developers prefer Tk, especially for its embedded capabilities. See http://www.tcl.tk/ for more.