Get with the script

Here we take a look at the simple scripting tools that pre-date the Perl "Swiss Army chainsaw."

This Content Component encountered an error

Any IT professional worth his salt will eventually write a script to automate some tedious task. Everyone knows that the biggest, baddest scripting language on Windows is Visual Basic. On Linux, Perl probably has that title, although there are many other options. But Perl is a relatively recent invention in the world of UNIX/Linux. Here we take a look at the simple scripting tools that pre-date the Perl "Swiss Army chainsaw." They are...

useful in their own right and serve to explain why Perl is the Big Deal it appears to be today.

The most well-used early UNIX/Linux scripting language is, of course, the Bourne shell or /bin/sh, named after its inventor Steve Bourne. It's called a "shell" because in the simplest case it does nothing but "surround" the programs on the disk with an interface (a command line) that is used to run them as required. In theory the shell doesn't do anything itself, which makes it "thin" like the shell of a marine animal. In reality, the Bourne shell's features have grown to the point where you can write simple scripting programs that never cause another program to run. The Bourne shell is supported by a hack deep inside Linux: If the first two characters of a file are "#!", and the file has executable permission, then the kernel will pass the file contents to the program stated in the rest of the first line. That's why Bourne shell scripts start with:

#!/bin/sh

On Windows, COMMAND.EXE or CMD.EXE is the program most equivalent to /bin/sh, except those programs also contain a lot of operating system functionality. /bin/sh is just an ordinary program designed to interact with the user.

A Bourne shell script is like Windows .BAT file, but the Bourne shell is far more flexible. You can string together more than one pipe:

ls | rev | grep "oof" | more

You can use long relative paths:

cd ../../../tmp

You can use complex programming logic:

while read line do; cat $line; done

The Bourne shell is the inspiration for many very similar shells, such as ksh (Korn shell), bash (Born Again shell), ash (Another shell) and the weirdly different Csh (C shell) and tcsh (Tenex C shell).

Plain sh and bash are recommended. Some of the syntax of these shells creeps into Perl, especially variable names and backtick syntax, but mostly Perl and sh syntax are separate.

It's other small Linux scripting languages that contributed most to Perl, and which are still sometimes useful when automating server tasks, or as command line power toys.

The most primitive editor on Linux is called 'ed' (press Control-C to stop it), once called 'e', later called 'ex'. It's equivalent to the venerable EDLIN of DOS. There's a batch version of ed called 'sed' (stream-ed). Sed is a very simple scripting language that supports many of the simple things you might do at the : prompt of 'vi'. You can run log files or dumped warnings through a canned sed script and pick out the interesting bits.

More influential on Perl than sed is 'awk'. Named after the last initials of its three inventors, this is a simple combination of the regular expressions invented for the humble 'grep' tool ("Get Regular ExPression"), plus interpreted syntax close to the C language. These days, interpreted C is generally called JavaScript, although that language also has its own unique roots. Also, awk contains lots and lots of fiddly features that Sysadmins use regularly in Perl. It's a mini-Perl with uses limited to text file hacks -- a living fossil, just as sed is.

Together, these tools, small and large, present a range of options for expressing solutions that can't be done with a simple command invocation. Unlike Windows, on Linux you don't have to leap from BAT files all the way to a full blown programming language like Visual Basic or Perl. There are simple tricks in between worth trying.


Nigel McFarlane is an open source software analyst and technologist. He has an extensive programming background and degrees in computer science and physics. His latest book is Rapid Application Development with Mozilla from Prentice Hall PTR. Nigel also answers questions as the Web and software development expert for SearchEnterpriseLinux.com. Click here to browse answers or ask Nigel a question.
This was first published in July 2004

Dig deeper on Linux administration tools

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataCenter

SearchServerVirtualization

SearchCloudComputing

SearchEnterpriseDesktop

Close