[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Scripting help



Scott E. Graves wrote:
> If I had a list containing redundant entries, what command could I use 
> suppress the duplications. Here's an example list:
...

	       Shell scripts give you awesome leverage

Shell scripts consist of one or more statements that specify C
programs and other shell scripts to execute. They run these programs
indirectly by loading each command into memory and executing
it. Depending on the kind of statement, the top-level shell program
may or may not choose to wait for individual commands to complete. The
executed commands have been compiled from as many as a hundred, a
thousand, or even a hundred thousand lines of C source code, most of
which you did not write. Someone else took the time to code and debug
those programs. Your shell script is merely the beneficiary, and it
uses those lines of code to good advantage. Although you have expended
comparatively little effort on your part, you gain the benefit
of as much as a million or more lines of code. Now that's leverage.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

In the multilevel marketing of plastic housewares, the trick is to get
other people to do much of the work for you. You're trying to create a
situation where someone else sows and you reap part of the
reward. Shell scripts provide that opportunity. They give you the
chance to incorporate the efforts of others to meet your goals. You
don't write most of the code used in a shell script because someone
else has already done it for you.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

Let's look at an example. Suppose you wanted a command to list the
names of a system's users on a single line. To make it more
interesting, let's separate each user's name by commas and display
each name only once, no matter how many sessions a user may have
opened on the system. This is how our command might look as a shell
script written in the Bourne shell, a standard UNIX command
interpreter:

  echo `who | awk '{print $1}' | sort | uniq` | sed 's/ /,/g'
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

Although this shell script consists of a single line, it invokes six
different executables: echo, who, awk, sort, uniq, and sed. These
commands are run simultaneously in a kind of series-parallel
progression. Except for the who command, which starts the sequence,
each command receives its data from the previous command in the series
and sends its output to the next command in the series. Several UNIX
pipes, denoted by '|' characters, manage the data transfer. The final
command in the sequence, sed, sends its output to the user's terminal.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

Each command works together synergistically to produce the final
output. The who command produces a columnar list of the users on the
system. It feeds this to awk via the pipe mechanism. The first column
in the output from who contains the names of the users. The awk command
saves this column and throws away the rest of the data generated by
who. The list of users is then sent to sort, which places them in
alphabetical order. The uniq command discards any duplicate names
caused by users who may have logged in on several sessions at once.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

Now we have a sorted list of names, separated by "new-lines," the UNIX
end-of-line or line feed characters. This list is then sent to the
echo command via a "back quoting" mechanism that places the output of
the previous commands on the echo command line. The Bourne shell's
semantics here dictate that single spaces replace all
newlines. Finally, our string of user names separated by spaces is
sent to the sed command where the spaces are converted to commas.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

While this might seem quite amazing if you have never seen a UNIX
system before, it is a typical UNIX-style command execution. It is not
unusual to invoke multiple commands from a single command line in a
shell script.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

How much code was executed here? The shell script writer took less
than a minute to write the script. Others wrote the commands invoked
by the script. Under one version of UNIX widely available today, the
six commands used contain the following number of source code lines:
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

 echo     177
 who      755
 awk    3,412
 sort   2,613
 uniq     302
 sed    2,093
 ------------
 Total: 9,353

One line in this shell script executes the equivalent of 9,353 lines
of source code! Although not an extraordinary amount, this many lines
are enough to prove our point. We have increased our power by a factor
of 9,353 to one. Again, we have leverage.
                      --Mike Gancarz, THE UNIX PHILOSOPHY, ISBN 1-55558-123-4--

This was a simple example of a shell script. Some shell scripts today
span several dozen pages containing hundreds of command lines. When
you account for the C code behind each executable, the numbers really
start to add up. The resulting leverage boggles the mind. As we shall
see later, this phenomenon even impressed Albert Einstein.

-- 
 Robert Cymbala         http://www.LAfn.org/~cymbala/index.html
        Cymbala@LAfn.org  ...alternate email: rCymbala@yahoo.com



Reply to: