The Internet Encyclopedia (Volume 3)

(coco) #1

P1: JDW


UNIX WL040/Bidgoli-Vol III-Ch-41 August 13, 2003 17:26 Char Count= 0


CORECOMPONENTS 505

command “echo $USER” might print out “linda,” if the
user with the username linda happens to be currently
logged in. Other common default shell variables include
$HOME, which indicates the current user’s home direc-
tory within the file system. The command “cd $HOME”
will change the current directory to the user’s home direc-
tory, for instance. The variable $PWD stores the present
working directory, or the current directory the user hap-
pens to be in at the moment. The $PS1 variable contains
the prompt that the user can set. For example, the fol-
lowing Bash command will set the prompt to display the
host machine name followed by the current date and time
before the default dollar sign:

export $PS1="[$HOST][`date']$"

This would produce a prompt that might look like this:

[computer1.cs.uchicago.edu][ Sun Mar 30
17:45:54 2003]$

The introduction of the word ‘date’ in backward quotes
(often called “backticks”) introduces a nice capability of
some Unix shells: the notion ofcommand substitution.

Command Substitution
Command substitution exists to allow one command to be
included within, and executed before, another command.
This is a powerful feature. Let’s say that a user wanted to
edit all the files containing the word “Unix” in a particular
directory. He could obtain a list of the files with the word
Unix in them by issuing a grep command in the form of
“grep -l Unix *.” This command uses the “*” wildcard to
indicate “all files.” Grep is a command that will search
through a file or files and look for a particular regular
expression. The regular expression in our example simply
states that we are looking for the lettersU, n, i, x,in all
the files in the current directory. The -l flag will indicate to
the grep command that it should only print out thename
of the files that match. Let’s say that we had four files that
contained the word “Unix”: file1, file2, file3, and file4. If
we wanted to edit all of these files with the word “Unix”
in them, we could issue the command

vi `grep -l Unix *'

which the shell would automatically expand into the fol-
lowing command before being executed:

vi file1 file2 file3 file4

This would in effect launch vi with these four files, and the
user could do whatever edits he wanted to those selected
files, effectively editing all files in the current directory
that contained the word “Unix.”

Shell Scripts
Every Unix shell supports a shell programming language.
Shell programs are commonly called shell scripts. Shell
scripts are simply lists of commands that are executed by
the shell interpreter one right after another. Any command

that can be called from the shell prompt can be added to
a shell script and vice versa.
A shell script can call another shell script, so a small
program can be created of multiple coordinating scripts.
Some of the Bourne shell derivatives (e.g., Korn, Bash)
also support the concept of shell functions, which are
loaded once (usually at login) and generally execute more
quickly than shell scripts. Functions also allow the shell
programmer to give a more structured approach to shell
programming, where one shell script may call other pro-
cedures (scripts or functions).
Shell scripts can be fairly powerful tools, and they are
highly portable. Unlike binary programs, which must be
recompiled to run on different Unix systems, shell scripts,
because they’re written in that common Lingua Franca
known as text, can generally be run on any other Unix
system that provides the same shell.
The shell scripting support provides some default lan-
guage capabilities. I have already mentioned the ability
to create shell variables to store data. Shell scripting lan-
guages provide flow control syntax (if... then... else), in-
cluding for loops, while loops, and conditional clauses in
the form of if-then-else syntax. The shell scripting lan-
guage also includes certain string comparison operations,
numeric comparison operations, and some default type
conversion capabilities, along with the ability to walk
through a command line and operate on the various op-
tions (preceded by a dash in Unix) and command line
arguments (arguments passed in to the shell script at run-
time).

Job Control
In addition to incorporating a shell scripting language,
shell variables, and command substitution, most Unix
shells (with the exception of the Bourne shell) support
something that Bill Joy added to his original C Shell: job
control. Unix is a multitasking operating system, allowing
a user to run multiple commands at the same time, but
originally, if a user wanted to execute three commands si-
multaneously, he would have to open three terminal ses-
sions, log in three times, and then execute a command
in each of the three terminals. Bill Joy thought it would
be nice if within a single terminal session, multiple com-
mands could be executed, and be able to run in the back-
ground, while the user continued to work at the prompt.
Job control allows a user to (a) execute a job (process)
in the background, (b) move a background job to the fore-
ground and vice versa, (c) temporarily suspend a back-
ground or foreground job, (d) stop (permanently kill) a
background job, and finally (e) get a list of all jobs that are
currently running in the current shell. To start a program
in the background, the user simply follows the command
with an ampersand character (&). When a job is placed
in the background by the shell, the shell automatically as-
signs that job a “job number.” The user can subsequently
use the job number to reference the various jobs running
in the background. The user can get a list of all the various
jobs running within a single shell by executing the “jobs”
command. Figure 3 shows the jobs command output with
four jobs running: an emacs session, xclock, the user’s fin-
calc program, and the user’s vi session for a term paper. A
list of common job control commands is given in Table 3.
Free download pdf