Delving More Deeply Into Unix
The Unix
operating system is an extremely rich one, providing users
with countless functions and features. It is also an extremely
terse system, packing a great deal of computing power into
brief command line entries. Some, but only a relatively small
amount, of this power is demonstrated in the sections below.
Users curious to explore the full range of Unix possibilities
are invited to peruse the complete set of manuals maintained
by Office of Information
Technology .
Input And Output
By default,
the terminal is the source of input, called standard input
for many Unix commands. Similarly, output, called standard
output, is displayed at the terminal by default. Redirecting
input or output streams to a file or a device is easily accomplished
through use of the redirect characters < and >.
|
|
Writes
the listing of files in the current directory to a
file called listingfile. The greater than sign
is used to redirect standard output from the terminal
screen to the file designated.
|
|
|
Appends
the standard output from the command ls to the end
of an already existing file, listingfile.
|
|
|
Here
the standard input is redirected. The less than sign
followed by a file name sets that file as the input
source. Effectively what is happening here, then,
is that the contents of file numbers are copied into
a new file, data, which is then retrieved for editing
under the emacs program.
|
| The
cat command is particularly versatile in redirecting
standard input and output. Normally, cat takes
a file name argument as its input, but will use standard
input if no arguments are included with the command. And,
although cat usually writes to the terminal, an
output file name can be substituted. |
|
|
This
amounts to creating a new file called myfile. Since
no command argument is given, input can be entered
line by line at the terminal, and ended by <CTRL/d>.
The text is saved in the redirected output file.
|
|
|
|
| Redirection
of both input and output can be used in the same command.
Assume you have an executable program stored in a file
called proj1: |
|
|
Executes
the program proj1, taking its input from the
file mydata and writing its results to myoutput.
|
The Pipe
The pipe
is a data channel from one executing program to another. Put
another way, it is a means of connecting output from one program
to the input of another, thus accomplishing in one step what
could otherwise be done only in two or more steps. To illustrate:
|
|
|
|
|
|
| In
these examples, two commands are used to determine the
word count of the output from who. However, much more
economy is possible: |
|
|
The
output from who becomes input to wc; thus one operation
gives the number of users. The vertical bar | symbolizes
the pipe.
|
|
|
Counts
the files in your current directory by using the output
from the ls command as input to the word count command.
|
| In
these examples, two commands are used to determine the
word count of the output from who. However, much more
economy is possible: |
Managing Unix Jobs
Because
Unix does not include a batch processing facility, jobs cannot
be detached to run independently (although the nohup
command allows execution after you log out. It should be issued
with the ampersand as described below).
Unix does
allow you to have several processes running simultaneously.
You can control priority of execution by moving jobs into
and out of the foreground. The job in the foreground is the
job currently active on your workstation. All other jobs attached
to that workstation are running in the background.
A job can
be moved into the background immediately on startup by issuing
the command followed by an ampersand (&):
|
|
Starts
execution of a file called program, but allows immediate
input from the terminal for other purposes.
|
| If
a command is issued with the ampersand, the system responds
with the job number (shown in square brackets), as well
as the process number. You will also see a line of status
information about that job. Then the system prompt will
be displayed for your next command.
A
job started in the foreground can subsequently be moved
to the background for processing. First suspend the
process with <CTRL/ z>. Conversely, a job in the
background can be moved up to the foreground. |
|
|
|
|
|
|
| A
list of your commands running in the background can be
obtained by entering the command jobs. The output of jobs
will be a table of information about all background jobs:
|
|
|
|
|
|
|
|
|
|
| The
first column shows the job number. The second displays
a plus (+) for the last job placed in the background,
and a minus (-) for all other jobs. The third column contains
the status of the job. Some possible status messages include
running (which means the job is executing and making progress)
and stopped (which means the job is in a suspended state,
not active but not accumulating cpu time either). If you
have more than one job backgrounded and want to foreground
a job other than the last job placed into the background
(indicated by a plus in column 2), it will be necessary
to specify the job number, immediately preceeded by the
percent sign (%). |
|
|
|
| Another
command, ps, is available to report on all of your processes,
both foreground and background. |
|
|
Gives
information about your processes, including identification
number (PID), time and status of the process.
|
| Execution
of a command running in background can be halted in one
of three ways: You can move it to the foreground ( fg)
and stop it with the sequence <CTRL/c>; you can
kill the process by referring to the process number
shown; or you can kill the job by referring to
the job number shown. |
|
|
|
|
|
Kills
the rn job shown in the table above. Note that
when you kill by job number, that number must
be preceded by a percent sign.
|
Note: It
is important to remember that <CTRL/z> does not abort
a job, it simply suspends it in its current state. This means
that the job does not make any further process in its execution,
but it retains all of the resources that it had before being
suspended (memory, for example). Any running job that goes
into an infinite loop (or otherwise becomes a runaway job)
should be killed with < CTRL/c>. If this is not possible,
then use <CTRL/z> to suspend the job, thus giving you
control of the keyboard. The offending job can now be finished
off with the kill command.
Some Handy Unix Tools
Unix offers
many utilities for making your work easier, including a large
number which can analyze and/or change the contents of your
files. A few of these are described here.
Sort
Unix provides
an easy method of sorting the individual records (lines) in
a file.
|
|
Reorders
the records in the file phonelist alphabetically,
based on the first character of each line. Therefore,
if each line in the file consists of a first name,
a last name and a telephone number, the output will
be a screen listing sorted by first name. Of course,
output can also be redirected to a file.
|
|
|
Sorts
on the second field (+1), the first being designated
0. Thus the result will be a listing by last name,
not first. The individual records do not change, only
the order in which they appear.
|
Sort
offers a number of options and other permutations as well.
For more information, check the manpage for this command.
Grep
Grep
is an acronym for global regular expression printer. The utility
is used to search input files for lines matching a pattern.
|
|
Searches
the file phonelist for instances of "Kathy."
Any line containing this string will be printed to
standard output, which by default is the workstation
screen.
|
|
|
|
If your
search string contains more than one word, enclose it in quotation
marks to avoid confusing the processor. More than one file
can be searched simply by listing the file names on your command
line, separating each with a space. "Wildcard" searches
are also possible, as are searches for strings appearing only
at the beginning of a line, lines containing a certain range
of characters, and so on.
Ispell
The spell
utility reads an input file and prints a list of those words
not found in the system dictionary.
|