Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [14]

By Root 814 0
useful, other people can make use of the program, treating it as a black box, a program that gets a job done, without their having to know how it does so.

In this chapter we'll make a brief comparison between different kinds of programming languages, and then get started writing some simple shell scripts.

Scripting Languages Versus Compiled Languages

Most medium and large-scale programs are written in a compiled language, such as Fortran, Ada, Pascal, C, C++, or Java. The programs are translated from their original source code into object code which is then executed directly by the computer's hardware.[1]

The benefit of compiled languages is that they're efficient. Their disadvantage is that they usually work at a low level, dealing with bytes, integers, floating-point numbers, and other machine-level kinds of objects. For example, it's difficult in C++ to say something simple like "copy all the files in this directory to that directory over there."

So-called scripting languages are usually interpreted. A regular compiled program, the interpreter , reads the program, translates it into an internal form, and then executes the program.[2]

* * *

[1] This statement is not quite true for Java, but it's close enough for discussion purposes.

[2] See http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?Ousterhout's+dichotomy for an attempt to formalize the distinction between compiled and interpreted language. This formalization is not universally agreed upon.

Why Use a Shell Script?

The advantage to scripting languages is that they often work at a higher level than compiled languages, being able to deal more easily with objects such as files and directories. The disadvantage is that they are often less efficient than compiled languages. Usually the tradeoff is worthwhile; it can take an hour to write a simple script that would take two days to code in C or C++, and usually the script will run fast enough that performance won't be a problem. Examples of scripting languages include awk, Perl, Python, Ruby, and the shell.

Because the shell is universal among Unix systems, and because the language is standardized by POSIX, shell scripts can be written once and, if written carefully, used across a range of systems. Thus, the reasons to use a shell script are:

Simplicity

The shell is a high-level language; you can express complex operations clearly and simply using it.

Portability

By using just POSIX-specified features, you have a good chance of being able to move your script, unchanged, to different kinds of systems.

Ease of development

You can often write a powerful, useful script in little time.

A Simple Script

Let's start with a simple script. Suppose that you'd like to know how many users are currently logged in. The who command tells you who is logged in:

$ who

george pts/2 Dec 31 16:39 (valley-forge.example.com)

betsy pts/3 Dec 27 11:07 (flags-r-us.example.com)

benjamin dtlocal Dec 27 17:55 (kites.example.com)

jhancock pts/5 Dec 27 17:55 (:32)

camus pts/6 Dec 31 16:22

tolstoy pts/14 Jan 2 06:42

On a large multiuser system, the listing can scroll off the screen before you can count all the users, and doing that every time is painful anyway. This is a perfect opportunity for automation. What's missing is a way to count the number of users. For that, we use the wc (word count) program, which counts lines, words, and characters. In this instance, we want wc -l, to count just lines:

$ who | wc -l

Count users

6

The | (pipe) symbol creates a pipeline between the two programs: who's output becomes wc's input. The result, printed by wc, is the number of users logged in.

The next step is to make this pipeline into a separate command. You do this by entering the commands into a regular file, and then making the file executable, with chmod, like so:

$ cat > nusers

Create the file, copy terminal input with cat

who | wc -l

Program text

^D

Ctrl-D is end-of-file

$ chmod +x nusers

Make it executable

$ ./nusers

Do a test run

6 Output is what we expect

This shows the typical

Return Main Page Previous Page Next Page

®Online Book Reader