Requiring no prior exposure to computers or to UNIX, this book explores the functionality of a widely-used version of UN
719 77 56MB
English Pages [580] Year 1995
NTRODUCT
Berkeley UNIX and ANSI C
JACK HODGES
STUDENT INFORMATION Name First
Middle
Last
Street
Apt.
CityZipcode
Account ID: Address
Telephone
Area Code
HOST INFORMATION Login Host Symbolic
Name
Login Host IP Address
Modem Numbers/Baud
Rates:
COURSE INFORMATION Laboratory Locations/Hours:
Laboratory Assistant Name:
_
Laboratory Assistant Userid:
Laboratory Assistant Office/Hours:
Instructor
Name:
Instructor Userid:
Instructor Office/Hours:
Instructor Phone:
'
PRENTICE HALL. Englewood Cliffs,
N.J.
07632 -
Library of Congress Cataloging-in-Publication Data
Hodges, Jack.
An
introduction to Berkeley
UNIX
and
ANSI C
/
Jack Hodges.
cm.
p.
Includes index.
ISBN 0-13-068495-3 1.
Berkeley UNIX.
2.
Operating systems (Computers)
(Computer program language) QA76.76.063H637 1995
I.
3.
C
Title.
94-24249
005.4'3--dc20
CIP
Acquisitions editor: Bill Zobrist
Production editor: Bayani Mendoza de Leon editor: Andrea Hammer Cover designer: Rich Dombrowski Buyer: Lori Bulwin
Copy
Editorial assistant: Phyllis
©
Morgan
1995 by Prentice-Hall,
Inc.
A Simon & Schuster Company Englewood Cliffs, New Jersey 07632 No part of this book may be any form or by any means, without permission in writing from the publisher. All rights reserved.
reproduced,
in
The author and publisher of
this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation
contained in this book. The author and publisher shall not be liable in
connection with, or arising out
of, the furnishing,
in any event for incidental or consequential damages performance, or use of these programs.
The use of trade names, trademarks, etc., even if not specially identified, should not be construed that such names are not protected by the Trade Marks and Merchandise Marks Act. Printed in the United States of
10
987654321
America
ISBN D-13-abflMTS-3
Prentice-Hall International
(UK) Limited, London Sydney
Prentice-Hall of Australia Ply. Limited,
Prentice-Hall
Canada
Inc.,
Toronto
Prentice-Hall Hispanoamericana, S.A., Prentice-Hall of India Private Limited,
Prentice-Hall of Japan, Inc.,
Simon
&
Mexico
New
Delhi
Tokyo
Schuster Asia Pie. Lid., Singapore
Edilora Preniice-Hall do Brasil, Ltda., Rio de Janeiro
as an indication
1
Preface
xi
Parti
UNIX
1
The Login Session 1
.
1.2
Introduction and Scope
3
Terminal and Console Login
4 6
1
.3
Logging on
1
.4
Changing Your Password
1.5
2
3
to a
Host
9
Logging Out
11
1
.6
Dialup Login and Terminal Servers
12
1
.7
Exercises and Sample Quiz Questions
16
Electronic
Communications
17
2.1
Introduction and Scope
17
2.2
Sending and Receiving Electronic Mail
19
2.3
Using the
UNIX
mail Utility
23
III
IV
3
4
5
Table of Contents
2.4
MH — The RAND Mail Handler
35
2.5
Problems Replying
40
2.6
Electronic Talk
2.7
Electronic
2.8
Exercises and Sample Quiz Questions
The UNIX
File
File
to
Email
42
News
45
System
53
57
3.1
Introduction and Scope
3.2
File
3.3
UNIX Commands
61
3.4
Directories and Directory Manipulation
67
3.5
Files
3.6
Filesystem Security
93
3.7
Exercises and Sample Quiz Questions
99
57
System Components
58
and File Manipulation
Creation and Editing
81
101
4.1
Introduction and Scope
101
4.2
File Editing
102
4.3
The
102
4.4
The emacs Editor
4.5
Text Alteration: emacs
4.6
Exercises and Sample Quiz Questions
130
4.7
Recommended Reading
131
vi
Full-Screen Editor
The UNIX Command
Shell
1
Command Mode
and Process Control
15
123
133
5.1
Introduction and Scope
133
5.2
The Login
134
5.3
Shell Control
150
5.4
Command
163
5.5
Linking Files and Directories
Shell Scripts
Manipulation
168
1
6
7
File
5.6
Exercises and Sample Quiz Questions
170
5.7
Recommended Reading
172
Formatting and Printing
173
6.1
Introduction and Scope
173
6.2
Printing Etiquette
174
6.3
Printing Tools
175
6.4
Printing a
6.5
Laser Printing in Draft
6.6
Text Formatters
6.7
nroff and troff
187
6.8
Online Documentation using nroff
198
6.9
LATEX
203
6.10
Exercises and Sample Quiz Questions
222
6.
Recommended Reading
225
1
Document
Mode versus Word
179 1
Processors
Network Processing
82
185
227
7.1
Introduction and Scope
227
7.2
Shared Resources and Network Etiquette
228
7.3
Single-User Status
229
7.4
Network-User Status
234
7.5
Network
239
7.6
Remote Processing
7.7
File
7.8
Exercises and Sample Quiz Questions
261
7.9
Recommended Reading
262
Status
System Management
241
251
1
VI
Table of Contents
Part
II
Programming Languages and ANSI C
8
9
Introduction to
C Programming
8.1
Introduction and Scope
265
8.2
Programming Languages
266
8.3
The C Programming Language
267
8.4
C
288
8.5
Variable Type Conversion
294
8.6
Program Layout and Organization
295
8.7
Style for
8.8
Exercises and Sample Quiz Questions
Types, Operators, and Expressions
C Program
Layout
Decision Making, Iteration, and Functions
10
265
295
297
299
9.
Introduction and Scope
299
9.2
Making Decisions
300
9.3
Iteration
311
9.4
Functions
318
9.5
Exercises and Sample Quiz Questions
339
Arrays, Structures, and Files
341
10.1
Introduction and Scope
341
10.2
Comparing
342
10.3
Array Variables
10.4
Structured Variables for Unlike Data Types
351
10.5
Exercises and Sample Quiz Questions
360
Iterations
Using Arrays
in File
Operations
347
1
VII
11
Programming Tools 1
1
1
1.2
.
11.3 1 1
.4
11.5 1
1.6
11.7
363
Introduction and Scope
363
Debugging a C Program
364
Program Debugging
370
Debuggers
380
Developing Program Libraries
385
Organizing and Maintaining Complex Programs
387
Recommended Reading
395
Part
UNIX
12
13
Shell
III
Programming
Regular Expressions and Scripts
C
399
12.1
Introduction and Scope
399
12.2
Regular Expressions
400
12.3
egrep
408
12.4
awk
409
12.5
sed
417
12.6
Exercises and Sample Quiz Questions
423
425
Shell Scripts
13.1
Introduction and Scope
425
13.2
Shell Script Execution
426
Shell Script Variables
427
13.6
C C C C C
13.7
Exercises and Sample Quiz Questions
13.3
13.4 13.5
Arguments
429
Shell Script Creation and Execution
431
Shell Script Control Structures
435
Shell Script
446
VI
I
Table of Contents
14
Bourne
449
Shell Scripts
14.1
Introduction and Scope
449
14.2
Invoking a Bourne Shell Script
450
14.3
Bourne Shell Variables
451
14.4
Bourne Shell Arguments
452
14.5
Bourne Shell Creation
455
14.6
Bourne Shell Control Structures
457
14.7
Exercises and Sample Quiz Questions
466
14.8
Recommended Reading
468
Appendices
A
Answers
to
Chapter
Sample Quiz Questions 1
- The Login Session
470
UNIX
472
File
System
Chapter 4 - File Editing Chapter 5 - The
Command
473 Shell
474
Chapter 6 - File Formatting and Printing
476
Chapter 7 - Networking
479
Chapter 8 Chapter Chapter Chapter Chapter
C Programming 9 - C Programming II 10 - C Programming III 12 - UNIX Programming Tools 13 - C Shell Programming I
Chapter 14 - Bourne Shell Programming
UNIX
469
Chapter 2 - Electronic Communications Chapter 3 - The
B
469
File
System
481
483
486
490 491
493
495
4 7
IX
c D E
Generic Login Session
Bare Bones UNIX
A Generic
Commands
Utility
496
497
499
mail as a Generic Utility
500
G
„ile
502
H
Execution Path
I
File
J
ASCII Characters
507
K
C Functions
508
L
L A T E X Templates
513
v
:
Editing as a Generic Utility
and Directory Permissions
L
.
I
L.2 L.3
L.4 L.5
The L A T E X Resume Template
505
506
5
1
E X Command File for Articles Executing latex on a Command File
5
1
A L T E X Sample Article or Research Paper A L A T E X Letter Command File
520
A
L AT
519
521
Table of Contents
L.8
L T E X Bibliography Examples L T E X Index Examples Other Features of T E X
L.9
Comparison
L.6 L.7
Index
to
HTML
523
526 527 528
535
Welcome ly
to
Berkeley
used version of
UNIX. The book
is
UNIX!
UNIX
This book
is
intended to introduce you to a wide-
called Berkeley
intended to teach you
System Distribution, or Berkeley to understand, use, and appre-
how
UNIX operating system. You any prior exposure to computers or to UNIX to succeed at learn-
ciate operating systems, and, in particular, the
need not have
The intention is that you gain sufficient facility with the mayou can extend your knowledge through experience on your own. Although this material has been left to the student to learn on his or her own in the past, we have found that some guidance is extremely helpful in showing ing this material.
terial that
you what can be done with the UNIX operating system and how it can benefit you when using any computer. At our institution, we run this as a self-paced laboratory course and recommend the adoption of this format elsewhere. This book
is
intended to accompany a one-semester introductory course in
learning about and using
UNIX
and
ANSI
C.
The
UNIX
large and diverse, so students are not expected to master
operating system it
in
is
one semester.
However, they are expected to learn the fundamentals of the operating system, as defined by the chapter headings in this book. They are also expected to use the operating system. Reading alone simply doesn't work. The students' knowledge of its use will probably be examined at a fairly shallow level, but
show expertise at that level. The book makes ommendations about how much depth should be covered in any particular they should be expected to
ic
rec-
top-
for this course.
Students are also being introduced to a powerful programming language.
They are not expected to master C, but are expected to learn the fundamentals of programming and to demonstrate the ability to program. This will include simple but correct use of syntax, programming style, debugging, and logic in simple program writing. As students read about the language and write their
XI
XII
Preface
programs, they should be encouraged
first
quisition of
new programming
C
about the similarities
to think
might share with other languages, because these
similarities will
make
the ac-
skills easier later.
To the Student There are four basic components
to
any computer:
( 1 )
hardware, (2) the oper-
The operating system tells the hardexecute which program on what data. A program is a
ating system, (3) programs, and (4) data.
ware how and when
to
series of instructions that manipulate data according to the semantics of a pro-
gramming language. Technically, an operating system
is
program or a
a
set
of
programs. Together, the operating system, programs, and data make up what is
called software. This
book
is
intended to teach you
ulate operating systems, programs,
and the interactions
that an operating
computer hardware,
and
data.
how
to use
and manip-
The elements of programming,
system mediates between the user and the
you learn
are generic. Thus, the concepts
book
in this
apply to any hardware platform, operating system, language, or data
The prevalence of the guage are widespread
UNIX
used today are
in
machines
that
version of
System V®,
you have access
to
System Distribution
XENIX®,
-
BSD.
and AIX®). Most of the
probably are or soon will be running some
UNIX.
BSD
dialect of the
UNIX
operating sys-
UNIX dialect in which many of the examples in this book were tested
tem. The
called Ultrix,
Different
which
UNIX
is
installed at this institution
dialects, particularly
command sets, utilities, and introduce UNIX, and to provide
intended to
on
DEC® VAX®
a
UNIX
Berkeley
slightly different is
Many operating systems being
derivatives (e.g.. Berkeley
This course introduces you to the
is
file.
UNIX operating system and the C programming lan-
academia and industry.
MACH®, A/UX®,
Ultrix®,
will
6420.
and System V. have
associated mind-sets. a comprehensive
set
The book
of tools for
UNIX. The examples in this book have been executed on severBSD-compatible UNIX hardware platforms, or hosts, (i.e., a computer that has a BSD-compatible operating system), so they should work for you.
those learning al
The C programming language
UNIX
is
implemented
that interact with the
UNIX
and they are often more This book tem.
(II)
the
is
is
widely used on
in (i.e.. written in) C.
Thus,
UNIX
it is
operating system and hardware
efficient than
programs written
in
topically divided into three parts: (I) the
C programming
language, and
(III)
UNIX
the following sections, the suggested organization
platforms because
easier to write programs
when
written in C.
other languages.
UNIX
shell
operating sys-
programming.
In
and administration of a
course that uses this book as a primary text or supplement will be presented.
HELP!
HELP!
— Related Materials
XII
— Related Materials There are
at least six
course on
UNIX
sources of information for the content of an introductory
and C.
•
This book and the sample quiz questions provided with each chapter.
•
UNIX
A
reference texts:
of references can be found
list
at the
end of selected
chapters in this book. •
A UNIX
•
C programming
examples worked out
director) that includes
in this text
and may be
helpful for experimentation and sample exercises.
A list of references can be found at the end of
reference texts:
selected chapters in this book.
UNIX
online manual pages.
•
The
•
Newsgroups:
We
have found
paced") to be a valuable tool
that a dedicated
in
newsgroup
(that
we
call "self-
disseminating information to the students tak-
ing this course on this campus. Because of the nature of such courses, a news-
group
is
Course
•
a valuable asset.
staff:
Uaboratory assistants and instructor. The information about
to reach these
people
is
how
generally furnished early in a course, preferably the
day.
first
The Book Each chapter
This book
is
concept
introduced, references are cited (e.g.. text page or
is
written in a modular format.
self contained:
is
man
A
page), tuto-
examples of how
rial
to use the concept are presented and discussed, and samknowledge of the material are given. Answers to sample provided in Appendix A. There is an index in the back of the
ple questions to test
questions are
book
How
for finding terms too specific for the table of contents.
to
Read This Book
Each chapter
in this
book
is
broken into sections. The
first
section always in-
troduces the topic and the scope of the topic on which the student should focus.
The
sections following introduce, discuss, and illustrate the facets of UNIX. C.
and so forth associated with the be
illustrative,
topic.
The examples provided
should be expected to experiment with the proficient at the level described will
be a table of related
the topic scope, and
commands
to all of the
is
utilities to
become
or
command options associated with the minimum expertise described in
exceeds the
presented to allow interested students to experiment
the topic without the explicit
section in a chapter
Answers
is
commands and
by the scope. At the end of many sections there
topic. This reference table often
more with
are intended to
and are not exhaustive of the scope of the chapter. Students
always the
sample
test
set
need for a
UNIX
reference.
of exercises and sample
problems can be found
in
test
The
last
problems.
Appendix A.
XIV
Preface
When
reading this book, references to other topics and locations will ap-
pear as (Chapter nn, Section nn, and page tion
number and
mm), where nn
is
the chapter or sec-
mm is a page number or range. Text references will appear in
[author year] format. Online manual references will appear as
where "com"
a
is
UNIX command
(man
com)
name.
Book Nomenclature Learning
UNIX
a screen.
To understand
is
best done at a keyboard, with the responses
book, you will need to understand what ferent ways.
Below you
you
UNIX
will see,
staring
and
we mean when we
to
this
at
you on
understand
this
display items in dif-
will find a listing of the terminology
nomenclature as they are found throughout
back
and the associated
book.
Book Nomenclature Explanation
Item terms
Bolded items
variable
Bolded
command
Italicized courier commands
command
Courier commands
prompt
Courier
response
Bolded courier fonts
23
are first-time definitions.
variable
names
in text.
are later
are
command
UNIX command
references.
system prompts. are user responses.
on which the with an example.
Italicized index entries refer to pages
or
first illustrated
UNIX command is defined
Courier
control characters like
Courier
carriage returns/enter key/return key.
Courier
escape key.
"file"
"Double quoted" items
host
'Single command All host
.
are files or directories. If taken
courier font
these will be in
'command'
definitions.
from an example,
with straight quotes.
quoted' courier commands
items are
commands
or
options in text.
names
will
be
Control characters are
in
courier font.
command sequences that means
keys simultaneously. For example, and, while holding
it
down, press the
'd' key.
use multiple keyboard press the "control" key.
HELP!
— Related Materials
XV
Book Creation A This book has been formatted with the use of L T E
X® and FrameMaker® on
A
T E X makes use of ASCII files and can be transmitted easily from one machine to another. I have made use of many different several platforms. Because L
computers (HP®,
SUN®, Apple®, and NeXT®)
sions of the book.
The book was
sty" documentstyle, called "module. sty."
command
into a
as described
file,
to write chapters for early ver-
originally constructed with a modified "book.-
in
Each of
Chapter
6.
the chapters
was included
Figures have been generated
from several sources. Most of the figures have been created on a Macintosh® and saved in PostScript form. I wrote a T E X® macro that placed a figure and figure caption into the document. This macro is described in Appendix L. On
NeXT. I can even grab portions of the screen, newer graphics have been generated on a NeXT. the
H
-
mil.ps
— /UsersAwdges/Doctinents/aasses/uy
Modo
Unix
Fie
Preface.tex
as
shown below. Many of my
•zzz:
/Doc
Control characters are command sequences which U3e multiple keyboard keys simultaneously For example, \em Cntl-dj means press the ''control'' key. and, (\em while holding it down) press the *d" key
Edit
{
*
r
r
r
\subsubsectian(Manual Creation)
•
.'
T
::
Sj
'.
i
,)i
.
manual ha een formatted entirely with the U3e of \LaTeX on a number latforms Bee ause \LaTeX makes use of ASCII files and can be transmitted achine to another. I have made use of many different ly from oj (HP. Su n. Apple Macintosh, and HeXT> to write modules for the uters al The manua s constructed with a modified 'book sty. called dule st ach of the modules is included into a command file, as Figures have been generated from a ribed in Modu .e-\ref (printing) er of sources Most of the figures have been created on a Macintosh saved in \sc PostScript) form I have written a \TeX macro which es the figur and figure caption into the document This macro is ribed in App ndix-\ref (AppC) On the (\sc NeXT). I can even grab ions of the creen. as shown on the next page, and included as I have below Many of my newer graphics have been generated on the NeXT
!
SeMcei
HMe
'
'
SO550
'
'
•
{
|
\begln(figure) [htbp| \macputhv(next eps)(28H3 00} (55) (-225) \vspace{0 05m) al preface ^ca.. in preparation ((The \figcap (next) (The \sc NeXT) and this icmai (\sc NeXT) and this manual preface in preparation \end figure)
E3
m
(
)
(
Jt-j*J.
tl.
II
itt *
»
nttai^hjkt
AhJ.jnJ.
h
ne.1
f
i
lit-:
¥ith the advent of YTeX versions running on the (\sc Macintosh) and (\sc NeXT), previewing the compiled manual and dropping figures into the files directly has made development of the manual much less painful than it was just three years ago On the \sc NeXT). an object-oriented \LaTeX development application allows the user to select environments Ouch as figure or enumer ated lists) of the document from a pallete
'
n,
then your
If
you want
to log
on
to a host
named
response will look as follows:
telnet localhost where "telnet" stands stands for the
name of the
for the
command
to connect,
and
"localhost"
host computer to which you wish to connect.
1. This discussion assumes that you have a choice of computers onto which you might log on. If you do not have a choice, then the first thing you may see is the login: prompt. If so. skip ahead to the next section. This type of prompt will likely be different at your institution.
.
O
Chapter
1 .3
Logging on to a Host
1
The Login Session
A
log-on procedure will always look the same and consists of two steps. The
host computer will prompt you for two items, one at a time: (1) your account
ID
(also
user ID, login ID. or userid) and
usually called the userid,
You must have to.
is
what the system expects
Each person using
the
computer
is
:
"
prompt.
down your account name,
called a user, and
any place
or userid. Write
that is likely to
users have a userid
all
A space has been provided, below, for you Do
or on the inside front cover of this book. here, or in
"login
at the
a valid userid and password on the computer you try to log on
that is associated with their account. to write
your password. The account ID.
(2)
it
when you receive it down your password
here
not write
be associated with your course.
Account Name:
A
sample login sequence
a host
at
shown, below, for a login session on sf suvaxl,
is
San Francisco State University.
indicating that this session
suvaxl
is
initiated
at a
terminal server prompt,
from a modem.
When I select the sfmy session to that com-
begins
It
as the desired host, the terminal server routes
puter and begins the login sequence.
sequence
called a login
is
prompt
The
thing you will see in the login
first
(login:). In this session.
I
have responded to
the "login: " request with jhodges. which is my userid on the host sf suvaxl A user' s userid is the same as their account name, and the name of their home directory. Thus, the user jhodges is the owner of the directory "jhodg.
es."
More on
this in
Chapter
3.
sfsu-annex 25: telnet sf suvaxl Trying.
.
Connected to sf suvaxl. Escape character is * A '
'
.
login: jhodges Password:
When
I
type
jhodges
at the login
ing that the system displays
my
prompt,
response as
I
my
type.
response
type will be echoed on the monitor; however, the password
you enter your prompt you will not see
for
userid, enter a carriage return
your password. Your response
your password. This
password, an encryption scheme ciated with the userid
is
it
echoed, mean-
is
that
you
not echoed. After
(), and the system will shown, so that others
will not be
a security measure.
translates
is
Most commands
Once you
and compares
it
enter your
to the
one asso-
you entered. If they match, as mine did, then you are at which point a number of things will happen, as
logged on to the machine,
I
.
Logging on to a Host
1.3
will explain next.
Below
is
I
remainder of the sample session started above:
sfsu-annex 25: telnet sfsuvaxl Trying Connected to sfsuvaxl. Escape character is Ayv .
.
'
ULTRIX V4.3
(Rev.
44)
'
.
(sfsuvaxl.sfsu.edu)
login: jhodges Password: Last login: Wed Dec 29 17:36:03 from huckleberry sfsu ULTRIX V4.3 (Rev. 44) System #32: Tue Aug 31 11:01:06 PDT 1993
(1)
.
(2)
****************************************************************************** *
* * *
for system downtime information and
Type 'news' (updated 10/20/93) for other information.
* *
*
* * *
Send your VAX questions or problem reports electronically to the email address "[email protected]".
* * *
*
****************************************************************************** Wed Jan 5 09:21:27 PST 1994 sf suvaxl> In the session above, the first line after the
from where "jhodges" at the
same
institution.
last
logged on
This means that
(at 1). I
password entry
tells
huckleberry is
when and
another host
logged on through the network. The
about the operating system version and the date. Note the statement starting "For Downtime Information. .." This block of text next line (2)
is
called a
tells
message of the day (motd). The
often send messages to
all
the users this
staff that administers a host will
way, so keep an eye on them. The
last
prompt, which signals that the login procedure has completed and I am now able to issue UNIX commands. Because you have now seen three different kinds of prompts, let's look more closely at what they mean and what you will see in the remainder of this manline
of the login procedure
is
the shell
ual.
1.3.1
The
Computer Prompts
easiest
way
application, or
to
inform the user where they are
what they should type
is
to
in the file
system, or in what
provide a visual cue.
A prompt is
a
Each utility on the computer has its own type of prompt. By recognizing the prompt displayed on the terminal screen, you can tell what utility you are in and act appropriately. So far you have seen a connect prompt, a login prompt, a password prompt, and a shell symbol(s) which performs
this function.
]
:
8
Chapter
1
The Login Session
Some
prompt.
of the different prompts that you will see are shown below.
important to remember that each prompt actions
Table
1.1:
you can
is
It is
associated with a specific range of
take.
Computer prompts
>
connect to a host
login
log on to a host
for userid
password:
log on to a
for
sfsu-annex 25:
a specialty prompt, this
&
UNIX
C-Kermit
kermit communications package
ftp>
file
$
default
Bourne
shell
Q. "8
default
C
prompt
csh>
another
localhost>
a customized is
localhost
[3
Note
— request host — request
password
one for a terminal server
mail
transfer protocol
the
shell
common C
C
prompt
shell
shell
prompt
localhost
prompt, where
name of a computer
another customized
C
shell
prompt
that the last four prompts, above, all represent different
C
shell
—
C shell prompt is the first one (i.e., the percent sign %), which is probably what many new users will receive by default. In this manual, however, examples will appear with the last two C shell prompts. There are two reasons for this. First, when you begin to log on to many hosts, it is good prompts. The stock
to
keep track of which one you are logged on
to,
and the name of the host
is
"localhost" is a placeholder for any hostname, such as huckleberry or sf suvaxl. Second, when you begin to repeat the use of commands, it is easier to do so if you remember the command sequence, and is a component of the last prompt. You will learn the sequence number 3 how to customize your shell prompt in Chapter 5. part of these prompts,
(
[
]
)
you should issue at least two commands: (l) whoami, to find out who the computer thinks you are; and (2) passwd, to change your password. Your identity on the computer can be Before ending your
checked
at
first
login session,
any time with a simple
command
called
whoami. When
issued, as
"
Changing Your Password
1.4
below, the system will respond with your user ID:
localhost> whoami jhodges
You can
command,
issue a similar
computer thinks you respond with a
are. Specifically,
to find out a little
by typing
more about who
who am
i the
computer
the will
line that looks as follows:
localhost> who am i sfsuvaxl sfsu.edu! jhodges ttyqc Jul 18 13:35
(modeml3.sfsu.edu)
.
The whoami command tells you what your userid is. This could be imporyou are wondering, later, about file or directory ownership. The 'who am i' command tells you your mail path, what device you logged on from, when you logged on. and from what port you logged on from. This can be imtant if
portant in sending mail, or for a variety of other purposes.
1.4
Changing Your Password Once logged on first is
you
to the host,
called the
will learn
.
how
cshrc to
two startup files are executed immediately. The and the second is called the login file. Later
file,
.
modify these
files to
For now, simply know that they exist called is
'"dot** files.
created and
can be
set.
and
is
you
first
because is in
in.
.
cshrc
file is
executed when your
set
.
the
first
its
commands you can you
you
is
the
created for you.
It is
issue are associated with
will issue
will see
is
commands
it
to the system.
shell
characteristics
session,
your mail path, your home directory, and other things.
the login shell that
the first thing
command
login file is executed once when you begin a login
log on to a host, a login shell all
customize your shell environment.
your home directory, and that they are
usually used to identify your terminal so that
is
Your
used to
The
in
When
called a shell specifically.
When you
prompt associated with your login
It
log
shell:
localhost>
My
localhost>. tells me what host I am logged on to. In general we will use the localhost> prompt in this manual, so that any host name will work. You can write down the name of your login host in the space prompt.
provided below and
in the
space provided on the inside cover.
1
Chapter
1
The Login Session
Local Host Name:
When you
first time, you will want to change your password you by your instructor to something unique that you will be able to remember. The reason for account passwords is to guarantee the security and privacy of your account, and you should take it seriously. You would probably not feel good if someone broke into your account and destroyed your work, or worse. Here are some guidelines for selecting passwords, all of which
from
are
log in for the
that given to
common •
sense rules to guarantee security of your account:
Never use your
when name •
Do
first
or last name: These are the
first
things
someone
will try
trying to break into your account. For that matter, avoid using any
unless you hide
it
with special characters.
not use popular or recognizable words unless modified with special char-
acters. •
•
Do
not use simple alphanumeric sequences like "aaaa."
The use of special characters and capitals is encouraged, especially when they are embedded within words that your recognize and remember. When you use numbers and special characters, place them in nonobvious locations. Avoid using numbers like zero instead of O, or 1 instead of /. These are also fairly easy to break.
•
Use many
characters: Shorter passwords are easier to break.
Many system
administrators are going to longer (8) character password lengths, so take
note of the length of the password you are initially given, because ally coincide with the
minimum
it
will usu-
length set by the system administrator.
you are generally free to select You and you alone will know your password. If you forget your password, then you will most likely not be able to access your account for some time, because you will have to inform the Other than these types of general
your
own
password.
system administrator,
Remember
this,
rules,
though:
who may then require the instructor to verify your existnew password. Going through these steps, in both di-
ence before issuing you a
rections, can take time, certainly
word
in the
first
place.
Do
more than
the effort to
remember
the pass-
not forget your password.
Only two people can change your password: someone who knows the password and someone who generates computer accounts. The system administrator generates the accounts, so he or she can modify a password. The only
who should know your password is you, which helps to guarantee no one gets into your account and causes problems. To help guarantee against accidents and snoopers, it is a good policy to change your password regularly. Many system administrators are starting to expire passwords on a
other person that
regular schedule. If you should log in and see a message like
"your
ac-
1.5
count will expire in password is
days," don't be alarmed;
6
just
As for changing your password, command, passwd:
in the allotted time.
issue a single
Logging Out
all
change your
you need do
localhost> passwd Changing password for jhodges Old password: Enter new password: Password is not different enough, unchanged. localhost> passwd Changing password for jhodges Old password: Enter new password: Verify: localhost> At
this point, the
the one
which
you
system will prompt you for the old password, which
originally logged in with,
will be echoed.
When you
is
and then the new password, neither of
have typed the carriage return
after the
new
new password by typing it a you have changed your mind, or if you enter the wrong password, then the system will act as though you never issued the passwd command at all. If you have entered the same new password, then your password is changed, and the next time you log in you should enter the new password. In the first case, above, I did not change the password enough, so nothing was
password, the system will ask you to verify the
second time.
If
done.
1.5
Logging Out When you want to terminate a login session, you to do is issue the logout command:
localhost> logout session 1 - disconnected from sfsuvaxl Connection closed by foreign host.
(
need
to log out. All
you need
130.212.010.102 )...
You can also use the exi t command to log out, particularly from remote You will now be logged off the system. If you were at a terminal or modem, then a new ">" prompt, signaling that you are again communicating with shells.
the terminal server,
would appear.
.
I
2
1.6
Chapter
1
The Login Session
Dialup Login and Terminal Servers Many
modem
people use a
connect to a computer/network. Although there
to
many advantages for logging in remotely, from home or from a business. For example, when you are home you can get up and get coffee or take a break quite easily. You do not have to come when the building or lab is open, you do not have to wait for a are clear advantages to logging in locally, there are as
on a terminal, you can probably choose the type of chair that you sit in, you can stay on as long as you can afford to. and you can (if you have a personal computer rather than a terminal) toggle what you are doing with other turn
processing tasks. These are compelling reasons for remote login that often out-
weigh the performance advantages of
you must
login,
direct login. If
you use
this
method of
your local computer with a modem and a commuknow the dialup phone numbers where you will be logcommand (if any) associated with the local terminal serv-
(1) prepare
nications package. (2)
ging
in. (3)
er. (4)
exit
know
the
connect to and log on to a host, and (5) log out from the computer and
from the terminal server. A terminal server is a special piece of hardware bank of serial lines (e.g.. from computers or modems) to a com-
that connects a
puter.
to a
to
server
many modems, and to computers and terminals.
computer and get
other computer
it is
Getting
1.6.1
A terminal
a switching station or multiplexer.
It is
connected
a terminal server prompt,
connected
to
Your Modem
is
generally
you can log on
If
you can probably log on
to
any
on which you have an account. to
Work
is not set up to help you get your modem or communications software work. Because of the plethora of modems and communications packages available, not to mention the variety of computing platforms on which they can
This manual
you in debugging your remote from a friend who has similar hardware and software, or from the business where you obtained the modem or communications software. The most common problem with getting mo-
be used, most institutions will be unable dialin.
For that you will have
dems
to
baud
rate
work
is
not having the terminal characteristics set properly, having a
mismatch, or having the communications protocol mixed up.
of these problems 1.6.2
to assist
to seek assistance
Some
are addressed in Chapter 5.
Access Numbers and Connecting to a Network
The session below puter and a
is
typical for a remote (dialup) login using a personal
modem. Once your
phone numbers available
at
instructor tells you. write
your institution below
down
com-
the dialup tele-
1.6 Dialup
baud number
(s)
2400 baud number
(s)
48 00 baud number 96 00 baud number 14400 baud number
(s)
12 00
Login and Terminal Servers
13
(s)
(s)
You can
generally create a telephone directory to store these numbers with
your communications package, and you should also write down the numbers
computer on the inside flap of this manual, as well as the characterisyou find to work for your communications package and the terminal type that works best.
for your tics
have
kermit
communications package, then you
If
you
to
precede the telephone number you use with a
are using
attention (at)
and
dial
as a
will
Hayes® command to get command will work
(dt) the telephone number. This
with any Hayes compatible
modem:
atdt3382400
The command above consists of an attention command, a dial telephone number command, a telephone number, and a carriage return, kermit and other communications packages will automatically store a telephone number list so that you do not have to remember the Hayes command set, but it is instructive to know a few commands in case something goes awry. For example, it is good to know the command for hanging up (athO) or for testing the line (athl ). Generally, a modem will come with a complete listing of its command set, and it is at least instructive to look it over. When your modem makes contact with the host location, the first response you will discern is an audible squawk (if you have a speaker turned on), and then you will see a the "CONNECT" message, such as the one shown subsequently. After you see the CONNECT response, wait a moment, and, if nothing happens, type a carriage return. The remaining portion of the message will then appear.
.
1
4
Chapter
1
The Login Session
CONNECT 2400 /REL
••••A***********************
•
* Welcome to SFSU FOGNet ***************************** *
*
!
To connect to any host,
type "telnet "
*
* *
*
To quit,
type "hangup",
"bye",
*
or "quit"
* *
*
type "?"
To get help,
*
* * *
*
To break a session, press "Shif t-Ctrl-6" and then, type "kill"
*
*
* *
*
To report modem problem,
call 415-338-1211
* *
*
*
Happy Computing!
*
*
*
********************************************************** sfsu- annex 25:
This
is
a sample
welcome message from
the network
communications
Your welcome message will probably differ, but will likely be similar. After the "Welcome to ..." line you will see a message. This is called a Message of the Day (motd). It doesn't generally change every day but can contain valuable information when it does. server here at San Francisco State University.
Every machine has the potential for showing such a message, and you should at least
glance
at
it
when you
log in to see
if
times and other messages from the person
a system administrator) will be posted to
motd
is
anything has changed. Often,
who administers the machine all
not the same as the login motd. This
nal server
and not a particular
host.
users in the motd. Note that this
motd
is
associated with the termi-
Following the motd
will
be the connect
prompt. Whenever a computer expects you to type something, vide you with a prompt of
prompt's appearance
may
some
sort.
change.
By
Depending on what you
prompt is "sf su-annex 25:." This computer you want to log on to. sion, the
this point, the user enters the host to
is
have opted to log on to the SFSU VAX
machines and what they mean
later.
should pro-
1
.
where you
8). In this ses-
will select
which
connect to and proceeds with the
login script, as in the direct login session above. I
it
are doing, the
recognizing different prompts, you can
avoid confusing problems (see "Computer prompts" on page
At
down
(called
As you can see subsequent!) the naming conventions for
More on
Notice that
I
use the
"telnet sfsu-
.
1.6 Dialup Login
vaxl"
response. This
means "connect me
and Terminal Servers
15
named
Con-
to the host
sfsuvaxl.'
sfsu-annex 25: telnet sfsuvaxl Trying Connected to sfsuvaxl. Escape character is ""* .
.
'
.
necting to a machine changes from institution to institution, and can even vary
widely within an for tor
institution. Please
how to understand on how to perform
this process,
these tasks at your institution and laboratory.
Connecting does nor mean have selected a host
use this discussion as a general guideline
and seek out assistance from your instruc-
that
and can now
you are logged
start the
in. It
login script.
typing a carriage return, the actual login procedure
See "Logging on if
you skipped
1.6.3
to a
is
simply means that you
By
selecting a host and
automatically initiated.
Host" on page 6 for more details on the login procedure
to this section.
Exiting from the Terminal Server
When you
have finished your work and logged off the host you were working you will most likely still be connected to the institution via the terminal server and will be returned to that point in the login sequence. Here you may choose to connect to another host, or you can choose to exit the terminal server by typing bye. or a suitable equivalent, as follows: on.
localhost> logout session 1 - disconnected from sfsuvaxl Connection closed by foreign host. sfsu-annex 25: bye NO CARRIER
(
130.212.010.102 )...
After you type "bye." wait until you see a
your screen
at
home
"NO CARRIER" message on
before exiting from your communications package. If you
then type "athO." locally, you will manually hang up the telephone, thereby loss. The reason is that some modems are not smart enough to know when the line has been disconnected from the terminal server, and you will end up paying the telephone company for time you weren't logged in. Alternatively, you can simply pick up the telephone receiver to guar-
guaranteeing signal
antee a disconnect.
16
Chapter
1.7
Exercises and Sample Quiz Questions
1
The Login Session
Exercises
1.7.1 Exercise
1
Log
in
and out a few times without doing anything.
Exercise 2
Change your password and
Exercise 3
Find out
Exercise 4
Successfully log in from a
1.7.2
Problem
who
log out.
the system thinks
you
modem
are.
(yours or a friend's).
Sample Quiz Questions 1
List three considerations in selecting a password.
Problem 2
When
Problem 3
List three reasons
Problem 4
What the
Problem 5
are response times the best
is
why
the difference
when logged
modem?
dial-in connections are unreliable.
between a user ID and a home directory with
same name?
You
are logged
on
to
your local host by modem. You are typing
away, having a great time when you realize
You stop to take What happened?
not working. as below.
Problem 6
in via
What
is
the difference
that
your commands are
a look and notice that the prompt looks
between the reponses
to the following
com-
mands? •
whoami
•
who am
•
What
•
How many types of prompts are
i
Problem 7 is
a computer prompt?
you
likely to see in a typical lo-
gin session? •
Show what
they look like and what utility they are associated
with.
Problem
8
Problem 9
What
are the
List the steps
two user-provided components
(prompts and responses) you will provide or see when
you change your password.
Problem 10
in the login script?
What does ATDT33812(X) mean' 1
Chapter 2 Electronic
2.1
Introduction and Most computer set
up
to serve
Communications
Scope
users have used machines set
many
up for a single user, or machines Batch mode means that an
users but in batch-type mode.
entire processing task
is
submitted to the operating system for execution,
once, instead of interactively.
Nowadays
at
a single user on an isolated systemis
commonplace, and the need to communicate with other The UNIX operating system has built-in utilities that enable you to communicate with other users on your machine. More important, these same utilities provide you access to computer users throughout the world. In this chapter you learn how to use three UNIX utilities for communicating with other computer users electronically: (1) mail, (2) talk, and (3) news. Each of these utilities is intended for specific kinds of communication and has particular standards associated with its use, called communications etiquette. When you have completed this chapter, you should be comfortable sending and receiving mail, talking with other users, and reading/posting items to a newsgroup. More specifically, you should know how to invoke each utility, how to work with it, and how to exit the utility. You should also know when it is appropriate to use one utility over another, and what factors are in-
becoming
less
users, online,
volved
and
is
less
increasing.
in effective electronic
the notion of
munications;
communication.
It is
very important that you take
computer etiquette very seriously with respect it is
as important as learning
how
to electronic
com-
to use these utilities.
17
I
O
Chapter 2 Electronic Communications
Communications Etiquette
2.1.1
As computer board.
When
find a
way
we spend much
users,
of our time in front of a monitor and key-
people are trying to reach
to
communicate through
we them, it only makes sense medium where we are most likely
us, or
the
be found. In the case of electronic communications,
most
efficient
way
communications ability to
is
communicate. The
to
communicate to
to
be the
of this potential benefit in
the potential for invasion of privacy and crank calls.
As our
electronically has improved, so has our need for a sys-
tem of communicating by adhering
flip side
this also turns out to
to
that is considerate of other users
common
on the system. Only
sense guidelines can the behavior of individuals be ac-
where you can say what you please, you do not own the network and can be restricted from access to it if you annoy enough people. As such, we will introduce some notions of network etiquette open
ceptable. Unlike the
here,
society,
hoping that you will embrace them with the sensibility on which they
and
arose,
that
you
will find
ways
to
extend them
in
your
own communications
with other users.
When you licited
send electronic mail (email) to someone, you are sending unso-
comments. You are inundating the
expense.
Of course,
a
first
reaction
your opinions
recipient with
when confronted with
this
at their
form of abuse
is
"there ought to be a law..." against this form of privacy invasion, a network etiquette that guides
and protects communication between computer
users. Well,
such a "netiquette," and it consists primarily of commonsense guideThere are different rules depending on usage, and each is touched on in book. The essence you are to glean from this discussion is that you should
there
is
lines.
this
were a communications medium, because that is what it is. When you use a shared resource, as with a networked computer, everything you do will be scrutinized by other users, and so your attitude is imuse the system as though
it
portant.
2.1.2
How to Communicate Electronically
The most prevalent problem with
electronic
communication
is
the decided lack
of inflection, facial expression, and timing that guides our understanding of
what people say. Even so, in normal conversation people constantly misinterpret comments. For example, a dry wit or a matter-of-fact statement may come off as an insult when not intended as such. When the same comments are typed at a terminal, the effect is,
ings, anger, and, often, return fire. This situation
people using electronic mail, and by the speed erated and sent.
When
or stupid, then there
is
people send mail likely to
used for electronic abuse
is
The result is hurt feelcompounded by the type of
unfortunately, exaggerated.
at
is
which responses can be gen-
that, to others,
appears unthought out
be some abuse coming back. The term often
flame.
One
of your goals as a network user should
be to avoid giving or receiving flames. There are three general rules for com-
Sending and Receiving Electronic Mail
2.2
municating your thoughts electronically:
you is
and
are kidding,
much more
(3) edit
important
(1)
be clear. (2)
1
9
someone when
tell
your messages before posting them. Being clear
when you communicate
electronically. Altogether too
often people use sarcasm and cynicism in their normal conversations. If you
when communicating
are going to use these forms of speech
electronically,
When you are joking around, the recipient may not know you well enough to understand. It is important to give them a clue as to how to interpret what you say. The most obvious approach is to say you are then you have to be careful.
joking directly, or "no offense intended.*' or something that makes the recipient that your
:-)
comments
are not intended to provoke.
:-)
can be used (sometimes parenthetically
cheek" comments. Smileys are a
comments rectly.
clear to
it
smiley face
to indicate "tongue-in-
representing
start at
A
humorous comments, or
have double meaning that you want the receiver to interpret cor-
that
As you Use them carefully. Avoiding what you write. Read the message over
Their use can be abused as easily as any form of communication.
will learn, there are
many ways
to type a smiley.
flames can only be achieved by editing
again after you have written your reply. Think of
all
the recipients to the
mes-
sage and the ways they might interpret your response.
2.2
Sending and Receiving Electronic Mail UNIX
has a built-in
command,
called mail, for sending messages to and re-
ceiving messages from other users.
You
will find this to
communicate with the course assistants or instructor as well as to friends.
for all intents
Although your use of email
be an effective way to
when you have
questions.
will be limited to begin with,
is no difference between sending mail to Germany. Sending a message to someone re-
and purposes there
someone across
the
room
or to
quires five types of information.
•
How How
to
•
How
to address the mail
•
How
to
•
How
to receive
•
The is
first
to use
invoke and exit the mailer
compose
item
required for
email appropriately
all
is
the
message
and manipulate messages
addressed by looking
at
email netiquette. The second item
applications and should be the
first
thing you
try,
such as log-
ging in and out of the computer. The third item introduces the notion of mail
path or address. The fourth and
fifth
items introduce the mailer(s) and their
use.
2.2.1
Netiquette and Email
Before you learn
how
to use email, there are a
using email that you should alw ays keep
in
few guidelines
to follow
mind. The essential notion
when
in every
20
Chapter 2 Electronic Communications
one of these rules
is
think first. If
you do
this
one thing, then you will enjoy
using mail without becoming frustrated or frustrating someone else. •
Never send a piece of mail to anyone who doesn't know who you are. This means that YOU are responsible for checking the carbon-copy (cc) list on incoming mail
to
make
sure that
you do not
proliferate
your response to the
entire free world. •
Never send out any sort of solicitation. shows extremely poor taste.
•
Think before you respond. Perhaps you should compose all your responses in separate files and edit them before putting them into a mailer.
•
When
It is
an invasion of privacy, and
it
using electronic mail, always identify yourself and leave a return
email address. The most appropriate location for the return path
message,
at the
end of your message. That way, when someone
is
is in
your
done read-
ing your mail, they have your return path looking at them in the face. •
Learn what a smiley face
work way of saying
(just
ley faces, e.g., tongue in
is
and do not abuse
kidding
cheek
:
-
)
A :
)
,
.
its
use.
A
smiley face
is
a net-
There are probably hundreds of smi-
oh-no
:
-0, sad-face
:
-
(
,
etc.
•
Never "flame" someone. If you must clarify something sent in email, then think about what you are saying, how you are saying it, and why you are say-
•
Never do anything
ing
son
it.
Don't send mail just
who
that
to toot
your
own
horn.
might draw the attention of the postmaster
sending mail to your postmaster about you, then something
As you can
(the per-
administers mail on the computer). If users from other locations are
see,
is
very wrong.
email netiquette does not place unreasonable restrictions
on what you send. Rather,
it is
intended to
make
sure that users are considerate
of one another.
2.2.2
Electronic Mail Addresses
Sending a message to someone requires an address to send the mail
to.
With
you need a name, an address, and a zipcode. With a telephone you need a telephone number. With email, the situation is similar, except that you use the userid instead of the name. The address can be replaced with the symbolic machine address where the user's account is located. The machine address is associated with a uniquely numbered sequence called an Internet Protocol (IP) address. A machine's IP address is usually associated with one the postal service
more symbolic names. The machine's IP address can also be used to address mail, similar to a zipcode or telephone number. If you know a person's userid and machine address (symbolic or IP number), then you can send someone mail. For example, the entire symbolic address for the host sf suvaxl looks or
as follows:
sf suvaxl sf su edu .
.
.
2.2
In this example, both
Sending and Receiving Electronic Mail
sf suvaxl and "sf suvaxl sf su. edu" .
chine address. Actually, both are aliases for the real ically reside in a file
named "/etc/hosts" on most machine address
IP-address symbolic-address namel name2
An example
is
.
are the
ma-
machine address, and typ-
hosts, you
will find
21
systems. In
/etc/
entries of the form:
.
shown below:
localhost> more /etc/hosts
# #
Host Database
#
130.212.10.102 sfsuvaxl.sfsu.edu sfsuvaxl vaxl 127.0.0.1 localhost
The lines starting with pound signs (#) are comments. The first non commented item (i.e., 130.212.10.102) refers to the machine's Internet Protocol (IP) address. The IP address comprises four numbers separated by periods. The IP address can always be used in internetwork communications. The next entry refers to the machine's symbolic name address (i.e., sfsuvaxl sfsu. edu), and all additional columns refer to pseudonames, or aliases for the machine address. Thus, 130.212.10.102, sfsuvaxl sf su edu. sfsuvaxl. and vaxl all refer to the same computer. The difference between them is that the IP address is recognizable by any machine on the network, the symbolic address is recognizable by most machines on the network, and the aliases are only used locally. .
.
.
A space has been provided, below, for you to write down your host's symnames and IP address. You should manual for quicker access later.
bolic this
also write
them on the
inside cover of
Localhost Symbolic Names: IP Address
The name address follows
a convention, as follows:
HOST INSTITUTION INSTITUTION-TYPE .
.
The name (symbolic) address
for
sfsuvaxl
in the
/etc/hosts
file
c.2.
Chapter 2 Electronic Communications
above
tells
and the
"HOST"
us that the
sf suvaxl,
is
"INSTITUTION-TYPE"
tution types are
commonly
mai l
Table 2.1:
is
edu.
the
edu,
is
Institution types
Commercial organizations
.EDU
Educational organizations
.GOV
Government organizations
.MIL
Military organizations
.NET
Network organizations
.ORG
Other organizations
.UUCP
Dialup connections
institution-type can also be a country-code, if the host
is
outside the
United States. For example, the institution-type for hosts in Canada Brittain is
How
.UK, and Australia
does
all this
sf su,
six other insti-
used:
.COM
The
"INSTITUTION"
In addition to
is
is
.CA,
.AU.
Remember, if you you can send them mail. The
address stuff affect electronic mail?
have a user's userid and
their mail address, then
standard notation for mail address
is:
name&domain where "name"
"domain"
is
is
a person's userid, "@" separates the
their
mail address. The mail address
address where the account then
it it is
message
is
located. If
is
interpreted as the
name of
is
domain does
name and domain, and identical to the
domain. For example,
(.),
the local (current) host. Otherwise, the
passed to a mail host (name server) that determines
the specified
machine
not contain any dots
if
your account
is
how
to get to
on the machine sfsu-
vaxl and you want to send the user jhodges some mail, then you could use the userid alone, as follows:
jhodges
If,
conversely, you wanted to send mail to John Q. Smith
whose account ify the entire
is
on the host
mail address.
JSMACHINE, you would
at
Podunk University,
probably have to spec-
Using the UNIX mail
2.3
j
23
Utility
qsmi th@ j smachine podunku edu .
.
"jqsmith" is the userid, HOST.INSTITUTION.INSTITUTION-TYPE the machine name " j smachine" is the host, the school
where, according to the template provided above,
and the domain breaks down as before. In this case,
"podunku"
is
to
the institution, and because John Smith's account
cational institution, the institution-type particular system
mail address
2.3
is
is
In this section, the
unique, and because the machine address
an edu-
is
unique, your
Utility
methodology and
UNIX mailer, /usr /ucb/mail is
is at
"edu." Because every userid on a
unique.
Using the UNIX mail
mailer
is
presented.
Many
is
essential
commands
for the standard
presented, and. in the next section the
other mailers are available, such as
MH
Emacs mail.
elm, pine, and mush; however, they are similar in use to one of the mailers You should try them if the ones presented here are insufficient
discussed here. for your needs.
mail mail
is
is
the standard, out of the crate.
UNIX electronic
mail
utility.
Your
generally directed to a home-level directory and then accessed by what-
mail is fast and accessible, but not altogether user common commands used are given below, but a full de-
ever mail handler you use. friendly.
Some
of the
found using the manual pages (type 'man mail' command). What characterizes UNIX mail is its environment. When you invoke mail, alone, your prompt will change from whatever it was to the folscription of the mailer can be
as a
lowing:
see this prompt, you are in the mail environment, so do UNIX commands. The easiest way to get out of mail, without
Whenever you not try to execute
changing anything,
&
is
to type the letter 'q' as follows:
q
Writing and Sending a Message
2.3.1
Using mail to.
1.
so
all
is
easy.
You
you need now
Also known as a mailer or mail handler
already
is
know
something
that
you need an address to send the mail The mail utility provides you with
to send.
:
.
24
Chapter 2 Electronic Communications
several
A
it.
mechanisms
for writing
simple example
and manipulating the message before sending below, and a list of commands follows so
illustrated
is
you can experiment with mail on your own. The easiest form of email is someone who has an account on the same machine as you. In this case, you need only know their account name. Suppose the person you wish to send mail that to
John Q. Smith, who has the userid jqsmith. To send your friend Smith a message using the standard UNIX mail utility you would enter the command to is
as follows:
localhost> mail jqsmith
the
computer
respond with
will
Subject which you enter a one-line subject of your message, for example:
to
Subject: Party this Saturday.
.
.you're invited!
The computer will move the cursor to the next line and wait for you to enter the body (content) of your message. You enter your message one line at a time, separated by carriage returns. At this point, you will not be able to edit your message other than to back space over incorrectly typed text. That is, you will not be able to move up a line and change a spelling. If you want the ability to edit your mail completely, and you know how to use an editor, then you can use your editor from within the standard
vided here, for the
vi
editor, but a
mai 1
utility.
A brief overview is pro-
complete discussion of
sented in Chapter 4. In your message, on a blank
line,
editing
file
is
pre-
type the following (tilde-
v):
-v
and the default editor itor is
will
invoked, you can
character, with the
'
be invoked on the your message body.
move
j,' 'k,' 'h,'
and
'1'
keys on the keyboard.
You can char,' where char is the new
characters directly under the cursor using the 'x' key. acter under the cursor with 'r
can return to the mailer by typing two capital Zs.
you can continue writing
1
message where you
See Chapter 4 for information on how to invoke and use an
the default editor in 2.
the
See Appendix
G
editor,
the ed-
When you left off,
You can
vi and related commands.
delete
replace a charcharacter.
are
done
You
editing.
or you can send the
and Chapter 5 on how to
your mailer. for a short description of
Once
the cursor around the message, character by
set
2.3
Using the UNIX mail
25
Utility
When you have completed entering your message, type either or a period (.) on a line by itself. The mailer will respond with:
message.
a
Cc:
which means carbon copy. yourself),
sending a message, type
An
you want
to
send the mail to anyone else
their userid's here, separated
list
carriage return key ().
Other Ways
2.3.2
If
alternative for
The message
to
by commas. Otherwise, press the
will automatically be sent.
(twice) before sending
To
abort
it.
Send Mail
composing mail involves editing a
mailer and then sending
(e.g.,
it.
Two
before invoking the
file
approaches are presented here, both of which
assume that you have already composed the message with your favorite editor and named it "mesgl." In the first case, you will include a file into the body of the mail message. First you enter the mailer normally
(i.e.,
with the
command). When you get to the point where you would normally the message text, type the following (tilde-r) on a blank line:
start
mail typing
-r mesgl
at
which point
the mailer will grab the
file,
stuff
segments into a mail message the
vi
this
way, you can copy
easily. Tilde-r is similar to the
editor (see Chapter 4), both of
merging
into the current message,
it
and return you to typing mode for the message. In
which have
the
same
' :
r'
file
command
in
effect of including/
files.
A second way to incorporate "mesgl" into a message is to use redirection (see Chapter 5, page 157). For the sake of continuity, the here. All
you need
localhost> mail jqsmith
mail -s "subject" jqsmith where the
mail
message subject
line
The new
you have no new mail, but have mail that hasn't been removed (or read) from you mailbox, then you will receive this message without the [new] part. If you have new mail, then you will receive this message without the brackets. In either case, to receive mail, part
merely enter the
is
bracketed here for a reason.
mail command
If
alone:
localhost> mail
If
you have new or unread mail, then
a
list
played according to the format below:
[STATUS] [NUMBER] [USERID] [DATE] [SIZE] [SUBJECT] where each
Table 2.2:
classification
mail Message
list
is
described below:
information
STATUS
(N)ew
NUMBER
The mail
USERID
The
userid (or mail alias)
DATE
The
date the mail
SIZE
The message
SUBJECT
The
mail, (U)nread mail, (D)eleted mail
first
identification
number
who
sent the mail
was received by
the mailer
size in lines/bytes
few characters of the subject
line
of the messages will be dis-
2.3
Using
this format,
Using the UNIX mail
consider the sample listing of messages
27
Utility
shown below:
localhost> mail Type ? for help. Mail version 5.3 2/18/88. /usr/spool/mail/hodges " 92 messages 11 new 48 unread 81 [email protected]. ED Sat Jul 13 16:02 43/1625 "NeXT Campus Consul" Mon Jul 15 07:18 47/1937 "intrusion into my" >N 82 [email protected] Mon Jul 15 09:38 12/350 "Re: Leave of Absence" N 83 eisman "Re: address" Mon Jul 15 09:40 14/408 N 84 eisman Mon Jul 15 09:41 12/350 "Re: Huckleberry upgr" N 85 eisman "Re: CSc 214" Mon Jul 15 09:45 13/426 N 86 eisman 61/2105 "NeXT" N 87 [email protected]. ED Mon Jul 15 13:25 "last message" 18/794 N 88 [email protected]. ED Mon Jul 15 13:27 Mon Jul 15 15:36 29/951 "Re: Allegro Goodies N 89 barbara Mon Jul 22 16:18 15/573 "Postscript graphs fo" N 90 phoebe Mon Jul 22 22:07 37/1451 N 91 [email protected] Tue Jul 23 12:55 13/339 "test" N 92 barbara "
(l)
:
"
@
&
Notice that the
first
thing you will see in the listing, other than the version
number and help message,
is a header (at 1) telling what your mail path is, where your mail is stored locally, how many messages are stored there, and what their general status is. The mail path in this display is /usr / spool /mail/hodges. which describes a directory in the UNIX file sys-
which
is
tem. There are 92 messages in the mailbox,
which have not been tion
1 1
of which are new, and 48 of
read. Thus, the listing that followed only displayed a por-
(messages 81 to 92) of
all
the mail for the user hodges on this machine.
Each of the remaining lines describes a specific mail message. The "N's" column mean that these are new messages. Message 81 has no status character, meaning it has been read. The greater than symbol (>) next to message 82 indicates this as the current message pointer, meaning that this is where you are "located" in the list. in the "status*' (first)
If a userid is alone, in the third (userid)
sides
column, then the user account
on the same host or subnet where the mail was received, as
in the
re-
case of
"eisman," "barbara," and "phoebe." When a user is on the network, the userid and symbolic mail address is used, as in "vo j in@omen SFSU EDU." .
The next four
fields
.
(columns) identify the day and time (on a 24-hour
was received. For example, message number 91 was reat 10:07 p.m. The next field for message 91 indicates the size of the message, which, in this case was 3 7 lines and 14 51 bytes. Notice that message 91 has no subject line. Finally, notice that when the display is complete, you are now in the mail environment, because the prompt has changed to an ampersand (&).
clock) that the mail
cieved on Monday, July 22,
Suppose
that
you want
to read
one of these messages. All you need
to
do
Ao
Chapter 2 Electronic Communications
is
type the mail ID number. For example,
89, so
& 89
I
I
decided to read message number
typed the following.
Message 89: From barbara Mon Jul 15 15:36:53 1991 Return-Path: Received: by toaster.SFSU.EDU (NeXT-1.0 (From Sendmail id AA28412; Mon, 15 Jul 91 15:36:51 PDT From: barbara (Barbara Ford) Message- Id: Subject: Re: Allegro Goodies from Franz Inc. To: [email protected] (John Hodges) Date: Mon, 15 Jul 91 15:36:50 PDT In-Reply-To: X-Mailer: ELM [version 2.3 PL10]
5
.
52
)
/NeXT-1
.
)
(T)
.
.
.
Status: R > Barbara, >
At the same time we ordered the Franz upgrade we ordered some software upgrades for the FrameMaker application (wordprocessing and page > layout) Could you possibly check and see what happened to those orders > for me? > >
.
>
Thanks a lot.
>
Jack called New York Frame Technology and the upgrades for Frame Maker 3.0 for both the Macintosh and the IBM will be shipped out this week. I think that does it? Does it not?
> I
&
There plate
is
a lot of information in a mail message! Let's take a look at a tem-
mail header and then you will be able
fused, by the preceding example.
to appreciate, rather than
be con-
:
Using the UNIX mail
2.3
Table 2.3:
Utility
29
mail Message header information
Message 89:
The mail message number
From
The userid who
Return-Path:
The
Received:
The host
From:
The
Message-Id:
The host
Subject:
The mail message
subject line
To:
The mail message
recipient
Date:
The
date the message
In -Reply -To:
The
originating
X-Mailer:
The mailer used
Status
The
This
is
sent the
userid to which a reply should be sent that received the
message
userid and mail alias (from finger file) identification
number
was
status of the
to
message
sent
compose
the
line
message
message from the originating sender
a complete header.
that are offset
for the
message subject
When you read mail you may
header. Notice that the message
ments
message
body accompanying
by greater than signs
(>).
This
not see a complete
the header includes is
com-
a standard protocol for
reminding a person of the original context of a mail message, so that the reader doesn't wonder what the sender
is
talking about.
You can
type these directly,
or you can learn to automate the process.
In the example message above, the message easily fit into a single screen. Had the message been too long to fit, it would have scrolled past. To look at it more leisurely, you can do one of two things. First, you can specify how many lines are displayed before a display "pager" is invoked. This is accomplished with the crt variable in a file named
.mailrc" that resides in your home directory. You may not have such when you receive your account, but you can create one as long as you are ful. A value of 25 will work for most terminal screens. "
1.
You can do
this
by writing a
script
and piping mail messages through
Script writing and. in particular, a script that performs this type of task,
it
is
before they are sent.
covered
in
Chapter
13.
a file care-
OU
Chapter 2 Electronic Communications
set crt = 25
you may save the message
Alternatively,
to a file with the mail 's'
com-
mand: & s
#N
f ilenarae
where "s" stands
"filename"
is
the
for save,
omit the #N, the current message
#N1-#N2. You may
at
file
is
you want
to send the
message
to. If
you
You can also use a message range, the 'q' command and read the file us-
saved.
then quit mail with
ing your favorite editor or use the
The mailer
"#N" stands for the message number, and
name of a new
more
paging
utility.
20 messages at a time, so if you want to look messages displayed, you need to issue the fol-
will only display
messages before or
after the
lowing command: &
f
N1-N2
where "f " means to take the message list lines for the following message number range and display them, "Nl" is the range starting message number, and "N2"
is
Responding
2.3.4
To respond &
the range ending
to
message number.
Email
message, type the following mail command:
to a
R #N
where "R" stands
for reply
and "#N" stands for the message number
you are responding. The mail address of the appear
in the correct location, the subject
then you will be given the opportunity to
is
to you, but they
possible that
compose your message.
will
is
you
intentionally. Generally,
arc doing >
it
tricky. If
to
someone has
many
to the particular
person
who
sent
sent a mes-
other users, then
it
send your response to the other us-
called implicit carbon-copying. This
ers as well. This
sponse
original sender will automatically
have sent the same message
when you respond you
which
of the message will be repeated, and
Replying to messages can sometimes be e
to
is all
right as long as
however, you should send your
you mail
in the first place. If
re-
you use
the "r" instead of "R" your response will be sent to all the original recipients
of the message. users are
li^
In general,
you should look carefully
to
make
sure that no other
J as recipients before using "r" to reply to mail.
Using the UNIX mail
2.3
Forget Those Long Mail Addresses
2.3.5
possible to create a personal distribution
It is
list,
-
Utility
31
Mail Aliases
or
list
of aliases, so that you
can send mail to a group of people without using their userid and mail address.
Such
lists
can be defined by placing a
your home directory
in
(if
one
following in the .mailrc
line like the
file
exists):
alias alias-name addressl, address2 ... who work
For example, suppose you have three friends
at different busi-
nesses but you get together occasionally or share funny stories online. Instead
of retyping their addresses called
the time,
all
you might want
to create a mail alias
"cohorts:"
alias cohorts bills@widgetsrus .com, larryw@f tn500 com, sue .
where "bills." "larryw." and "sue" are your friend's userids on their local hosts, followed by their mail addresses to which a message addressed to "co-
horts" will be sent. To display the current list of such aliases, use (a) command from within mail. Systemwide distribution lists "bugs" and "help." where they
exist) are defined in
the alias
(such as
lib/alias-
"/usr/
es." Contact the system administrator to be added to a systemwide mailing alias.
Forwarding Email
2.3.6
some circumstances,
In
a user
to
Other Users
may have
multiple accounts on a machine, or ac-
counts on multiple machines. In such cases,
shown subsequently. Be
done, as
ily
which might be redirected back
nice
it is
to read mail sent to an account, or to forward
it
if
you can decide whether This
to another account.
is
eas-
careful not to produce any forwarding
to the original account, as this is called a cir-
cular reference, and will cause problems for your local postmaster. The for-
mat
for producing a mail forward
your home directory. is
be
to
file
In this file,
is
you
to create a file
named
.
insert a line that specifies
For example, consider the example below, which
sent.
forward"
"
in
where the mail
is
a
.
forward
from the host sf suvaxl:
localhost> cat .forward hodges@ futon. sf su edu .
In this
.
forward
mail receiving account)
sf su edu. The .
file, is
mail that
is
sent to
jhodgesdsf suvaxl (the futon -
forwarded to the user hodges on the host
nice thing about forward files
is
that
.
you can selectively de-
cide whether to forward or not, depending on the machine and the type of traf-
32
Chapter 2 Electronic Communications
you receive from the machine. Also, if you edit your forward files in all accounts except one to include to remaining account, then you need only log fie
.
into that (remaining) account to receive all of careful,
when
creating
.
forward
files
wrong person,
sent to your userid will either be sent to the
returned as unsent to the user
A
who
sent
You must
your mail.
be very
not to type errors, because mail then
it.
This
is
or,
worse,
it
will be
called mail bouncing.
to forward mail is when you you leave town for some reason. In such cases, you may want people sending you mail to receive a message informing them that you are unable to respond for a while, to tell them how to
second occasion
in
which you might want
are unable to respond to mail for a period, e.g., if
vacation utility is supported on your host, you can modify forward file to use the vacation utility as shown below. To use the vautility, you must first initialize it before you change your forward
reach you.
your
.
cation
If the
.
file:
localhost> vacation -I
This will add two directory,
and ".vacation. pag" to your home
files: ". vacation. dir"
vacation
will not
modify your ".forward"
file as
work unless you have done shown below:
this.
Now
you can
localhost> cat .forward hodges, "| vacation hodges"
This
is
a different kind of
.
forward
than the previous one, to
other feature, that of sending yourself mail
(hodges) makes
at
show
multiple hosts. In this
file,
anthe
you receive a copy of the mail sent. The second item pipes the received mail into the vacation program for the user hodges. Piping does for commands what redirection does for files, by elimiitem
first
nating the need to display a put). Instead the
When you fined in a
file
\
command's output
response from one
"
n subsequently.
.
is
sent a
vacation. msg." to
to the
monitor (or standard out-
command becomes
are sent mail, the sender
named
The message you send sin
sure that
the input to the next.
message whose content
residing in your
home
is
de-
directory.
people should include a mail header for them, as
2.3
Using the UNIX mail
Utility
33
localhost> cat vacation. msg From: [email protected] (Jack Hodges) Subject: I am showing an example of a vacation message Delivered-by The Vacation Program Please bear with me, I am showing an example of the UNIX vacation program, and will turn this off promptly .
:
J. B .Hodges
2.3.7
Table 2.4:
Internal
mail Commands
mail commands
-
Display the previous message
?
Display a brief summary of
i
Execute the
(R)eply
Reply
to originator of
(r)eply
Reply
to
With no arguments, display all currently defined aliases. With arguments defines an alias called name for addresses
(a)
lias name list
in
(c)hdir DIR
(co)py N1-N2 FILE
command
sender and
that follows
message only
all
original recipients
list
Change your working
directory to
Append each message do not mark
(d)elete N1-N2
shell
commands
the
in list
DIR
N1-N2
to
filename FILE, but
messages for deletion
N1-N2
Mark
Delete the current message, and display the next one
(ex) it or x
Return to the shell immediately, without modifying your mailbox
folders
List the
(fo)lder
Switch to a new mail
(f)rom N1-N2
Display the message headers for the message
(h)eaders
List the current range of headers; an
dp
the
message
names of
list
as deleted
folders in your mail folder directory file
or folder list
Nl -N2
18-message group
34
Chapter 2 Electronic Communications
Table 2.4:
Internal
mail commands
(cont)
(m)ail list
Send mail
(n)ext
Display the next message in the sequence
(p)rint N1-N2
Display each message in the
(q)uit
Terminate the session, saving
(s)ave N1-N2 FILE
Append each message in list N1-N2 mark each message for deletion
to users specified in
Each of these can be typed out
minimum
in full.
list
list
all
N1-N2 on
undeleted messages to filename
FILE, and
The parentheses simply
characters that have to be typed for the
any others included are optional for the
the screen
command
to
indicate the
be recognized;
user.
I have mentioned several tilde (~) commands, ones that are used while composing a message, specifically for editing a mail message and including external files into a mail message. Below are a few additional tilde commands for mail, but this is not an exhaustive list. Remember that each of these commands must be issued on an empty line.
Table 2.5:
Internal
mail
tilde
commands UNIX command COM
-!COM
Execute the
~b USERS
Blind carbon-copy the message to userids
-C USERS
Carbon-copy the message
~f N1-N2
Read
~m N1-N2
Same
-r FILE
Read contents of FILE
-t USERS
Send the message
~v
Edit the message with the editor defined by the .mailrc
~w FILE
Write the message
-COM
Pipe the message through
--STR
Insert the string
the messages in as
~f
to userids in
N1-N2
into this
in
USERS
USERS message
but tabs are included into the
message
to the userids in
to
STR
filename
USERS
FILE
command
in the
EDITOR
as a filter
message, preceded by ~
variable in
.
2.4
MH — The RAND
— The RAND Mail Handler
MH
2.4
35
Mail Handler
MH is a mailer developed by the RAND Corporation to run in the UNIX command shell. Unlike the UNIX mail handler "/usr/ucb/mail," MH is more transparent to the user using UNIX commands. MH is not a single program that is invoked with a single command, like mail. MH consists of a package of programs
that are
executed individually rather than collectively as a separate
process. Output from
MH commands
is
normally displayed on the screen, but
UNIX processes, and output from other UNIX process-
can be piped into other
MH commands. This way the user can intermix mail reading/responding with other UNIX processing activities quite easily. The MH en-
es can be piped into
vironment can be used as ".mh_profile that
,,
file in
is
but can also be customized by creating/editing the
your home directory. This
created the
file is
first
time
you invoke MH.
Up the MH Environment to use MH you must invoke one of its utility commands to initial-
Setting
2.4.1
Unlike mail,
One way to do this is to send yourself mail and incorporate it with the command, inc. Another way is to compose a mail message with the MH command, comp. The initialization will be demonstrated with the former apize
it.
MH
dummy piece of Now you are pre-
proach. If you do not have any unread mail, send yourself a
mail with the subject line
pared to
initialize
MH
"testmail" and
with the inc
don't read
command. Type
it.
the following at the shell
prompt:
localhost> inc I'm going to create the standard MH path for you. Create folder /usr/f 1/ jhodges/Mail/inbox" ? y Incorporating new mail into inbox. 1+ 01/28 To:jhodges testmail "
(7)
.
(T)
MH will now create a ".mh_profile" file, and
it
will ask
you
if
you want the
name "Mail," (at item 1). When you respond, MH will continue by incorporating new mail into your local mail folder (called "inbox") as shown at item (2), whereupon you can use MH to process mail. The " + " in this listing means that message is the current message, or the one standard mail directory
1
which
will
Now, be
be displayed
if
if
the user types
you wish, you can
edit
similar to (or exactly like) the
show
without a message argument.
your newly-created
sample
file
provided.
commands noted in the next section. If you want more the man entry for MH (i.e., man mh).
" .
mh_prof ile"
You can
then use
to
MH
information, please read
36
Chapter 2 Electronic Communications
Table 2.6:
Components
of the
MH
.mh_profile
Path
Mail
directory for mail folders
Draft-Folder
drafts
directory for drafts
prompter-next
prompter
prompt
Editor
vi
default editor
Msg-Protect
600
permissions on
new
articles
Folder-Protect
700
permissions on
new
folders
Signature
Jack Hodges
user signature for messages
anno
-inplace
directory for mail folders
dist
-annotate -inplace
form
forw
-annotate -inplace
form for forwarding
send
-verbose -alias aliases
where
scan
-form scan. size
what
to
do with
inc
-form scan. size
what
to
do with incorporated messages
ali
-alias aliases
where
to find aliases
whom
-alias aliases
where
to find legal mail addresses
2.4.2
Below
for editor to use
for distribution lists lists
to look for mail recipients
the scan
command
MH Commands is
a
list
of useful
MH. Some of these
MH commands.
aren't
These are commands
to
needed often, but have been included so
be used with that
you can
by quite nicely with comp, send, repl, forw, inc, next, scan, show, prev, refile, folder, and rmm.
experiment with them.
You can
get
2.4
Table 2.7:
MH
— The RAND Mail Handler
MH commands
ali
list
anno
annotate messages
comp
compose
dist
redistribute a
folder
scan +mine
which
will display a standard
folders with the
the
MH mail message
list.
folders command and change
Note
you can
that
list
mail
the current mail folder with
folder command.
The
2.4.3
You can add
MH
Aliases File
a personal alias
your "Mail"
file to
the complete mail addresses of people that
remember
not wish to
The format
their address.
director}'.
This
file
you routinely send mail is
contains to but
do
shown below:
localhost> cat -/Mail/aliases class chong,manrson, adrey, goss, grenf eld, gog, chi whir ten, gudermon bunns mike dwyerOcs ucba edu test: hodges wienrab:wienrab%oso-70@ohio-state arpa everyone * ralph hyper@hqlabs hq com, hyper%hqlem@hqlabs HQ COM tedc IGRQTGCOOC UCBA EDU ,
:
,
:
.
.
.
:
.
:
:
.
.
.
.
.
The "aliases"
file is
organized by alias names, such as class. The items
in the alias entry are listed,
each separated by commas, and offset from the
name by
alias entry is
a colon
location in
without
use
full
:
).
The
comp. Note
full
those with
(
the variety of e-mail addresses in this sampling.
paths are IDs local on the host where the alias
full
paths will
work from any
alias
used by typing the name into the To:
host. In general,
mail paths in case you copy/move
it
to
your
another host.
Those
resides, whereas alias file
should
:
2.4
— The RAND Mail Handler
Composing a Message Using
2.4.4
A sample
39
MH
MH to write and send a message is shown below. Supcompose a message to someone. The MH command to
session using
pose that you want to use
MH
comp. When we type comp, depending on what editor has been selected will get an edit window for the purpose of writing the letter. In example, the vi editor is used, so the comp window will look as follows:
is
by the user, we this
localhost> comp To: CC :
Subject
space reduced for readability
"
/usr/f 1/ jhodges/Mail/draf ts/43
"
26 characters
lines,
4
This example normally takes up the entire screen. size here to save space. In this case,
It
has been reduced
you would use vi commands
in
to insert the
mail address for the recipient, the subject line, and the content of the message.
An example
is provided. To insert the recipient's mail address, you could type a '$' to get to the end of the "To" line. This would place the cursor on the co-
You
'a' to append text, and then type the address after which you would press the key to stop appending. To move down to the subject line, you could type a j twice, one for each line. Then you could use the same procedure as for the mail address (i.e., $, a, type text. ) to type the subject, again finishing by pressing the key. You would then type a j to get onto the line of hyphens, and an 'o' to type the text of the message. When you are done composing the message, you type an key again, and you exit comp by typing the standard escape key for the editor you are using. In vi. you would type ZZ. At this point you will see the following (what now?) message:
lon.
could then type an
the colon, after
'
'
'
'
What now? h -h unknown. Hit for help.
Normally you respond with something
quit.
In this example,
tions to
I
makes
sense, like
mistyped and then took the opportunity
"What now?" When
the mail.
that
I
was done
I
used the
send
or
to list the op-
send command
to
send
40
Chapter 2 Electronic Communications
What now? Options are: display [] edit [ ; list [] push [] quit [-delete] refile [] +folder send [] whom [] What now? send
Two items of note are the "What Now? Options" Normally when you want
to post a mail
and the drafts
folder.
message or news posting you would
send, but you may also wish to review the original message using display, edit the file using edit, refile the message to a folder for future use with refile, and so on. The "drafts" folder holds copies of all mesreply with
sages you have composed, for future reference.
2.5
Problems Replying to Email You
should be able to give people
at
other institutions your email address and
when you try to send somemail will bounce (be returned un-
they should be able to send you mail. Sometimes
one mail, or they
try to
send you mail, the
sent).
There are different reasons for bounced mail, two
which
will be introduced here: unavailable service,
common
ones of
and address problems. The
bounced mail may not be your fault; if you find that you have the same problem repeatedly, you should speak with the postmaster or bottom
line
is
that
system administrator. 2.5.1
Returned Email: Service Unavailable
Occasionally systems crash. Should you be sending or receiving mail
when
a
crash occurs, your mail should be kept in a mail spool directory on the mail
gateway site,
and
until the its
system
is
restored.
Should you be sending mail
to another
network crashes, then you may have your mail bounced and
ceive a cryptic message, such as the one
shown subsequently.
re-
)
2.5
Problems Replying
41
to Email
>From MAILER-D%ucsbuxa@hub ucsb edu Mon Jan 30 14:09:10 1989 Return-Path Received: from hub.ucsb.edu (hub.ucsb.edu. ARPA) by corwyn.cognet.ucla.edu (4.12/1.3) .
.
:
.
.
id AA06823; Mon, 30 Jan 89 14:09:01 pst Received: from ucsbuxa.ucsb. eduby hub.ucsb.edu 5 59/UCSB-v2 id AA18004; Mon, 30 Jan 89 14:06:25 PST Received: from hub.ucsb.edu by ucsbuxa.ucsb.edu (3 2/SMI-3 2 id AB13170; Mon, 30 Jan 89 14:07:58 PST Date: Mon, 30 Jan 89 14:07:58 PST From: MAILER-D%ucsbuxa@hub. ucsb. edu (Mail Delivery Subsystem) -« Subject: Returned mail: Service unavailable ~« Message-Id: To: (
)
.
.
.
.
The message in
at
item
(1), the
subject line, tells you that there
sending the mail. The message
work
related.
down when for a certain
note like
it
item (2)
tells
you
that the
was
a problem
problem
is
net-
This message bounced because the remote host/system was received the mail. Normally, attempts to deliver mail are
amount of time
this,
at
(say 3 days) before bouncing. If
made
you should get
chances are reasonable that resending the message
at
a
another
time will succeed. Otherwise you should notify the postmaster. 2.5.2
Email Address Problems
There are two
name
common
mail address problems everyone should be aware
of:
Sometimes you will receive mail and find that you cannot reply without receiving a bounced message. This may be caused by a mail server that cannot find your address (at their end), their address (at your end), or some combination in between. If this is the case, then you may be able to succeed by changing the intended recipient's mail address, on your mail, to include a machine that is very likely known. You then (1)
server problems and (2) uucp addresses.
let that institution's
name
server figure
it
out. In
such cases, apply the follow-
ing rule:
replace the first @-sign with a %-sign and tack on ©domain at the end where domain is your local machine address. This will route your mail through known machine before sending it out and usually fixes problems in mailer
a
confusion.
42
Chapter 2 Electronic Communications
Here
is
an example of a modified mail address:
jqsmith% jsmachine .podunku. edu@sf suvaxl sf su. edu .
UUCP Mail
2.5.3
UNIX
UNIX
to
bounced
System Copy (uucp) addresses are another primary source of Mail addresses that end with the UUCP Institution-type, such as
mail.
the one below:
.
.
.
[host
!
]
host username !
are
sometimes mistakenly referred
provides links to numerous
sites
to as
of files (see anonymous ftp, Chapter a network connection and
usenet or internet addresses,
7,
page 242). However,
a
is
that
modem, and messages
odic basis.
If the
uucp addresses may many uucp addresses is
the mail cannot be delivered
made
not
and
within
bounces.
it
this,
bounce. The primary cause are linked to a
network via
from a mail server on a network on a
are taken
connection
uucp emulates
not a direct network connection. Because of
is
mail sent to or responding to
of the bounced mail
uucp
throughout the world for the remote copying
some
Some
set
peri-
amount of time, then
mailers can handle uucp-
type mail addresses and will try to resend the mail. Others will simply hiccup.
Below is a short list of more obscure, but valid, email addresses which show some of the combinations of notations I have mentioned. DIALRA@asuacad bitnet 74236 2754@compuserve com [email protected]. vf .ge com hisrov@cirrus com skij ig@smith. rise rockwell com Claris qm!Norman_Rockwell@ames .arc .nasa.gov .
.
.
.
.
.
.
!
Notice that the net
is
first
example
is
an address to an organization called bitnet. Bit-
an alternative network, like uucp or internet.
It
should be noted that there
are different networks around the world, that there are going to be
and
that the user
new
ones,
how
to address
supports a more direct form of
communi-
should become comfortable with figuring out
mail rather than focusing on a specific one.
2.6
Electronic Talk In
addition to electronic mail.
cation called electronic talk, All
you need
to use
it
is
UNIX talk
is
very similar to a telephone conversation.
the recipient's userid
and mail address, as
in email.
43
2.6 Electronic Talk
Unlike email, where you can send a message to
many people
at
once.
UNIX
does not come stock with a means for teleconferencing using talk.
Talk Etiquette
2.6.1
Like email,
talk
who happens
has
its
own
set
of guidelines. Because you can
'"dial"
any
you could talk to someone with whom you shouldn't be talking or who doesn't want to talk with you. There are ways around this, of course, as you will see. The essential rule user
of etiquette
is
be logged
to
to
is
entirely possible that
Sometimes talk
is
someone unless you have some introduction to them in the past.
•
ideal.
it
simple:
talk
Do
in.
You
not
need
will
not a useful
way
to
sent
them mail or otherwise had
communicate, but sometimes
to figure these things out
it is
on your own. but here are some
pointers.
Trying to
•
talk
a long distance
is
less than ideal
because the signal must
bounce off many machines and response times may be high. Trying to
•
talk
during peak periods could get frustrating, also because of
performance.
Trying to
•
talk about personal
matters can get
awkward because one cannot
read inflection or expression from the responder's words or face.
Other forms of etiquette apply
in talk,
probably more so because the con-
is "live." In the section on email it was suggested that you edit your messages before sending them. Using talk you will be not be able to do any editing, so it is essential that you have a good attitude and be willing to explain
nection
what you mean by your comments. Try not to leave things person doesn't have to guess what you mean. 2.6.2 It is
Invoking Talk
easy to
dress,
out. so that the other
and
talk to
(2)
logged on are
someone. You need
to
know
( 1 )
their userid
and mail ad-
Two ways to find out if issuing a who and looking
whether they are logged on. (
1
)
userid. If they are
fingering them, or (2)
they are for their
logged on. you can issue the following command:
localhost> talk jqsmith
which
is
the simplest
form of the template
talk userid@domain where "domain"
is
the
machine address. Note
that a full address
may be
re-
.
44
Chapter 2 Electronic Communications
quired, and that the address could be any legal mail address.
talk
When
first called,
sends the message:
Message from TalkDaernon@domain talk: connection requested by your_name@your_machine talk: respond with: talk your_name@your_machine .
to the user
.
.
you wish
talk
to
At
to.
this point, the recipient
of the message
should reply by typing:
talk your_userid@your_domain
It
doesn't matter from which machine the recipient replies
logged
in
on many machines),
person's machine
is
(i.e.,
they could be
same and the source appropriately noted. Once communication is established, as long as the userid
is
the
two parties may type simultaneously, with their output appearing rate windows, which will look like the one below:
the
in sepa-
localhost> talk [email protected] [Your party is not logged on]
localhost>
Your typing will
echo
in the
will
always echo
in the
lower window. Typing
upper window, and
that of
your friend
will cause the screen to
be
and kill characters will work in talk as normal. If you are having awkward problems in talk, then the most likely is that the terminal's termcap is incorrect (see Chapter 5. page 143) for details on working with terminal characteristics. The most obvious problem reprinted, while your erase,
you find problem is
that
having a single character echoed per
Exiting Talk
2.6.3
To
talk,
exit
moves 2.6.4
line.
your interrupt character . talk then bottom of the screen and restores the shell prompt.
just type
the cursor to the
Talk Protocol
Although both parties can type simultaneously, "talking"
is
having a dialog.
kind of protocol for establishing the end of a
You should decide on some ment. One such method is to continue to pause,
and then type
a
com-
typing until you get to where you want
double carriage return.
You may
also decide to use a
2.7 Electronic
45
News
It is up to you and your friend to agree, or at least you to do something that will enable your friend to distinguish between your comments. Otherwise they will all run together and you end up with mush, especially when the screen scrolls or refreshes.
special character or phrase.
for
Talk Permissions
2.6.5
Permission to
can also
talk
receive
talk may
set this in
your
be granted or denied using the .
cshrc
file.
For example,
messages, then you would
set
mesg
mesg command. You
if in
general you want to
as follows:
localhost> mesg y
Conversely,
you want to turn messages off so same command as follows:
if
ered, then use the
that
you cannot be both-
localhost> mesg n
These are both examples of usage one of these commands into your
2.7 Electronic
.
at the
cshrc
command
file,
and
it
line.
will
You can
also put
become your default
News
Electronic news, like email and
talk,
is
a
method of communicating with
oth-
computer users. Unlike email and talk, that single out a few users, or a sinbroadcasts messages to a particular newsgroup. A newsgroup is gle user,
er
a
m
name
1
associated with a forum on a particular topic.
The messages associated
with the newsgroup are maintained on a blackboard.
of
list
whose information
is
available to a large
group's messages are thus available to anyone
A
blackboard
number of
who
entities.
is
a type
A
news-
subscribes to the news-
group, and anyone can post to or follow up on messages posted to the newsgroup.
newsgroup are maintained across the Reading news may end up being a terrific
The messages associated with
network and updated regularly.
a
waste of time, but everyone should be introduced to
its
use.
You have been
forewarned.
News
is
a feature of the internet that arose
from the need for governmental
asencies to communicate with one another and their contractors. The use of 1. There are many incarnations of electronic news, of which rn is but one. Your institution may use news. xrn. trn. rnews. or others. In addition, there are other services which are similar to news, such as gopher and the World Wide Web, which are information servers, and ire. which is a
multiple person talk program. Seek assistance for these services locally.
4b
Chapter 2 Electronic Communications
this facility has grown over the years and has led to a proliferation (type newsgroups sometime, but be prepared for a long wait) of newsgroups. Some of these newsgroups are extremely helpful for transmitting ideas, software, announcements, copies of papers, and so forth between institutions and individ-
uals.
Conversely,
many
of the newsgroups are purely social in nature, mas-
Some do
querading as technical bulletins.
needs just like other dot In this section
newsrc
.
els are
some
common commands
for using the various
this feature,
and because many
fluence or import in your field,
it
pays to use caution
You simply
once. Be-
in posting
in-
and responding
this in
mind and
find
could help them get a job. or into graduate
never know. There are a few guidelines that can be used to
smooth your introduction
When
who
at
audience could be people of
in the
newsgroups. Unfortunately, often users do not keep
themselves flaming someone
•
lev-
Etiquette
allows a computer user to express ideas to a large audience
school.
news
presented for reference.
cause of
to
up
etiquette guidelines associated with news, setting
environment, invoking rn, reading from and posting to news will
News
2.7.1
.mailrcand .mh_prof ile).
files (e.g.,
be presented. In addition,
News
The .newsrc
a shell script for initializing news, and can be tailored to suit the user's
file is
a
1
not even masquerade.
to
news, as presented below.
posting to newsgroups, never cross post unless you
are doing. Cross-posting
is
when
a user has a
than one newsgroup. Because this
is
know what you
message targeted toward more
potentially wasteful of resources, be
was made to before doThe worst-case scenario is when the postmaster is made you are not using news properly, in which case you could lose
careful to check the
newsgroups
the original posting
ing a follow-up.
aware
news •
that
privileges.
When
posting to newsgroups,
any given
you make a 2.7.2
remember
field are reading the
fool of everyone
that the
newsgroup.
If
most important people
you make a
on your network, and they
in
fool of yourself,
will
remember.
Invoking rn
Using news
is
quite easy.
The UNIX command
is
rn
(for readnews).
localhost> rn
When
rn
is
typed for the
first
time, the system will present
you with
a
(long) introduction to reading news. In this introduction, which you should
I
.
For a more complete history of the origins of email and news, add
ers
to your
.
newsrc.
news announce newus.
.
.
47
News
2.7 Electronic
news etiquette and instructions. At the same news server will present you the option of subscribing to one or more newsgroups. The list of newsgroups is very long and you could spend hours simply saying 'n" to every newsgroup you do not want to subscribe to. Suffice it to say that you can always subscribe to a newsgroup, so it is best to exit news
read,
is
a thorough discussion of
time, the
and then
shortly after this question/answering session begins
.news re to the
file that will
UNIX
shell
be created.
exit
rn, keep typing
later edit the
'q' until
you return
prompt.
2.7.3
Reading News
rn.
mail,
like
To
has
its
multiple levels within
own environment and its
environment
to
prompts. Unlike
mail, rn uses
keep track of newsgroups and mes-
news environment:
newsgroup selection Each level has its own commands and prompt, so using rn can take some getting used to. Below I have included an entire sample session using news on the host SFSUVAX1, spread across a number of examples. sages. There are three levels in the
level. (2) article selection level,
and
(3)
( 1 )
pager selection
level.
localhost> rn (Revising soft pointers--be patient.) Unread news in comp. lang. lisp 63 articles Unread news in comp. lang. lisp. franz 2 articles Unread news in comp. sources .wanted 193 articles Unread news in rec .humor funny 3 8 articles Unread news in rec scuba 159 articles etc Checking active list for new newsgroups... Newsgroup alt.allsysop not in .newsrc--subscribe? [ynYN] n Newsgroup alt .bbs .allsysop not in .newsrc--subscribe? [ynYN] n Newsgroup alt .binaries .pictures .d not in .newsrc--subscribe? [ynYN] N .
.
(T)
continued...
This
The
first
first set
portion of the session illustrates what happens
of items
(i.e..
on the
left) is
a
list
when rn
starts up.
of the newsgroups currently sub-
The newsgroup names are organized from "comp. lang. lisp" is a computer science topic, the subtopic is programming languages, and the subsubtopic is the LISP programming language. Some of the names can be difficult to understand. On the right you are told how many articles were posted since you last read a particular newsgroup. Some groups will accumulate artiscribed to (e.g., rec.humor.funny).
general to specific. For example, the newsgroup
cles at an alarming rate. Note,
of the active
(i.e..
from the
"etc.** that this is
groups that have been added to the network since long, so
I
only a partial listing
subscribed to) groups. The next set of items are
used the capital
"N'
response
I
(at 1), after
last
read news.
responding
new newsIt is
'n' to a
rather
few. to
]
48
.
Chapter 2 Electronic Communications
stop the process.
The
reply from
rn
is
shown subsequently.
(I'll add all new newsgroups (unsubscribed) to the end of your .newsrc.) (Adding misc. writing to end of your .newsrc unsubscribed) (Adding news members to end of your .newsrc unsubscribed) (Adding news software. readers to end of your .newsrc unsubscribed) (Adding rec.games.board.ee to end of your .newsrc unsubscribed) (Adding rec .pets cats to end of your .newsrc unsubscribed) (Adding rec sport .volleyball to end of your .newsrc unsubscribed) (Adding soc culture italian to end of your .newsrc unsubscribed) (Adding vmsnet tpu to end of your .newsrc unsubscribed) .
.
.
.
.
.
.
continued...
This means that
I
will not be
asked those questions anymore, but that
subscribe to those groups in the future will
happen
is
that
rn
member
that the first
whether
I
want
if I really
will start reviewing
group was "comp
to read
any of those
.
type
'n,'
The next
to.
I
can
thing that
each subscribed newsgroup. Re-
lang .lisp." rn now prompts me
articles.
This
******** 63 unread articles in comp. lang. lisp-read now?
I
want
is
the article selection level:
n
[ynq]
so rn continues on to the next group:
******** 2 unread articles in comp. lang. lisp. franz-read now? [ynq] n ******** 193 unread articles in comp. sources .wanted-read now? [ynq] n ******** 38 unread articles in rec humor funny-read now? [ynq] = .
When
.
the user types an equal sign (=), this tells
portion of their subject field (similar to
mail's
rn
to
'f ' and
list
the articles by a
MH's
'scan' com-
mands), and enters the user into the article selection level for the group "rec 673 674 675 676 677 678 679 what-
.
humor funny." .
Stanford research Forbidden Zone #121 (Relative Value Ranking) Editorial Policy on Offensive Jokes -- Monthly Po Ghandi and British Police Proposed Improvements to World League American Fo Judges, Lawyers and Whales A hard thing to give up. .
npxr?
[
npq continued...
:
News
49
article,
type the
2.7 Electronic
This partial
list
number shown on
shows
the
left.
the articles
by number. To read an
For example,
we
will read article
number 673, hop-
ing for a chuckle.
What npxf?
[npq]673
Article 673 (37 more) in rec .humor funny (moderated) Subject: Stanford research Message- ID: From: [email protected] (Edward L. Rodriguez) Date: 4 Jul 91 07:20:06 GMT Keywords: topical, chuckle .
.
Lines:
10
My first atttempt at an original joke: How many Stanford researchers does it take to screw in a lightbulb? Three. One to hold the ladder, one to turn the bulb, and one to bill the government for the house. Edited by Brad Templeton. MAIL your jokes (jokes ONLY) to [email protected] If you post instead of mailing, it screws up the reply-address sometimes. Attribute the joke's source if at all possible. A Daemon will auto-reply. End of article 673 (of 710) - what next? [npq] q continued...
at the bottom of the posting ("Edited by ..."). This newsgroup is moderated, and all postings are screened by the moderator. Many newsgroups are unmoderated, and posting a message is discretionary. When rn has gone through all the newsgroups you have subscribed to, you will see the following message:
Note the portion
means
that this
******** End of newsgroups- what next? localhost>
at
which point you
2.7.4
will
[npq]
q
be returned to the
command
shell.
Posting to a Newsgroup
Posting to a newsgroup means to submit an article to the newsgroup. The pro-
cedure
is
gerous
tactic).
You can edit a file or write one "on the fly" (sometimes a danThe command for posting to a newsgroup is Pnews. To use the
simple.
Pnews command,
type the following
line:
localhost> Pnews newsgroup
where "newsgroup"
is
the
newsgroup you wish
to post to.
then interrogate you for a few minutes to determine
how
The system
to set
will
up the posting
DO
Chapter 2 Electronic Communications
for distribution, will ask for an editor to use, and will then ask for a file to use.
These
have defaults
all
parentheses) and this
(in
is
straightforward enough.
When you are done, the system asks you "What now?" just as in MH (see page 35). When you eventually reply with send, the message or news posting speeds along
Setting
2.7.5
The
first
time
Up
rn
the
.
newsrc
want to unsubscribe the message number
to maintain
then you can edit
file,
your newsgroups.
your
in If
with your favorite editor.
it
with an exclamation point
list
To
see the effect of the
If
to
you
before
(:)
resubscribe, simply
on the execution executed whenever you invoke .
is
To
!).
(
newsrc
home
you want
newsgroup, then simply replace the colon
procedure shown below
rn, the rn startup
named .newsrc
will create a file
it
rn how
to a
reverse this procedure.
of
News Environment
invoked
is
directory which instructs
modify the
A short list of news commands is included below.
merry way.
its
file
rn: •
rn
will look for
newsgroups. find one, •
rn
If
will
it
your
rn
back
your
will input
.
newsrc
file,
which
is
your
list
of subscribed-to
newsrc, it will create under the name oldnewsrc.
doesn't find a.
.
it
up
one. If
it
does
.
newsrc
file, listing
out the
first
several
newsgroups
with unread news. •
rn .
it
will
perform certain consistency checks on your
newsrc
up for you, but you may have
could be a •
out of date in any of several ways,
is
rn
2.7.6
An
rn
.
newsrc.
If
your
warn you and patch
to wait a little longer for
it
to start
up
(this
lot longer).
will next
check
to see if
any new newsgroups have been created, and
give you the opportunity to add them to your •
rn will
goes into the top prompt level
News Command Reference
-
the
.
newsrc.
newsgroup selection
level.
— Abridged
abridged set of commands for each of the three news levels
is
provided be-
news on your own. The commands for rn are startup, newsgroup selection, article selection, and
low. Use them to set up and read
divided into four sections:
pager selection.
Newsgroup
Selection Level
In this section the
newsgroups
in
prompt looks
words "next" and "previous" refer to the ordering of the .newsrc file. At the newsgroup selection level, the
your
like this:
******** 63 unread articles in comp. lang. lisp-read now?
[ynq]
2.7 Electronic
The following commands can be used Table 2.8: y,
at the
newsgroup
News
51
selection level:
rn Newsgi 'oup selection commands
or space
=
Do
this
newsgroup now
Do
this
newsgroup now, but
list
subjects before displaying arti-
cles
n
Do
not do this newsgroup now, go to the next newsgroup
P
Do
not do this newsgroup now, go to the previous
1
Do
not do this
$
Do
not do this newsgroup now. go to the end of the newsgroups
newsgroup now, go
to the first
newsgoup
newsgroup
list
g newsgroup
Go
/pattern
Scan forward for a newsgroup matching pattern, e.g., use ? to match a single character, * to match any sequence of characters
?pattern
Same
u
Unsubscribe from current newsgroup
c
Catch up
q
Quit
X
Quit, restoring
to newsgroup; if you aren't a subscriber, you will be asked if you want to subscribe and how you want the newsgroup placed
as
/pattern,
— mark .
all
newnewsrc.
unread
newsrc
you would have had .
but search backward
in
articles in this
newsgroup
to its state at start of
you had exited with case you didn't really want if
rn; the 'q' will
as read
.
newsrc be called
to type 'x.'
Article Selection Level
On
the article selection level
article
before the article
comes
at the
END of the
is
you are not asked whether you want
article.
The prompt
the following:
End of article 673
(of 710) -what next?
to read an
displayed; the normal article selection prompt
[npq]
at the
end of an
article
looks like
52
Chapter 2 Electronic Communications
The following
Table 2.9: n,
rn
Article selection level
space
commands
are the available
in the article selection level:
commands
Scan forward for the next unread
N
Go
P
Scan backward for the previous unread isn't a previous unread article
P
Go
Refresh the screen
Restart the current article, and decrypt as a rot 13 message
X
Refresh the screen, and decrypt as rot 13 message
number
Go
pattern
Scan forward for
?pattern
Scan backward for
r
Reply through net mail
R
Reply, including the current article in the header
f
Submit a follow-up
F
Submit a follow-up article and include the old article with lines prefixed either by ">" or by the arguments to a "-F" switch
c
Catch up
u
Unsubscribe
Save
List subject of unread articles
s
destination
=
It
to the next article
to the
numbered
in this
article
u
if
there
containing pattern
article
containing pattern
newsgroup;
to this
i.e.,
(i.e.,
mark
all articles
and X commands
un-encrypt) messages that are posted that
The encryption schemes in
as read
sh
Although
this
established for maintaining the content of
ant to read postings,
generated
newsgroup
to a filename or pipe using
and decrypt
file
article
are
meant
to
may seem
news
the postmasters at each institution to maintain control users.
here
article
sensitive or offensive to certain groups.
no means
article; stay
to the previous article
should probably be noted that the
to encrypt
article
(if
are used
may
postings.
It is
who
is
left to
and peace among
inform readers that they
order to avoid being offended. Those
be
odd. there
their
may
not
decr\ pt the
2.8 Exercises
messages have no the intent of the
real
53
and Sample Quiz Questions
complaint because they were warned ...
at least, that is
mechanism.
Pager Selection Level At
the pager level (within an article), the
prompt looks
like the following:
-MORE- (17%)
where the percentage (here \19c) means been displayed so
Table 2.10:
rn Pager selection
far.
The commands
level
that this
is
how much
of the article has
available at page selection level are
commands
X
Display next page and decrypt a rot 13 message
Display one more line
q
Go to the end of the current article (don't mark it either as read or unread)
3
•
What
is
the
•
Provide an example of
symbol used
mail message
for in
MH equivalent to the
lists?
>?
how each would
appear
in a
message
list:
Problem 4
MH commands to do the following:
Provide the
Get new messages into List all
messages
Respond
to
in
MH:
your working mail folder:
message number 32:
Put message number 32 into your "Personal" mail folder: Exit
Problem 5
What
is
MH:
the proper
rn command used
to catch
up on unread news? To
search for a specific piece of information in news
Problem 6
List three types of
do not occur this is a
in
problems posed by electronic communications
normal dialogs between people. For each, describe
Define 'flame.' List two practices
Problem
Answer •
that
why
problem.
Problem 7 X
articles'.'
avoid giving or receiving flames?
the following questions about
What of
to
rn
news
command
is
used
to
rn
list
the
articles within a particular
numbers and subject
line
newsgroup?
•
What command
is
used
•
What command
is
used to Follow up to a newsgroup posting
to
add
a
newsgroup? '
2.8 Exercises
Problem 9
Problem 10
and Sample Quiz Questions
55
Describe the name address convention, and give three examples of
What
stitution types
commonly
someone with
the following address:
Name
used.
is
the origin of a
irisa irisa .
.
f r?
the file that contains the following line. Explain the
each term
in-
message from
meaning of
in the line.
130.212.10.102 sfsuvaxl.sfsu.edu sfsuvaxl vaxl
3.1
Introduction and From
Scope
a user's viewpoint, an operating
system consists of different programs
executed to perform specific tasks. Programs that perform system-re-
that are
lated user instructions are called
commands. Commands
are distinguished
from applications and user-defined programs, which perform non-system-related tasks. All of the
UNIX it
file
programs and data
that reside
system, and each must be organized
on the system make up the
in a consistent
manner so
that
can be used when needed. This chapter presents the organization and manip-
ulation of
programs and data
in the
UNIX
operating system.
The
topics cov-
ered in this chapter are listed below: File
system components: directories and
files
UNIX commands Directories and directory manipulation Files File
and
file
manipulation
system security
UNIX file system is an organization of files and directories that enable perform its function. Knowing how the UNIX file system is organized is important in knowing how to use When you have completed this chapter, you should understand how the UNIX file system is organized, and The
UNIX
to
it.
how
to
of the
move around
file
system
within
it.
in different
You
should be able to manipulate the contents
ways. For example, you should be able to
contents of a directory anywhere in the currently located in the fde system.
file
You
list
the
system, regardless of where you are
should be able to change your loca-
57
58
Chapter 3 The UNIX
tion to
File
System
anywhere
contents of the eral, a file
in the file
file
system, and you should be able to manipulate the
system: to create, move, rename, or execute them. In gen-
system can reside on any type of storage device. For example, a disk
drive, a diskette drive, an optical drive,
tems on them. By mounting a device,
UNIX file
system and
tended scope of
this
its
and a tape drive can
its file
The mounting of file systems
users.
course and
is
have
all
file sys-
system can be made visible
mentioned for
is
beyond
to the
the in-
clarification purposes. File
system mounting can only be performed by administrative personnel. Beginning with this chapter, It
will
be assumed
I
that,
at the end of every example. command name and arguments are invoked,
will cease to print a
when
a
they are followed by a carriage return.
3.2
File
System Components In
UNIX,
file is
there are essentially
two types of structures: directories and
a meaningful sequence of information that has a
enced. Programs and data are types of
files. Files
name
files.
The
file
A directory can orga-
files.
system describes the organization of directories and
and be organized by other
rectory, called the root directory,
A
Files cannot organize anything except in-
formation; neither directories nor other
rectories can organize
files.
can be refer-
are organized in directories,
so a directory can be thought of as a folder or organizer. nize other directories as well as
that
Most di-
files.
one
di-
which cannot be organized by (contained
in)
another directory. The root directory
is
directories.
There
is
often visualized as the top of a tree's
root system or the branches of an inverted tree.
The
directories
which the root
directory organizes can be thought of as branch points to different parts of the root system (see Fig. 3.1). Every directory and file originates from the root directory, tree.
and every branch point actually describes a mini root system or sub-
Every directory of the
file
system eventually terminates with a
directory that doesn't organize anything, called an
nating
file is
often called a node of the
nothing can emanate from
it.
file
empty
directory.
system, or a leaf of the
file
A
tree,
or a
termi-
because
3.2 File
Figure
3.1
:
The UNIX
file
system
Ctmp^
CaevJ
(_users^
Q bin}
(^tom^
(^blevine^
is
System Components
comprised of directories and
GD
(1)
Root Directory
•
(2)
Subdirectory
•
(3) File
(L
b
"\^
]
Directories and files are organized by function.
number of primary subdirectories. For example,
are contained in a directory called "usr."
found
files
(jhodges^
C^CCod^
a
59
in the directories
Many
The all
root directory contains
of the users' directories
of the system
"bin" and "etc." Other system
commands
are
directories are listed
below:
localhost> Is -F
backups/ bin/ dba/ dev/ etc/
/
genvmunix* lib/
lost+f ound/ mnt/ opr/
pak_admin/ pcs750 .bin* public/ quotas secure/
This example slash
(
/
).
is
file.
Note
that
for an executable
by
is
which
is
denoted by a single
name, root does
not.
Any
directory
The contents of the root diFor example, the item "vmunix," above,
called a subdirectory.
"vmunix"
files.
followed by an asterisk ( * ). This is the notation meaning that it is a program." Directories are denoted name. Thus the directory " 1 ib" is listed as " 1 ib / " in this
file,
a slash after their
usr/ var/
directories have a
rectory can be directories and a
vmunix*
ultrixboot
a listing of a root directory,
Whereas most
'below' the root directory
is
vmb. exe*
sys/ tmp/
is
.
60
Chapter 3 The UNIX
File
System
Below is a partial description of the contents of some of the major subThe contents of these directories changes slightly from one UNIX
listing.
directories.
implementation and platform to another.
Table
Standard UNIX top-level directories
3.1
/bin
System binaries and many of the system commands
/usr
Manual pages, user
/lib
System
/etc
System commands
/dev
Hardware device configuration
/tmp
Temporary storage
libraries
These directory names and
UNIX
directories, applications
files
(publically read/writable)
their contents are
an important part of the
operating system, because they have been standardized across the dif-
you get an account on a network somewhere other than where your current account is. and you want to know where the contributed software is. then if it was placed in "/usr /local/bin" or "/usr/contrib" on your old system, that would be the first ferent platforms that use the operating system. If
place to look on the
new
system.
tem, you are simply going to
aged to do ing the
so.
file
This
the best
is
system, and
it is
To
find out just
what software
is
on your sys-
have to go looking around, and you are encour-
way
to learn about the
the only
way
commands
to find those little
for manipulat-
jewel programs
that aren't publicized but are available nonetheless.
Naming Conventions and Paths
3.2.1
Each directory and path
is
file in
the file system
a concatenation of directory
is
associated with a path.
names, separated by slashes
An
item's
(/), starting
with the root directory and ending with the directory in which the item resides.
The
root directory
Any full
is
designated by a lone slash
(/).
path that begins with the root directory (/)
path.
A
full
path
is
rectory can be referenced with a full path, although
1
2.
Sec Section This
is
not
3.4.1
.
page 69 for details on
necessanK
true.
is
called an absolute or
a path that starts at the root directory.
this
nomenclature.
See Section 3.6.2 tor
details
it
is
Any
file
or di-
rarely necessary to
write the entire path out in
full.
All other full paths start with a slash to denote
that every path originates with the root directory.
"jhodges"
belonging to the user
61
UNIX Commands
3.3
For example, the directory
resides in the "fl" subdirectory under
"/usr," which is a subdirectory off root. So my directory, jhodges, resides three levels below root. The path for the files and directories belonging to
jhodges
is
denoted as follows (see bold
line in Fig. 3.1):
/usr/f 1/ jhodges
The root directory has no name, and all other names specify The path is our means of finding and executing programs in the
To
tem.
use the path effectively,
it is
important to understand the syntax asso-
UNIX commands and to know how to UNIX commands. For the remainder of this chapter,
obtain information about
ciated with
Ch3
manual, examples will be provided from the
this
subdirectories.
UNIX file sys-
and
in general
"UNIX/Ch3"
throughout
directory.
It
should be noted that any modifications to a directory require write permission to the directory
to
(covered
make modifications
Section 3.6.2 of this chapter).
of these directories,
your own.
else's directory except
similar
in
to either
You
are
You
will not
be able
anyone perform the same or
or, in general, to
encouraged
to
your own directory, and even to excommands will work in directories not owned by you. look at the "UNIX" directory to compare what you do to
commands you
are reading here in
periment and see which
You
are
welcome
what you see
to
in the
manual. They should be similar,
if
not identical.
UNIX Commands
3.3
When
users log on, the
first
thing they see
about what happens to produce the prompt
can be manipulated. For now, to the file system.
it
is
is
More is explained and how these events
a shell prompt.
in
Chapter
important to look
5,
at the
prompt with respect
Consider the prompt below.
localhost>
When you mand
see this
interpreter. This
grams and they
1.
fl is actually a
file
2.
mounted
file
are evaluated
system of
The "jhodges"
As opposed
ing.
its
and executed now.- Each
own. and looks
UNIX command
like a subdirectory in the local
is is-
UNIX
system. directory
is
provided as a template to
a variety of responses that are not clearly 3.
prompt you are in a command shell. A shell is a comthat you can issue UNIX commands or run pro-
means
to a batch job,
which
is
shown
in a
illustrate the
use of
commands and
to
show
simple directory such as "UNIX."
submitted altogether and run
later
according to priority rank-
62
Chapter 3 The UNIX
File
System
sued with the following syntax:
command -name [command-options] [command-arguments] command- opt ions
where
(often called keys) are single or multiple charac-
how the command response is displayed, and command- arguments are the user-provided items that the command operates on. Some command options or arguments may be optional, and will appear in square brackets in the command template. In general these will generally be noted in the text. In UNIX, most every command argument will be a directory, which are used
ters
a
file,
to
to specialize
or a path to same, so you, as a user, will have to
do (what command you wish
know what it
to execute, its options,
and
its
is
you want
required argu-
ments) before you can do anything. Let's say that you successfully changed
your password
in the first
chapter of this manual.
Then you
correctly issued the
passwd command: localhost> passwd
passwd is
a good//rs/
command
because
it
performs a necessary task of pro-
viding security for your account, and because
ments.
Many
file
it
system commands can be used
requires no options or arguin this
way. Others are more
What do you do when you do not know the name, options, or arguments of a command, even though you know what it is that you want to do?
complicated.
3.3.1
When
Getting Help about
Commands: Online Manual Pages
learning about any operating system,
it is
perfectly normal not to under-
you need them. This is awkward because, without knowing what it is that you need help on. or what command it is that you need to execute, you will not know where to look for the answer. stand the use of particular
The its
commands
UNIX operating system is
nuances. So
there
is
until
so large that no one person can
remember all of
an online version of the manual that you can call on to
find out syntax and usage of any
UNIX command. What command
is
more, you will not
The drawback is that They are comsomeone to reneeded by information have only the save and space, pact to is necthis is all that fresh their memory of how to execute the command. Often have to know, a
the
priori,
manual pages
essary, but
what the name of the
are written as a reference
you need
to
know how
is.
and not as a
to read the
tutorial.
man page
or you will be as con-
fused as before you began. There are three things you need to know to use the
man
pages: (1)
how
to
invoke them. (2) their general format, and (3) how
search for what you are looking
for.
to
.
.
.
.
.
Man
Page
The command is man, which you type prompt. The general usage is shown below:
Using the online manual the shell
63
UNIX Commands
3.3
Invoking the
)
.
simple.
is
at
man [subsection] command -name command name, "subsection" is an optional (as noted by the square brackets) subdirectory name, and "command -name" is the name of some command you wish information about. For example, if you type: where "man"
is
the
localhost> man man
man Name
(
1
V ^\
man - displays manual pages online subsection Syntax man -k keyword. man -f page_title. section man options -rof f_options page_title. man options section page_title. section page_title /usr/bin/man option /usr/bin/man options section page_title. Description There are two man commands: /usr/ucb/man, and /usr/bin/man. For most users, /usr/ucb/man is the default man command. The command which man shows you which man command is the default. The recommended default is /usr/ucb/man. .
.
[
]
[
[
]
[
]
[
]
[
]
.
[
is
]
.
.
]
"manual" page, or
actually a
.
]
.
[
the system displays a
This
.
.
[
uals.
.
.
.
.
.
man
]
.
.
page describing
condensed version of the
full
system
the online
man-
command refercommand
ence manuals that are available for the computer. The 'subsection'
argument
is
to look for
optional because the system has a set of default locations in which
The "subsection' when provided, overrides the listing that 'man (1) has been labeled. The '1' refers to man library where this command's man page resides.
manual pages.
search. Notice in the
a subdirectory in the
'
If you know what sort of task you want to find out about, but not the commands associated with the task, then there is a modification of the man command you can use. The modification makes use of what is called a command
option or key.
1.
Many command'sarguments
square brackets.
If a
have default values, which are normally identified by a value
value isn't provided for an argument, then the default value
be confused with a command's optional arguments which are also shown
in
is
in
used. This could
square brackets.
64
Chapter 3 The UNIX
File
System
localhost> man -k manual where the '-k' is a man command option. Virtually every UNIX command has options. The argument "manual" is called a string. The '-k' man option
means "do ual,'
a search through
and return a
mand
list
of the
all the man pages for a string equivalent to 'manman pages that pass the test." Thus, after the com-
issued above, the listing
shown below
es of information in the listing below:
section for the
command, and
( 1 )
is
displayed. There are three piec-
command name,
(3) the subject
(2)
man
library sub-
header for the command.
localhost> man -k manual - create the cat files for the manual catman (8) - displays manual pages online man (1) - manually manipulate the routing tables route (8c) - translate version 6 manual macros to version 7 macros trman (1) - locate source, binary, whereis (1) and or manual for program
The items on
the far left are
command names,
so you can then issue a
man
command with one of these items as the argument. There are a number of places a man page might be found. Many manual pages are located in the directory "/usr/man." The numbers in parentheses next to the command name designate a subdirectory where the man command should look to find the man page file.
For example, items with a
directory under the standard the
"NAME"
line in the
( 1 )
man
man page
after
them can be found The items on
directory.
the string in the
man
for.
left is
"man
(
1
sub-
the
left.
This
This provides the context of
page. For example, after issuing the
above, one of the items on the
"manl"
the right constitute
command on
that describes the
item will contain the string you were looking
in the
." )
To
man command,
the right of this item
is
"displays manual pages online ." If you go back and look at the name line for the man man page, you will see that it is the same. There is no difference between executing the 'man 1 man' command and executing the line
the
'man man' command. However, this is because Section of the manual is in a default manpage directory. If the man page was in Section 8. as is catman page, then the only way to read the manual page is to issue the 1
pages the
command: localhost> man
8
catman
Another name, or issue the
I
.
You
will read
more about
alias, for the
apropos command aliases
and how
command 'man
just as
to create
you would
your
o\\ n in
the
-k'
is
apropos. You can
'man -k' command.
Chapter
5.
3.3
65
UNIX Commands
localhost> apropos manual
Man
Page Display Format
Understanding a
what
to expect,
comes
man page
requires
and what order
some
to expect
a palatable task. Otherwise,
it
it
will
"inside" information. If
you know
then finding that information be-
in.
seem impossible. Every man page
adheres to a similar format, as shown below. Not every field will be found in
every
man
page, and not always in the
the standard
Table
3.2:
Manua page 1
man page
same
order: however. Table 3.2
shows
headings and their contents.
entry headings
NAME
The command name and
SYNTAX
The command's
DESCRIPTION
»
A discussion
OPTIONS
A
RESTRICTIONS
«>
Special instructions and caveats
EXAMPLES
A couple
illustrations of
comman
FILES
A
of related
such as libraries
BUGS
Generally system specific, with notes from system programmers
SEE ALSO
Related
legal
a short description of
arguments and
of the command,
discussion of each of the
listing
files,
commands and
its
its
function
their order of
usage, and
its
appearance
history
command's options and
their
usage
options application
information references
Locating Information in Manual Pages
When more
a
man command is issued, the system displays the man page using the utility, more is a command that displays a file one screen page
paging
at a time.
A page is displayed,
and. at the bottom of the page, a highlighted box
is displayed. The percentage refers to you have already seen. If you desire to see more of the file, then hit the space bar. If you want to quit, then type the letter 'q.' To find a particular heading or string in a man page, type a slash, the string, and a carriage return. For example, if you want to find the "Examples" heading in the man man page, then after you have invoked the 'man man' command type the characters that follow, which will appear where the word "more" is displayed.
with a percentage and the word "more" the
amount of
the file
:
66
Chapter 3 The UNIX
File
System
/Examples
where the slash ('/)
is
a called a search
string (or pattern) to search for.
type is
"/examples,"
or
Note
that
command and "Examples" is the UNIX is case sensitive, so when you
"/EXAMPLES," you
the response to this search in the
will see different results.
man command
Below
manpage:
localhost> man man Display offirst screen of man page here
/Examples
.
skipping catman(8) command. Examples /usr/ucb/man Examples The following examples all assume the use of the default command: /usr/ucb/man. The following example shows how to locate reference pages containing
the
keyword
graph man -k graph The following example shows how to display the graph '
'
%
(Ig)
reference
page: % man lg graph The following example shows how to display plot reference pages: % man 1 plot 3 plot --More-- (79%)
The search only succeeds the current display page. If
case, the is
it
word "Examples,"
displayed
at
(
1
),
which
is
if
the search key
"/Examples," current string. You can additional options.
"Examples")
is
not on
close to the top of the screen. In this example,
three lines from the top of the display. If
to search to the next occurrence of
retype
(i.e.,
succeeds, the search will place the key (in this
Examples
in the file,
you
it
you want have to
will either
or you can type an 'n' for the next occurrence of the
type a question mark while
in
more
to get a listing
of
)
.
3.4 Directories
67
and Directory Manipulation
and Directory Manipulation
3.4 Directories
Assuming that you have successfully logged into localhost at least once, that you have changed your password, and that you are familiar with a few things about the file system, then you are ready to explore the file system for yourself. As previously mentioned, this is the best way to build confidence in your
ability to navigate
what goodies are out
UNIX
nipulate
so that
we
files. (2)
(4)
is
be able to
(3) be able
are in the
and
located,
so that
a
it
is
the best
you learn how
to find out
and ma-
to navigate
log on to a system,
good place
way
you
are placed in
to begin. Directories are the lo-
As
data.
users,
we must we can find (
1
are currently positioned in the file system, so that
what we seek,
tains
When you
which we execute or use as
cations of the files
know where we
system, and
file
there. In this section
directories.
your home directory,
and use other
around the
list
to
the contents of a directory to see if
move our
same directory where
be able to create, rename,
present location in the
we wish
the files
move
to
file
it
con-
system
work with
are
or destroy directories. Each of
these topics will be covered in the sections below. •
Current directory
•
Directory contents
•
Change
•
Directory creation
•
Directory renaming
directory
•
Directory relocation
•
Directory removal
The Current/Working Directory
3.4.1
When
users log on to a
home
called their
owned by your
computer running UNIX, they are placed
directory.
Your home
directory
the directory
is
user ID. In general, your user ID and your
have the same name.
home
what
is
which
is
in
directory will
A user's files and directories generally reside
"in," or
"be-
you potentially have your own subtree in the file system. You will be able to move around the file system mostly unfettered, and look at directories and files where you go, but your ability to low." their
home
directory,
meaning
that
write (create, modify, or destroy) files and directories
is
generally limited to
your home directory. As a result, when a user successfully creates a new directory or
1
This
is
file, it
a metaphor, of course.
directories as places to go. 2.
Any
user can create
It is
easier to speak of the
home
will frequently use the location
All this talk of "generally"
places other than your 3.
I
can generally be found somewhere under their
is
meant only
own
directory, but
files
and directories
you
an exception rather than a
in the
when
to directories.
will be able to write in rule.
temporary directory called
deleted on a regular basis by the system administrator or
directory.
directory as a location and other
metaphor when referring
to suggest that, at times,
it is
home
the system
is
"
/
tmp," but these are
rebooted.
68
Chapter 3 The UNIX
File
System
Wherever
the user
directory.
When you
happens
to
be located
called the current or
is
your home directory are one and the same. The working directory because, in general,
ple, if
UNIX commands
made with
tories are being
assume
it
UNIX will
to print a file, then
resides in the current/working directory unless
which
is
the reference to files
important
and direc-
respect to the current/working directory. For
you issue a command
file's location
working
log on to the system, your current/working directory and
can use to find the
file.
assume
you specify another path for the Because of this, it is often nec-
essary to remind yourself of what directory you are currently located
can be accomplished with the ing directory,
command for printing
(i.e.,
in.
This
displaying) the work-
pwd.
Printing/Displaying the
Working Directory
Probably one of the most important
command
exam-
that the file
you where you
will tell
UNIX commands
is
pwd, because this Without such
are located in the file system.
command, you might be able to get around, even create files or directories, but you would never know where you are working from. Because UNIX requires a reference point or path to every command, program, or data file, pwd provides a reference point, pwd is simple to use, as shown below: a
localhost> pwd
It is
not necessary to use
when you
pwd
on the
first
command you
log in the system automatically places
tory. Nevertheless,
would respond
I
had issued the command
in
of a session, because
your
own home
right after logging on. the
direc-
system
as follows:
/usr/f 1/ jhodges
As mentioned
previously, this
is
the notation that the system uses to denote
the location of a directory in the file system,
directory
owned by
response that excellent
way
is
the user jhodges.
displayed will be the
When
its
path: in this case the path to the
pwd command is issued, the pwd command is an
the
full path,
to navigate in the system. In this
thus the
example, the
first
slash
(/).
as
always, references the root directory, "usr" is a first-level subdirectory, and both "f 1" and "jhodges" are other subdirectories. Directories are always separated by slashes, so
it
three levels 'down' in the
is
file
easy to see that the
"jhodges"
subdirectory
There are two shorthand notations for your home directory useful. I,
full
HOME,
that are very
environment variable, and is set path of your home directory when you log in. HOME, if echoed,
One
is
which
is
system.
is
called an
to
as
.
3.4 Directories
below, will return the same as above.
1
and Directory Manipulation
69
Notice that in referring to the
HOME
You
find that
localhost> echo $HOME u s r f 1 1 hodge s '
environment variable, we precede
its
name with
reference operator for
all
variable in the
this is the
a dollar sign
(
$
).
UNIX command environ-
ment/interpreter.
A need
second shorthand for your home director}
to refer to
is tilde (~).
Any
time you
your home directory you can use either of these shorthands
without having to type the entire path which they represent.
localhost> echo usr fl ;hodges
-
Listing Directory Contents
Knowing where you
are in the file system
tents of a director}'
equally important in being able to access and use the
located there.
To
is
list (i.e..
is
very important.
Knowing
the confiles
you will use the meaning that the com-
display) the contents of a directory
command. Is means list the contents of a director}, mand expects a directory as an argument. When invoked without an argument: however, the 2 s command assumes that the directory is the current/working
Is
directory. If an listed. If a
argument
nondirectory
consider the listing of the
1
:
is
given, then the contents of that directory will be
given as argument,
is
home
it
will
be echoed. For example,
directory of jhodges below:
-
:;.!":=:> Is
mail-probs new-net new-topics stud212 testl .c texput log
csc212 cshrc-cognet dead. letter
F90 Mail :-!ar:ual-in-IaTeX
News
fe
P.ZADMZ 7eX3 .
.
f ilterf ile
C
TeX3 7e:~a:ros
foo2
.
.
forward ftp. list mail -elsewhere
bin core
Notice that the listing
tmac
widget
is in
columns, and that
it
is
alphabetical, starting with
The Is command, issued without options, provides this form of listHowever, a listing such as this is not extremely informative. Suppose you
capitals. ing.
1
2.
HOME and home are equivalent for the purposes of the current discussion. Typing Is
$ HOME or
Is ~ would produce
the
same
result.
70
Chapter 3 The UNIX
File
System
have forgotten what directory you are
in,
or
if this is
not your
home
directory,
you may not know what type of items (i.e. their status) are being listed. The 1 s command has a number of options that make directory listings more useful. 4
For example, the -F' option acter that tells
executable
lists
what the item type
file is
the contents of the directory, but adds a char-
A directory
followed by a slash (/), an followed by an asterisk (*), and a link is followed by an at is.
sign (@). Items without something after
ably represent readable
same
them
For example,
are
if
none of the above, and prob-
the "-F" option
is
used on the
directory as above, the result changes:
localhost> Is -F F90/ Mail/
Manual -in-LaTeX/ News/ README. TeX3 .0* TeX3.0/ -* Texmacros/ bin/ core*
files.
is
mail-probs* new-net* new- topics* stud212*
csc212* cshrc-cognet* dead. letter fe*
®
In this example,
rectory,
testl c* texput log tmac/ widget*
filterfile* foo2* forward* ftp. list mail -elsewhere/ you can see
.
.
that the item
"testl c" (at 2) is an Is command, and you should
and
tions to the
that
.
"TeX3
executable read the
.
0"
file.
(at 1) is really a di-
There are many op-
man page (man Is)
to find
out more about them. Two options that are particularly useful are the '-a" (all option and the '-1' (long) option. The '-a" option lists the contents, as above, I
as well as the dot files that usuallv aren't listed.
localhost> Is -aF ./ .
./
cshrc forward login login-original* .mailrc .mh_prof ile .newsrc .old-cshrc* .oldnewsrc .plan .profile* .project rhosts .
.
.
.
.
rnlast rnsof F90/ Mail/ Manua 1 -in- LaTeX News/ * README TeX3 TeX3 .0/ Texmacros/ bin/ core* csc212* cshrc-cognet* dead. letter .
.
.
fe*
.
filterfile* foo2* forward* ftp. list mail -elsewhere/ mail-probs* new-net* new-topics* stud212* testl. c* texput log tmac/ widget* .
3.4 Directories
In this
example, two things are apparent.
command, you can
First, in the
You
see that options can be put together (concatenated), side by side.
put them directly together or you will have to use additional hyphens.
phen informs the command
whatever follows the hyphen
that
71
and Directory Manipulation
is
should
The hy-
not the expect-
ed directory argument. Thus, you can produce any combination of effects that
you want. Second, you see
many more
that there are really
played with the original Is command. The dot
files are
files
than were dis-
those files that start
with a dot (or period), and are often, but not always, used for initializing and
customizing the shell and other applications. In addition, at the top of the listing there are
two
entries for directories
and a double dot
which you have not seen before: a single dot
(.)
UNIX
the current directory.
fde system, the directory labeled dot
is
you are located, dot
will represent that directory, regardless of
double-dot directory
is
'up' through the file
You can is
as
its
In the
Wherever name. The
parent directory, and provides the necessary route
system without using full paths (path names starting from
You can
the root directory). to list dot files
the
(..).
use the capital ('A') option with the
Is
and directories other them the current and parent
use dot or double-dot as arguments in any
command where
command
directories.
a directory
Is is one such command. If you type 'Is ', then it is the same typing Is without any argument, because the dot directory is the current di-
required.
rectory.
see a
.
may appear worthless, you will other commands, particularly when moving or copy-
Although the use of dot
meaning
for
it
with
in this
context
Is
ing files and directories. Using the double dot as an argument to diately obvious, because
is
imme-
you can then see the contents of the directory above
the current directory.
localhost> aastromo abag aberger abierman aelimam ahhsp ajung alanjung
1s
.
.
The
listing
j j
you see above
fl. because "f 1"
in
mmurphy montever moortgat mpearson mreisch ncalgrnt nf isher ngraf ilo
brands edwards jeng jgemello jglenn jhodges j huang j Johnson
compment cpauling davidm dellis dgermann dhof fman dleveen dphsf
is
is
the double-dot can be
resides).
You can
much more
if you cannot remember
sf ch sgill shehadeh shelter shu smedes
identical to the listing for the
the parent directory for
which "jhodges"
sbhimjee scpeo
"jhodges"
command Is /usr/
(i.e., is
the directory
see, then, that listing directories using
efficient than using the full path, especially
the full path.
72
Chapter 3 The UNIX
One
System
File
last
example using Is. You have seen how
current directory and
its
parent, to
by type. You have not seen how
tents
attributes
ownership,
(i.e.,
size,
to list the contents of the
and
the entire contents,
list
to list the con-
to list the contents of directories
creation or revision date,
etc.), all
can be very important depending on what you are trying to do. For use the'-l' (long) option to the Is plays
many
command. Because
characteristics of each item, only
one item
and the characteristics are displayed across the screen, is shown below, followed by "jhodges."
scription
file
this
you can
the long listing dis-
displayed per
is
in fields.
The
line,
field de-
part of a long listing for the directory
[PERMISSIONS] [LINKS] [OWNER] [SIZE (BYTES) where "PERMISSIONS"
by
of which
]
[
REVISION -DATE] [NAME]
columns. The first column idenmeans that the item is a directory, an T means that it is a link, and a hyphen means that the item is a file. The "REVISION-DATE" columns (three) indicate when the file was last revised, and tifies the file
refers to the first ten
description character.
A
'd'
include the date and the time.
localhost> Is -1 total 354
drwx 3 jhodges drwx 9 jhodges drwxr-xr-x 2 jhodges drwxr-xr-x 2 jhodges -rwx 1 jhodges drwxr-xr-x 31 jhodges drwxr-xr-x 2 jhodges drwx 2 jhodges -rwx 1 jhodges -rwx 1 jhodges -rwx 1 jhodges
The
512 512 512 512
45079 1536 512 512
45056 1
1427
29 20:26 F90 12 21:38 Mail
—
16 15:27 News
24 19:46 README TeX3 .
.
1990 14:53 6 18:45 29 20:28 3
4
bin core csc212 cshrc-cognet
listing is alphabetical, starting
"PERMISSIONS"
discussed later
empty directory (512, system, as this
is
e.g. for
TeX3
.
0"
in this chapter.
News,
(at 2) is
with capitalized items.
field of this listing
at 1) is
the overhead required to set
and the notion
Section 3.6.2. The size of an
always the same for a particular
up a directory. The
45079 associated with approximately 45K (kilo)bytes.
listed in bytes, so the
.
17 17:28 TeX3 21 00:47 Texmacros
actual content of the is
®
10:16 Manual -in-LaTeX
2
Again, as before, the of ownership
is
Jan Jun Jun May Jan Jun Jun Aug Mar May Jan
the size of
size of a file
"README.
3.4 Directories
Command
3.4.2
73
and Directory Manipulation
Wildcards
When you issue a command that might equally apply to many items at the same time, you probably will not want to issue the command for each item individually. Rather, you would like to issue the command once and have it execute the appropriate items.
There are several ways
similarly for
all
this task, all
of which use what are called wildcards.
A
to
accomplish
wildcard matches
strings in specific ways, so including a wildcard in the right position
match one or more items. Consider (*).
The
asterisk will
may want
to
list
localhost> Is -1
the
most general wildcard, the
may
asterisk
match any number of any characters. For example, you
the contents,
current directory.
first
You
and the subcontents of
all
the directories in the
could use the wildcard to do so as follows:
*
In a similar vein,
you may not remember the exact name of a command arcommand execute on any similar item, hop-
gument, but are willing to see the
ing that the one you are looking for will be one of them. For example, suppose
you want
to
directory. If it
is
EPS
an
know
the size of a
file
named "vimoves eps" .
you couldn't remember the name of the file (or, at least, that
it
has a .eps
in
your current
remember that name extension), you could use file
but did
the asterisk wildcard as follows:
localhost> Is -1 *.eps
Note
same command was used in both the previous examples, but it was mentioned that applying the Is comnondirectory would simply echo the name. However, when a wild-
that the
the effect is quite different. Earlier
mand
to a
is applied to a command, the current directory The previous command is actually interpreted as
card or other regular expression is
assumed
as a default.
localhost> Is -1 ./*.eps
By
specifying
as in the
of the
more about
files in the directory.
the question
mark
(?)
Two
it
will
command
to
other wildcards that are generally useful are
and the bracket
fect to the asterisk, except
you are seeking, work on a subset
the characteristics of the items
second example, you have forced the
([ ]).
The question mark has
match any single character.
a similar ef-
74
Chapter 3 The UNIX
System
File
localhost> Is Chi? ChlO: Chll:
Chi 2:
REGULAR SCRIPTS SED
AWK Chl3:
ch-ext csh ch-ext2 .csh helloyou. csh looptest csh
onsince csh phones .csh phonesp.csh simplegoto csh
.
.
Chl4: helloyou. sh looptest sh looptestO sh .
.
simpleif csh simpleswitch. csh
.
.
.
untiltest sh untiltestO sh
simplecase. sh simplegoto. sh simpleif.sh
onsince. sh phones sh phonesp.sh .
In this example, the question
.
.
mark
will
match any single
character, and the
UNIX directory matching this template are the "ChlO." "Chll." "Chl2." "Chl3." and "Chl4" directories, so the listing is of the contents of
entries in the
those directories.
The bracket is used to specify particular character ranges. One range would be all the lowercase letters, or a numeric ranges, such as the following: localhost> Is Ch[0-2] ChO:
Compat212
How2UseBBS
Chi: Ch2:
MH-Stuf
SAMPLES
NEWS
which has a similar
effect as the previous
and "Ch2" directories. In general,
if
command,
you have
to
list
specific
the "ChO." "Chi."
numbers, characters,
you are sure are in the filename, then the bracket is most effechave no idea what characters to use. but know how many charthere are. then the question mark is most effective. In any other case the
and ranges
that
When you
tive.
acters
..sterisk is the
way
to go.
Wildcards can be used on any
amount of time and want
to
effort.
One
look deeply into the
file
last
UNIX command,
and can save a large
director) listing option
system
is
when you you want a
useful
for something. In that case
3.4 Directories
recursive descent into the tree and a listing of what sion the
is
is
found as you go. Recur-
a very powerful tool. In the case of Is. a recursive listing
named
directory will be listed and. for every directory
by the named directory, the command
command
75
and Directory Manipulation
which
will be issued again.
The
means
is
that
organized
effect of the
named in the command. Recursive options exist for many commands, because they work so well on tree-like structures such as directories. In the case of the Is command, the *-R' option is used to perform this task. Consider the subtree for "UNIX/ Ch6" below: recursive
localhost> Is -R Ch6 EXERCISES MAN LATEX NROFF-TROFF Ch6 /EXERCISES: Exercise-6 1-input
PRINT-TOOLS UNIX-MAN1-EX
VGRIND
.
TEXFORMATS TEXINPUTS TEXMACROS
PAPER REAL-PAPER RESUME
COMFILES FOO
every directory under the one
Exercise-6 1-output
.
Ch6/ LATEX: BIB
call is to visit
Ch6/ LATEX/ BIB: bibtemplates .bib
bibtex dvi2ps latex
tex
unixmanual .bib
UNIX/Ch6
called a shallow subtree, because
is
it
organizes other directories,
flat
number of embedded levels are organized under "UNIX/Ch6." A subtree has no embedded directories, and a deep subtree has many embed-
ded
levels.
but a small
UNIX/Ch6
happens
the deepest level has been
You have ever,
how
shown
to be three levels
deep (only one instance
many ways; howyou have seen the command work only on the current directory. This seen
to list the contents of a directory in
need not be the case. The Is command, and any other be issued on any path
in the
fde system.
UNIX command,
For example, suppose
that
you
the directory
localhost> Is /usr/man/manl
1
.
The
results
may
not
about the usase.
can are
home directory, and you want to list the contents "/usr/man/manl." You could type the following:
currently located in your
illegal
at
in the display).
make sense
if
the permissions have a directory "closed." but there
is
nothing
of
76
Chapter 3 The UNIX
System
File
Changing the Current Directory
3.4.3
You now have
the ability to determine
where you
are in the file system
determine the contents and attributes of the current directory.
and
to
you want to list or use the contents of another directory, however, you either need to remember the path to that directory, or you must be able to go there (i.e., change the working directory to that directory). The command to change the current directory is cd, for change directory. The simplest usage of cd is without an
cd
argument. Unlike Is, when
home directory is taken as cd are to move down one
is
If
issued without a directory path, the users'
the default directory.
Two other simple examples of
own file system and to move up one directory into the global file system. Remember that the listing of j hodges had a directory called "TeX3 0." You could list the contents of that direcdirectory into your
.
tory, directly,
by typing:
localhost> Is TeX3.0
you actually wanted to do something with more than one file in that directory, then you would end up typing the same paths over and over again. It would be much easier to simply move to that directory as follows: However,
if
localhost> cd TeX3.0
Suppose
that
you have been working
in
TeX3
for a while
.
and want to
go back and work in your home directory: that is, you want to change the directory back to that directory. Any of the following commands will do exactly the same thing from TeX3 0: .
localhost> localhost> localhost> localhost>
cd cd cd cd $HOME .
The
have the same effect when you are
one (cd
.
.
)
will not
system. This
is
because the
The only time
"
.
"
. .
"
. '
if
it
is
the easiest
lowing example.
directory
way
to
to
you are
parent directory, but you can use
times
below is
if
you
are located in a
the first level in your
own
it
your
in the root directory,
anywhere
move up through
file
the parent of the current directo-
home directory is when you are "below" your home directory. It should be noted
first-level subdirectory .
directory or
can point
"
.
you cannot use 'cd
.
have the same effect as the others
home
directory above your
ry.
commands is that, although they all directory, the third located in the TeX3
difference between these four
else.
the
As
file
in a
that
because root has no
a matter of fact,
some-
system. Consider the fol-
3.4 Directories
localhost> cd
II
and Directory Manipulation
../.
Remember
that
the root directory
Then
the
is
we
command just and then
directory,
are in your
three levels
issued
is
home
directory
when we
up from your home directory changing
to its parent. If
you are located deep
are doing, then this
mand above full
is
is
an excellent
way
Concatenating Unix
all
that
in the file
home
of your
system look-
are but. rather,
what
to navigate. If the slash in the
com-
bothering you. remember that the
path of a directory, and that
and
our examples.
to the parent directory
ing around for things and do not recall (or care) where
you
started, in
directories
"
. .
you
"
will be translated into a
must be separated by
slashes.
Commands
Almost every time you change your current directory you contents of that directory to see what
is
there, or
make
will
want
to list the
sure that your current
indeed, the one that you intended to change to. The reason for the you presumably have some work to do in the new location, and probably want to make sure that the files you want to work on are there. The reason for the second is that, before you make any file modification, you want to guarantee that you are working on the right directory. At first, it is probably of value to type the individual commands and see what they do. But this rapidly becomes tedious, and there are ways to reduce this tedium. One way to directory
is.
first is that
perform the same is
to use
set
command
command
shell,
of tasks, in the same order, but to input them
sequencing.
you may
When you
all at
in a
many commands together by separating them When you do this, each command will be executed sequenstring
by semicolons (:). tially and its results (if any) will be displayed on the terminal screen ever appropriate). The general form is shown below: coiranandl;
once
UNIX commands
are typing
command2; command3 ;
.
(or
wher-
.
it doesn't matter how many commands are sequenced. The semicolon is command terminating character, so this is simply a faster way to type out a number of commands that you wish to execute sequentially. Suppose that you
where a
want
to
change your current working directory and
directory.
You
could use
command
list
the contents of the
sequencing (also called
command
new
concat-
enation) as follows:
localhost> cd TeX3.0;ls
Note
that
no spaces were placed between the semicolons and the com-
mands. This was done for
illustrative purposes.
UNIX
ignores spaces so the
78
Chapter 3 The UNIX
System
File
command below localhost> cd TeX3
.
3.4.4
performs the same tasks:
Is
;
Directory Creation
to move around and list directories is fine and good; however, in own directory, you may want to organize your files in some meaningful way. To do so, you must be able to create new directories. A good method for
Being able your
organizing your directory
is
to
break
it
up
into subdirectories according to
function or type. For example, consider the listing for the directory
jhodges
below:
localhost> Is -F
Manual- in-LaTeX/ News/ README TeX3 * TeX3 / Texmacros/ bin/ core* .
mail-probs* new-net* new-topics* stud212* testl c* texput log tmac/ widget*
csc212* cshrc-cognet* dead. letter
F90/ Mail/
fe*
ilterfile* foo2* forward* ftp. list mail -elsewhere/ f
.
.
.
.
The user jhodges has created directories for "Mail," "Manual- in- LaTeX," "TeX3 0," "bin," and so on. The first directory (Mail) was actually .
is where the MH mailer stores the mail for jhodges. The Manual -in-LaTeX directory contains this manual, formatted in latex format. The "TeX3 0" directory contains the sources and binaries for the 3.0
created by the system, and
.
version of tex, the text formatting language used to write
manual. The "bin" directory contains binary
by jhodges. Jhodges could just as or "Code," or whatever.
ments"
er's directory, the
directory,
easily
files
versions of this
have created directories for "Docu-
more highly organized a ussee when you list their home
In general, the
more subdirectories you will files you will
and the fewer executable
The command
many
and other executables created
see there.
to create a directory (subdirectory is the
same
mkdir. For example, to create a new subdirectory Docs beneath directory jhodges, the command would be
thing
I
is
the current
localhost> mkdir Docs
Now
if
we
list
pear. At this point
the contents of
Docs
is
jhodges
(with Is), then
an empty directory.
there, at least with the present level of expertise,
we want we would
If
Docs
to put
be
will ap-
something
in trouble,
be-
3.4 Directories
79
and Directory Manipulation
cause you have not been introduced to the editors, copying, or moving.
Docs, and under
want, you could create a directory under
3.4.5
Renaming
You do
not have to stick to the
it
is
will be
you
Directories
same directory names
that
any time you may change the name of a directory with the
command
If
that, etc.
you
used to do naming and relocation of directories, and
important to understand
"Documents," you would
its
usage.
To rename
At
start with.
command mv.
a directory, say
This
files,
so
Docs
to
type the following:
localhost> mv Docs Documents
mv command requires two arguments. If you provide only one argument. UNIX will complain and do nothing. When you again list the contents of the directory after issuing the command above, Docs is no longer there and Documents is. As you can
Relocating Directories and Their Contents
3.4.6
The
see. the
mv command is also used to relocate an entire directory. Read the man (i.e.. man mv) for more information; however, an example is pro-
page for mv
vided here. Suppose that you have two directories, "bin" and
Code,"
in
your home directory. Suppose
that all the binaries in
"Code/C-
bin
are
C
bi-
you decide to move them under the Code/C-Code directory. If you want to keep the "bin" name intact, then you would issue the mv comnaries and
mand
as follows.
localhost> mv bin Code/C-Code
Because the under
bin.
then,
last
argument
is
an existing directory,
bin directory
you want to relocate and change the name of bin instead, you could type the following:
C-Code.
If
is
moved be C-
to
localhost> mv bin Code/C-Code/C-bin
and
Because the
last
relocate the
bin
3.4.7
You
argument
in the
path
is
a
new name. UNIX
will
rename
directory.
Directory Removal
will
sometimes move the contents of a directory without doing anything and then remove the directory. You can remove a direc-
to the directory itself,
tory with the
rmdir command.
your home directory called
Suppose
that
"bin/C-bin" and
you have a bunch of
files
decide that they should
all
off
be
80
Chapter 3 The UNIX
bin
in the
localhost> cd -/bin localhost> mv C-bin/* localhost> rmdir C-bin
The
System
File
director}'.
Then you can
issue the following
commands:
.
first
command moves you to the director)' where you want to move the The second command moves all of the contents of C-bin
contents of C-bin.
Note the use of the wildcard (*)
to the current directory.
the
C-bin
The
directory.
third
command removes
denote any item in
to
Two
the directory.
errors
can be made when trying to remove directories: (1) the directory may not be empty, and (2) the entity may not be a directory'. Consider the first case of trying to delete the
nonempty
director}' '"foo"":
localhost> rmdir Misc/foo rmdir: remove Misc/foo: Directory not empty-
In this
when
example, the directory "foo" has
command
the
to
remove
issued.
it is
at least
one
The following
file
remaining
action
in
it
taken:
is
localhost> rm Misc/f oo/f ooblah remove Misc/foo/f ooblah? y
rm:
here
rm has been aliased to
'rm -i." meaning interrogate the user before per-
forming the action, and the user provides an affirmative response rm, mv. and
cp
gates the user
(file
(i.e..
asks for verification) before executing the
can be useful when deleting or replacing
chance
to think
'rm - i
'
if
is
used
Chapter to
It
because
Many
should be noted that the
it
it is
5.
users simply alias
rm command is many
file
consider the second case, where the
rm to
actually
institutions,
aliased to the interrogative version for this reason.
Now
remove a
command. This
gives you a second
not used with the interrogative option, and at
including ours, that in
files
over what you are going to do.
in their login shell.
dangerous
to the query,
copy), can each be used with a *-i" option that interro-
More on
command rmdir
instead of a director}
localhost> rmdir Misc/foo/fooblah rmdir: Misc/f oc f ooblah: Not a directory
where the correct action
rmdir on
1.
The
string.
asterisk
is
a
to take
is
to use the
rm command on
files
or to issue
a directory.
nanx completion argument
in regular
expressions that matches an\ character
For more information on regular expressions see Chapter
12.
3.5 Files
3.5
Files
and
File own
file
important
is
tion,
we
ries.
That
files.
look is.
UNIX
the
file
system
system within the global
navigate through the zation
File
81
Manipulation
Manipulation
The organization of of your
and
is
at the
file
that
our use of the computer
manipulation of
files in the
to create,
is
the organization
is
system, and so
system. However, the reason that
every user must be able
The commands
important, as
is
file
is
file
based on
same manner
your
ability to
system organi-
files. In this
sec-
as with directo-
rename, relocate, and remove
for these tasks will be presented in reverse order to the
presentation of directory manipulation, because the last example presented for
removing directories can also be applied File
to file removal:
removal
File relocation
File
renaming
File
copy
Locating
files
Looking
at file
Locating
commands
File
3.5.1
As
a user,
ry.
You
contents
Removal
you will constantly be adding and removing files from your directoadd files by editing new files, saving edited files with new names, and directing the output of programs to new files. We have not covered any of these items yet. To remove a file, however, you will use the rm command, rm stands for remove, and can be used to eliminate single files or entire directories. For example, suppose that, instead of moving the contents of the directory "C-bin" to the directory named "bin." above, you wanted to delete the files and the directory altogether. You could use the rm command first on the files, and then remove the directory with rmdir. as follows: will
localhost> cd -/bin localhost> rm C-bin/* localhost> rmdir C-bin
Again,
we have used
the wildcard (*) to denote
directory. Alternatively, there
contents of
mand
C-bin
is
an
rm
and the directory
will be issued,
all
itself.
Recursively means that the com-
by the system, on the contents of C-bin, and,
the contents are directories, on them, and so forth until the
executed on every directory and issued as
shown subsequently.
file
"C-bin"
items in the
option that recursively removes the
beneath
C-bin. The
if
command
option
is
some of
has been
'-r' and
is
82
Chapter 3 The UNIX
localhost>
nti
File
System
-r C-bin
The recursive option cp and mv. File
3.5.2 File
the
number of commands,
particularly
Renaming
renaming
identical to directory renaming.
is
mv command,
get 1"
available on a
is
"widget"
such as
You can rename
in the directory
jhodges
a
file
to
with
"wid-
as follows:
localhost> mv widget widgetl
It
should be noted
no longer
you now now do
a directory listing,
"widget"
"widgetl."
File Relocation
3.5.3
File relocation is
that, if
exists except as
also identical to directory relocation. If the second argument
is
a directory, then the
new name, then with a new name, a
ties is illustrated
moved
file is
the file
is
then the
to the directory. If the
renamed.
file is
If the
second argument
second argument
moved and renamed. Each
is
is
a path ending
of these possibili-
below:
localhost> mv widget bin localhost> mv widget bin/widgetl
named widget is moved to the bin directory. named widget is moved to the bin directory and
In the first case, the file
In the
second case, the
renamed
to
relocation
with the
mv
widgetl.
that if
new
Finally, the
ment
It
Like the
mv command
in a list
files
rm command, mv
will first ask the user if
its
is
has an interrogative option
(i.e.,
he wants to overwrite the existing If the last
file.
argu-
a directory, then all of the items but the last are
named by
the
last.
The arguments can be a mixture to rename many files
and directories. This mechanism cannot be used
and directories, however.
file
contents will be overwritten
can have more than two arguments.
of arguments
relocated to the directory
of
should be noted, in the cases of file renaming and
the second file already exists,
file.
-i) which
file
3.5 Files
3.5.4
is
performed with the
83
Manipulation
cp command, which
and a new name. For example,
a file to be copied
widget
File
Copy
File
copying
File
and
and then
if
takes
you wanted
two arguments: to copy the file
one of the copies, you could type the following:
edit
localhost> cp widget widget-new
Now you would have names, and you could
two
about loss of the original
moving let's
copy
the
if
files
edit the
with the same contents, only with different
"widget -new"
one named
file's contents. File
the second
argument
is
without worrying
copy also performs the task of
a path and a name. For example,
widget and edit the copy in the directory directory new-files exists, you could type
say that you want to copy
"new-files." Given
that the
the following:
localhost> cp widget new-files/widget-new t
new name
directory
In this case,
widget
continues to exist in the current directory, and a
widget -new, has been widget-new already existed,
copy of widget, called
created under the
files
directory. If
it
written.
By
new
new-
would have been over-
using the - i option, with cp, you can have the system interrogate '
'
file. It should be noted that, when you you you are experimenting with files, it is generally useful to make a copy of the original before you begin, rename it something obvious like "file, orig,"
are about to
if
copy over another
and then continue. The copy to
command can
also be used to
copy directories and
copy them recursively using the '-r' option.
3.5.5 It is
Locating Files in the File System
not at
name of a
all
uncommon
file
you to forget where you put a file, or to forget the remember something in the file. You still need to before you can execute any commands that use it.
for
but be able to
be able to locate the
file
There are two commands
find and the grep
that
you
will find invaluable in
performing these
grep/fgrep/egrep. Given that you can locate a file, there are often cases when you will want to compare files for their differences. In such cases, you will want to use the command di ff. tasks:
Locating a File by
family:
Name
or Characteristics
Altogether too often you forget where you propriate to file
copy every
file
into
last
saw
a
file.
Because
it
is
not ap-
your home directory, and because your
system could be extensive, you need a
command
that will search
own
any por-
84
Chapter 3 The UNIX
System
File
tion of the file
system and return
UNIX command find command
shown
is
is
match your search criterion. The utility. The syntax for the find
files that
the file-search
next.
find [pathname-list] [expressions] where the "pathname- list" can be one or more paths from which to start the search, and expressions represent the command options. For example, suppose that you have a file in your home directory called "widget." You could
find can
first try to
see
directory.
You would
localhost> find
.
if
locate
are located in
your home
type the following:
-name widget -print
In this example, the dot
which file
widget when you
is
(
the current directory.
.
represents the starting point for the search,
)
The '-name' option
name" You
with the following
is
used to say "look for a
could use regular expressions
in this term,
but wildcards (and any special characters) would have to be escaped. This
means
that a backslash (\)
*widget would
\
bols must be escaped in
command and
that
would have to prefix file which ended
search for any
find
widget).
-name
Special sym-
because they have special meaning within the
meaning has
The '-print' option
the wildcard (e.g., in
to
be ignored to use the symbol as a wildcard.
specifies that any successes to the
find
search should
be displayed with their path from the starting location. The output of this com-
mand
looks as follows:
/widget localhost> .
Note directory.
that the path is
Now
Change your
" .
/,"
meaning
that
widget was found
in the current
try the
same command.
directory to the root directory and execute the
find command
try to
move
to another location
and
again:
localhost> find
.
-name \*widget -print
/usr/cpl/aday > find cannot open < /usr/cpl/tests2 > find cannot open < /usr/cpl/vba!077 > find cannot open < /usr/f 1/ jhodges/widget /usr/f 1/moortgat/bin find cannot open < /usr/f 1/swilson/Mail find cannot open < /usr/f 2/ghammer > find cannot open < .
.
.
(T)
.
>
.
>
.
3.5 Files
The ries are
we
file
are looking for
flagged by find.
the root directory. Also,
readable
(i.e.,
path of the
You can
/
noted with
Be prepared
you
will see
(1).
to wait a
while
file is
85
Manipulation
File
Note
that unreadable directoif
you
find from
try a
"cannot open" messages
you do not have read permissions)
directory.
Note
for any unthat the full
was the root directory. same command without having to change directories, UNIX commands, will allow an absolute path.
given, because the starting location
also issue the
because find,
localhost> find
is
and
like all
-name \*widget -print
One
somewhat more elaborate example,
final,
is
find to
to use
search for
meet different criteria. To do so, you must be able to select an item that meets consecutive criterion (logical AND), and you must be able to select an item that meets one or more in a grouping of criterion (logical OR). In files that
find, unless otherwise
stated, all criteria
you provide
for item selection are
considered as conjunctive. This means that the item must satisfy
all
the crite-
(AND). To select one of many, you have to use some roundabout methods, as shown below. Also, I have been using the -print option, but, in this next example. I will delete any files that are located in the " / tmp" directory and are owned by the user jhodges. To do so, I must be able to execute a command rion
on the selected
localhost> find /tmp
set
of items:
-user jhodges -atime
\(
In this example, the f
-o -mtime 2 \)
!2
ind command is
the user has perhaps been putting
some work
porary directory that he wants to delete.
match the
issued on the
"
-exec rm
/
tmp"
{}
directory, so
in the publically available
When you want
\;
tem-
to select items that
logical value (true or false) of an expression, then
you should en-
close that expression in parentheses, but you must escape the parentheses as
have shown above.
I
have used the "-atime" option with a value of
" !
I
2" to
which have not been accessed in 2 days. I have also used the "-mtime" option to select items that have been modified in the last "2" indicate that any files
days.
Any
files that satisfy (i.e..
o" option, for OR), sion.
I
AND are owned by
have then used the "-exec" option
mand. The the
match) one of these two criterion (using the "-
-exec
"
} {
"
expand
jhodges,
satisfy the overall expres-
to delete the files with the
"rm" com-
to the current directory so that the items are found,
option must be terminated with an escaped semicolon (\
; ).
and
86
Chapter 3 The UNIX
System
File
There are many additional options for find, as detailed below:
Table 3.3:
find Options
reference
NAME matches the current filename
-name NAME
True
if
-perm XYZ
»
True
if the file
permission flags match the octal number
XYZ precise-
iy
FOO
-user F00
True
if
the file belongs to
-group GRP
True
if
the file belongs to the group
-atime AT
True
if the file
-mtime MT
True
if
the file has
been modified
-type TYP
True
if
the item
the
rectory
is
d.
has been accessed in
is
and a link
same type is
in
as
GRP
AT days
MT days TYP:
a file.
f. is
the default, a di-
1
-print
Always
true: display the current
pathname
-Is
Always
true; display the current
pathname and associated
-exec
statistices
Execute the following command and string: must be terminated with an escaped semicolon (:) for example.
—
find removes
Used
all files
-name foo -exec rm
.
below
.
{}
\;
named foo
to construct expressions using disjunction
Locating Files by Content The use of find requires that at least part of the original file's name be remembered. When you cannot remember any part of the name of a file, it is not prudent to try repeatedly with various different names simply because the find command is dreadfully inefficient. Instead, you can use a string search command, egrep. grep, or f grep to find the contents of a file. All three utilities let the user find strings within files without knowing the name of the file, and without editing the file. The difference between the grep famil) of
how much versatility matched, egrep allows the
commands to be
is
wait on discussing sions,
it
until
the
Chapter
and f grep requires fixed
command
use of 12.
full
allows
in
defining the pattern
regular expressions, so
grep grep
strings, f
The S) max tor the use of same. They all require a pattern
is
the fastest, and
egrep
commands show n
these
fgrep)
to search for
the
will
allows limited regular expres-
potentially quite slow. is
we
(
and
at least
\\
is
uh
one
3.5 Files
location to look for the pattern
(i.e..
and
File
Manipulation
87
two arguments):
fgrep [options] expression [files] where the "options"
are tabulated below, the
"expression"
is
expression including the string desired (or a string with fgrep). and a
is
of
list
"widget"
you wish
files
to search.
a regular
"files"
For example, consider the fragment of
below:
localhost> fgrep always widget The questions *always* expect the shortest answer. If there is a
fgrep
returns the string
if
Most often you use fgrep on
the file
all
name
is
given. This
the files in a directory,
is
a trivial case.
and sometimes even
multiple directories. In these cases, both the path and filename, followed by the
content of the line in which the string occurs, are
you
are looking for a function definition for
You
rectory.
could
the the file path
list
and
listed.
For example, suppose
"md create" :
line in
which
in a
program
di-
the string appears as
follows:
localhost> pwd /usr / f 1 / j hodges /code / c 1 / S IMULATOR localhost> fgrep md: create */*.cl
rep/demon-processes t (set *ep* (md: create '*ep*)) rep/plnew. t (set *ep* (md:create '*ep*)) rep/p2new. t (set *ep* (md: create '*ep*)) rep/processesl t (set *ep* (md: create **ep*)) rep/processes2 t (set *ep* (md: create '*ep*)) utils/fixmd. t (define (md:create name) find: cannot open < /usr/f2/ghammer > .
:
;
:
;
.
:
;
.
:
;
:
;
.
:
.
In this case,
fgrep
is
being applied to two directory levels (*/*), using
"md: create" in all the below files ending with "SIMULATOR." Each recl" in all the directories sult is written on a separate line, with the path to the file first and the context (i.e.. the line in the file) in which the string is found on the right
the asterisk wildcard,
so
it
will search for the string
"
.
1
.
Note
sions.
that the use of the asterisk as a
The wildcard can be used
in
wildcard
fgrep
in
UNIX
is
different
from
its
use in regular expres-
even though other regular expression operators cannot.
88
Chapter 3 The UNIX
Table
System
File
grep/f grep/egrep Options
3.4:
reference
-X
Display only lines exactly matched (f grep only)
-c
Display only a count of matching lines
-V
Display
-i
Ignore uppercase and lowercase distinction during comparisons
-1
List only
-n
Precede each line by
Some
all
lines that
don't match
names of files with matching its
of the character options for using
relative line
grep/egrep
number
are
grep/egrep Regular expression
Table 3.5:
lines in the file
shown below:
operators
A
Matches
the beginning of a line
$
Matches
the
•
Matches any character
*
Matches zero or more regular expression instances
+
Matches one or more regular expression instances
Comparing Whenever
Files
a data or source
file is
which.
file is
used regularly,
made, and the user
One way
what has changed, only compare files by content, and is the user
command
syntax
is
it
will
is
often the case that modi-
want
to
check on which ver-
to keep track of versions
modification date (see long directory
changes. The
line
by Content
fications to the file will be
sion of the
end of a
that
is
through the
file's
but this mechanism will not tell The dif f command is used to means to locate and keep track of
listing), it
has.
a useful
as follows:
diff [options] filel file2 where "f ile2" can be a directory, or both "filel" and "f ile2" can be directories. In the case where filel and f ile2 are both regular files, the) arc
3.5 Files
and
File
89
Manipulation
compared line by line, and the lines where they differ are displayed. In the case where f ile2 is a directory, f ilel is compared with a like-named file in the directory named by file2. In the case where both filel and file2 are directory names, then
dif f
dif f that
and then
will first sort the directories alphabetically
do a content comparison on each
file.
displays are fairly simple to read.
A block of lines will be taken and
block of line numbers will be displayed. Then the lines
in
filel
will be
Then a dashed line will be displayed, followed by the corresponding lines in f ile2. preceded by a right angle bracket > Below is an example on two short sample files (which are comdisplayed, preceded by a
left
angle bracket ( diff filediffl filediff2 line numbers 2c2,3 -* < that block of line numbers will be displayed. Then the lines in filel will -*
separator
> >
that block of LINE numbers will be displayed. Then the lines in filel will
In the line
number reference of the
display response, the line number range separated by a "c." In the example, the first "2" refers to the line
in
each
in
filediffl.
file is
the options to the
while the "2,3" refers to the lines in f iledif f 2.
diff command
a suitable reference or the
Table 3.6:
diff Options
man
are tabulated below.
in
pages.:
reference
-b
Ignores trailing blanks and strings of blanks
-i
Ignores the case of letters ("A' will be equivalent to 'a'
-w
Ignores whitespace such as blanks and tabs equivalent to 'if (a==b)
3.5.6
A few of
More can be found
.
"if
(
a == b
) '
will
be
Displaying File Contents
at the contents of a file is often enough to tell whether it is the you are looking for. You can use grep or f grep if you remember the contents of a file. However, if you do not remember the name or the contents,
Taking a look file
then you need tents of files.
some general
Both
more
(i.e..
and
cat
brute force)
mechanism
can be used to display
for listing the con-
file
contents.
90
Chapter 3 The UNIX
File
System
Listing a File to Standard
Output by Page
more is a command that takes a file (or list of files) as its argument(s) the contents of the file at the standard output, and
more
displays one screenful at a time. For
characteristics (as described in Chapter 5,
more
is
called a
to function properly, the terminal
page 143) must be
displays a page on the screen, and then asks the user
desired.
You can
, or you can play one
file at
continue to page through the exit
by typing a
'q.'
When
and lists pager because it
a
file
by
file list is
if
set correctly.
more display
given,
more
is
key
hitting the space
will dis-
a time.
localhost> more ftp. list
more
supports
string with the (/)
some
you are looking for a then you could do a search using slash
string search capabilities. If
word "examples"
in
it,
followed by a regular expression for the desired
string:
/examples
and
more would
search for the next occurrence of
place that line near the top of the current screen.
localhost> grep mdrcreate */*.cl
I
|
is
too long to
in the file
can also use
when a directory show on one page:
a process pipe (see Chapter 5, page 159)
UNIX command output
examples
You
more
listing or other
more
have not shown the output for these commands simply because
of output has been
shown
in
Table
3.7.
and
with
shown
already.
Some
useful options for using
this type
more
are
3.5 Files
Table 3.7:
more Options
-n
Specifies
-d
The user quit" at 'h'
and
Manipulation
File
91
reference
how many
lines to display per
page
be prompted with the message "Press space to continue, 'q' to the end of each screenful; illegal user input is responded to with "Press will
for instructions"
number N
+N
Starts the display at line
+ /RE
Starts the display
iN
Continue but redefine the page size
is
Skip forward i lines and continue
if
Skip forward i pages and continue
ib
Skip backward i pages and continue
two
lines
above the
a file
a
then you might simply
file,
is
short or
to
be
N
RE
lines
Output
Listing a File to Standard
When
line containing the regular expression
you do not
really care to look closely at the contents of
dump
the contents of the file to the screen using
cat. cat can be used the same way as more:
localhost> cat .login
This turn the files,
command will scroll through the entire contents of " login" and reuser to the command line, cat can be used very effectively on short .
because
on larger
Two
files
it
quickly shows an entire
unless
it is
used with a
additional programs,
tions of a file or
head
file,
but should be used with caution
command
like
head
or
and tail, can be used
command response. For example,
tail. to display por-
consider the following com-
mand: localhost> tail ftp. list
which would display the started at
any location
ginning of the
file
last
in the
10 lines of the file,
and
or the end of the
it
file.
file
"ftp list." tail can be .
can be started with respect to the be-
92
Chapter 3 The UNIX
File
System
localhost> tail +20 ftp. list
will start displaying lines 2
mand been the
file.
lines
Other options are available, see (man
performs a similar function on the tion
first
"count"
specified, the first
is
from the beginning of the
file.
Had the com-
issued with a -2 0, the display would have begun from the end of
tail). The
10 lines of a
file or, if
head command the
"count" op-
lines.
localhost> head -20 ftp. list
Locating
3.5.7
Commands and Documentation
Sometimes it is difficult to find where a command or executable file resides. If you cannot find a command, then you cannot execute it, because it must be in your current directory,
you
to execute
in
your execution path, or linked
There are two commands
it.
or information relating to
to
your directory for
commands commands whose name you know: which and that are
used to locate
whereis. Locating a Program File The which command
is
used
(in the
C
shell) to find
execution path, and can be useful in determining a particular
name
command command is
exist, to the
the
and what
exist
that is
their ordering
is.
program
how many
files
within your
executable
which traces aliases,
executed with the alias
is
files if
of
they
invoked. The syntax for
which filename and an example
is
shown below
localhost> which tex /usr/f 1/ jhciges/TeX3 0/usr/ local /bin /tex .
In this response, the path
argument identify
is
displayed,
which executable
command
behavior.
of the
which is
first
executable found which matches the
can be used, along with the path variable, to
being invoked
when you
get bizarre
program or
3.6 Filesystem Security
Locating a Program Source or The where is command is used to same way that which is used, but and can be used
in either
low, although usually the
command command
Man
Page
find source, binary, and is
manual
files in the
not limited to the user's execution path
shell. is
93
The command syntax is shown file name alone:
be-
issued with the
whereis [-sbm] filename "
sbm]
where the
first field
als alone.
There are other option
[
" is
(man whereis). Consider
used to search for sources, binaries, or manufields, that
the use of
can be looked up
whereis
in the
man page
on the previous example:
localhost> whereis tex /usr/bin/tex /usr/lib/tex
tex:
Notice that the locations of both the executable and
by whereis. and not searched,
its
source
files is
given
that the previous directory, in the user's execution path,
whereis
was
only looks in standard directories for items.
3.6 Filesystem Security The or
last section
system.
file
of this chapter
When
the 'Is
concerned with the security of your directory -1' command was demonstrated, the first few
is
columns contained several items whose descriptions were These items addressed the permissions and ownership of
left
unexplained.
a directory or the
items in the directory. As a user, you own your own directory. This means that you control who can search your directory, who owns the files or subdirectories in your directory, and who can read, write, or execute files within your directory. It is important that every user be aware of the commands that control access to their directory, and what access control means. In this section, you will learn how to maintain and modify your file system. In particular, the fol-
lowing topics will be addressed: •
Account access
•
Access permissions
•
Ownership
•
Director)'
and
file
creation
masks
94
Chapter 3 The UNIX
File
System
Account Access
3.6.1
The introductory chapter of this manual introduced you control mechanism you have available. If someone is your
rights to
files,
to the
primary access
trying to obtain full
then they must obtain your password. If you regularly
change your password with passwd. then the chance of account break-ins dramatically reduced.
is
important that you not choose passwords that are re-
It is
remember, because they are the easiest codes to break. Passwords same as your name, passwords with a small number of characters, and passwords with commonly used terms, products, and famous people or places are generally considered easy to break. You can selectively allow owner access to your account without revealing your password. You do this by adding machine/name pairs to a file called " rhosts." This will be discussed more ally easy to that are the
.
in
Chapter
cause
it
count
full
7.
page 249. but the
potentially allows
.
rhosts
file is
considered a security risk be-
someone who has compromised another usefs
ac-
access to yours.
Access Permissions
3.6.2
The type of access
that a user has to a file or directory
cess permissions associated with a
can type the 'Is
-1' for a
file
is
determined by the ac-
To see what permissions
file.
a
file has.
you
or directory, as follows:
localhost> Is -1 total 356 -rwx
1
-rw-r— r--
1
drwxr-xr-x -rwx -rwx
2 1 1
jhodges jhodges jhodges jhodges jhodges
656 482 1024 2693 57979
Mar 4 15:05 Jun 17 18:25 Jun 20 10:19 Jul 14 15:10 Jun 8 16:59
testl.c texput.log tmac widget widgetl
(D
L permissions
owner
The
resulting long file and directory listing follows the following format:
[PERMISSIONS] [LINKS] [OWNER] [SIZE (BYTES
The
)
]
[
REVISION -DATE] [NAME]
The permissions of the partial listing are shown in first column identifies the type of permission of an
tifies the tities the
three
item as a
file,
the first ten columns.
a "d" identifies the item as a director]
item as a link.
The next nine columns
columns each. Each
set
in
.
hyphen iden-
and an
1" iden-
are divided into three sets of
stands for one of three user
and other. Permissions are always written
A
item:
t\
pes:
owner, group.
order of owner, group, and other.
95
3.6 Filesystem Security
The
fourth field in the listing identifies the
owner of
a
file
or directory, and
only that user (or a system administrator) can modify the permissions on the item.
Three types of permissions are available: read, write, and execute. Like the user types, permission types are always written in the
same
read, write, ex-
Read permission comes first and is indicated with an "r." An r in column of a set means that this user type (i.e.. owner, group, or other) has permission to read the file or directory. Write permission comes next and is indicated with a "w." A w in the second column of a set means that this user type has permission to write to. modify, or remove the file or directory. Finally, execute permission is indicated with an "x." An x in the third column of a set means that this user type has permission to execute the file or directory. Write permission on a directory means that the user type can create and delete dirececute order. the first
and items
tories
means
designated directory. Execute permissions for a directory
in a
that a user type
directory.
According
list
the contents of or change to the location of the
to this notation, the
owner has
a directory, the else has read
can
read, write,
and execute permission.
item labeled "tmac." above
When
a field in any set
with a hyphen), access permissions of that type
filled
(at 1). is
and execute permission, and everyone
is
is
empty
(i.e..
denied for that entity.
example above, the owner has full permissions on the files "test 1 c." "widget." and "widget 1." but has closed out all access to other users, because no entries exist in any columns after those for the owner. In the
.
Changing Access Permissions or directory. As owner you can change access permissions. The command to do so is chmod. which means "change the mode of a file." chmod can be issued with
There are two ways of a
to
modify the permissions on a
file,
mnemonic (symbolic)
Symbolic With
the
File
or binary arguments.
Mode Change
mnemonic arguments,
a "u"
the group, an "o" for other users, is
file
used
to
add a permission and a
used to designate the owner, a "g" for
is
and an "-"
is
"a" is
used to indicate
all
users.
A
"+"
used to remove permission. For exam-
ple:
localhost> chmod g-rwx,o-rwx tmac
will
remove
users.
all
permissions to the directory "tmac" for
all
"group" and "other"
96
Chapter 3 The UNIX
Binary
File
File
System
Mode Change
Alternatively, a binary shorthand can also be used with this
works,
we
need a brief digression
will
Binary (base 2)
is
we count
using only the digits
numbers
are very popular
used
ing or
in
in
reach 9,
we 1
decimal,
1,
how
and
1
we
first
,9.
.
In binary,
Decimal
10 fingers. Binary
can indicate that electric current
To
and cycle through the
begin with
second column
..
(to the left).
(rightmost)
first
When we
two columns and put
a
1
is
flow-
write the natural
When we
digits.
column
we
reach 99.
in the third
and
at
represent
column
(to the
and so on.
In binary, to represent the natural
through the at
using the digits 0,1,2,
called bits for "binary digits.*'
represent 10 by restarting the
in the
100 by restarting the left),
and
among humans because we have
computers because
numbers place a
we count
not flowing, or the direction of a magnetic field.
is
see
an alternative way, to the decimal (base 10) way, of rep-
resenting numbers. In decimal,
is
chmod. To
about binary numbers.
to talk
bits.
and place a
When we reach 1
in the
1
,
we
numbers, we begin with
0.
represent 2 by restarting the
second column
(i.e.,
2 =
10).
When we
then cycle
first
column
reach 7,
we
two columns and put a 1 in the third column (i.e., 8 = 100). and so on. The same can be done with the octal (base 8) and hexadecimal (base 16) numbering systems. In the octal system, we begin with and cycle through the digits until we reach 7. Using these schemes, we have the following representation of decimal, binary and octal numbers (Table 3.8). represent 8 by restarting the
first
3.6 Filesystem Security
Decimal, binary, and octal equivalents
Table 3.8:
Binary
Decimal
r used for permissions
V^
l
1
2
10
2
3
11
3
4
100
4
5
101
w
5
(T)
6
110
7
111
7
8
1000
10
9
1001
11
10
1010
12
11
13
12
1011 1100
13
1101
15
14
1110
16
15
1111
17
16
10000
20
set
6
14
permissions are grouped into three sets of
earlier, the
Each permission within a 1
Octal
l
As mentioned a
97
may
be thought of as a
3.
(permission denied) or
(permission granted). For example, "r-x" would be 101 in binary and
equivalent to 5 in decimal and octal (noted
used with
chmod
at 1).
This octal/binary code can be
to set permissions. For example:
localhost> chmod 750 filename
would
set the
permissions
to:
rwxr-x
which breaks down
to the binary
and octal representations as follows:
owner:
111
7
group:
101
5
other:
000
The first set (owner) is given the permission to read, write, and execute. The second set (group) is given permission to read and execute, and everyone else (other) has
ships sions,
no permissions
between type,
level,
at all. Fig. 3.2
graphically depicts the relation-
and numerical representations for 744
which give the owner
full rights,
and anyone else read
file
rights.
permis-
98
Chapter 3 The UNIX
Figure 3.2:
7
4
111 10
10
w x
Own
System
Octal and binary codes for
4
r
File
r -
- r
Grp
file
permissions
OCTAL
BINARY
-
TYPE
-
Oth
LEVEL
Below is a session where the permissions on the file "blah" are modified "group" and "other" through their possible values. Note the sequencing of UNIX commands using semicolon. Also note the use of wildcards in re-executing previous commands with minor modifications. The listing is perfor
formed using the
'1'
sions change and
how
that the
it is
localhost> Is -IF blah -rw-r--r— 1 jhodges 44 Jul li localhost> chmod 700 blah; Is -rwx 1 jhodges 44 Jul li localhost> chmod 711 blah; Is -rwx— x--x 1 jhodges 44 Jul li localhost> chmod 722 blah; Is -rwx—w w- 1 jhodges 44 Jul li localhost> chmod 733 blah; Is -rwx-wx-wx 1 jhodges 44 Jul li localhost> chmod 744 blah; Is -rwxr r-- 1 jhodges 44 Jul li localhost> chmod 7 55 blah; Is -rwxr— xr-x 1 jhodges 44 Jul U localhost> chmod 7 66 blah; Is -rwxrw— rw— 1 jhodges 44 Jul U localhost> chmod 777 blah; Is -rwx rwx rwx 1 jhodges 44 Jul li
—
—
File
rights,
an executable
When
an asterisk
is
all
placed on the listing of the
file
and Directory Ownership the permissions
column
in a
in the
directory
'Is
in this
-1'
listing tells
how
manual. The third col-
who the item's owner is. In the example providare owned by "jhodges." If enabled, an owner
the response identifies
items
permis-
file.
links the item has but will not be discussed
ed,
how
permissions are changed so
13:46 blah -IF blah 13:46 blah* -IF blah 13:46 blah* -IF blah 13:46 blah* -IF blah 13:46 blah* -IF blah 13:46 blah* -IF blah 13:46 blah* -IF blah 13:46 blah* -IF blah 13:46 blah*
The column following
many umn in
'F' options so that the reader will see
the display changes.
owner has execute
whether or not
3.6.3
and
of a directory or the
file
99
and Sample Quiz Questions
3.7 Exercises
can change the owner of the item
if
they so desire using
chown command. For example, if we want the user "geisman" cd to the "jhodges" directory and modify the file "widget,"
able to
out giving everyone access to the
file,
then
we can
to
be
with-
type the following:
localhost> chown geisman widget
geisman is the user ID and widget is the item that will this point, geisman owns the file and jhodges does not.
where At
3.6.4
When
a
File
and Directory Creation Masks
new
user account
is
generated, the permissions of directories and files
created by the user have a default setting.
mask, because
be modified.
The
setting
is
called a creation
defines the permissions that users will not be given rather
it
than the other way around (as with chmod. which works after a file exists). As owner of your directory, you can set the default mask at any time using the command umask. The modification of this default, in the login scripts, is part of Chapter 5. umask uses the same permissions encoding scheme as chmod.
except that
it
clears permissions from the full permissions set instead of adding
them from the
null permissions set.
That
is,
the
umask setting is the octal com-
plement of the
chmod
umask
022, which masks out no permissions
value
is
file
permissions created. For example, the default (i.e..
retains all permis-
and masks out write permissions for "group" and "oththey have read and execute permissions). This mask corre-
sions) for the owner,
er" users sponds
(i.e..
to a
755 default permission on user-created
complement of 7
is 0,
and the complement of 5
is
files
and
The number
directories.
2 (based on an octal
system).
3.7
Exercises and Sample Quiz Questions 3.7.1
Exercises
Exercise
1
Exercise 2 Exercise 3
Find the pathname of your
Find the pathname of the
Use Is
to
list
home
directory.
TeX macros
(inputs) directory.
the contents of the directory
"/usr/ local /lib." Try
various Is options (a. la. t. F. R, etc.) and note their effect. Deter-
mine how
to use Is to
list all files
and subdirectories of
all
subdirecto-
ries.
Exercise 4
Create a subdirectory of your naries.*'
1
.
Public execution permission for
Change your
chown
is
home
directory. Call
current directory to be
it
"bin." for
bin.
increasingly being disabled for security reasons.
"'bi-
1
00
Chapter 3 The UNIX
Exercise 5
File
System
Make a copy of "-/ cshrc" in your home directory. Call it "csh. compare." Be very careful not to overwrite the existing " cshrc" file as you are doing this. Ask for help if necessary, but do not let the .
.
person assisting you do the typing. This
is
a normal and necessary task
every user must perform, and you should learn the file
is
in
your directory,
list its
it
permissions.
once and for
all.
Once
Who owns the new file,
and what permissions do they have? Exercise 6
3.7.2
Problem
Change permissions on "csh. compare"
to
-rwx
.
Sample Quiz Problems 1
is the UNIX command to locate and print the names of below your home directory which contain "foobar" names?
What
at
Problem 2
or
What UNIX command(s) would you use
"~/Docs/Letters/TeX"
tory
to recursively
all files
in their
copy the
direc-
"~/Docs/Letters/TeX-
to
Old"? Problem
3
What UNIX command and
Problem 4
is
used to
list
the entire contents of a directory
their attributes (file, directory, etc.)?
What
is
the difference
between
which
and
whereis. Give one
ex-
ample of when each would be used.
Problem
5
What UNIX command
is
used to copy a directory called "dir" and
its
contents?
Problem 6
Suppose your userid
is
vcscl999
and
that
your current working
di-
"-/subdirl " List three ways to change your current directory to your home directory with as few keystrokes as possible. Number your answers from fewest keys to most keys. rectory
Problem
7
Show
.
is
the
UNIX command to change the permissions on
current directory ending with
owner, and read and execute Problem
8
Suppose your userid
UNIX command rectory
to
is
"
.
tex"
to read, write,
all files
Name
"/usr/sl/vcscl999/subl/sub2"
four directories off slash
(/)
is
ending with
your current directory, regardless of where
Problem 9
the
for group, and other.
VCSC1999. What
remove
all files in
and execute for
the simplest/shortest "
.
dvi" from
the di-
without changing from
that is?
and what they are used
for.
Chapter 4 File
Introduction and
4.1
Creation and Editing
Scope
By now you should be familiar with the UNIX file system: its organization, how to move around your directory and other directories, and how to manipulate files and directories. It is now important that you be able to move around within a file and manipulate its contents. By creating and editing files, you can produce programs, program data, and papers. In this chapter, we present two editors: (1) the stock tive
on
UNIX
UNIX editor,
platforms,
vi, and
emacs. Both
(2) a versatile
and popular alterna-
editors are full-screen editors;
howev-
emacs is more extensive and takes longer to learn and master than vi. When you have completed this chapter, you should be comfortable using either er,
editor
on a day-to-day
commands another Ch4
to
file into
opposed
basis. This
move around
the
the current
file,
editing your
own
files will
be
to
being able to enter the editor, use
add or modify
use
commands
to local changes, save the file,
for editing can be found in the
someone
amounts
file,
and
text,
to
move
exit the editor.
"UNIX/Ch4" a much more
text around, read
perform global changes as
Some sample
directory, but
you
files
will find that
valuable experiment than using
else's files.
101
102
Chapter 4
Creation and Editing
File
4.2 File Editing An
editable
file is
a
must be able
the user
to
•
Access the
•
Move around
•
Alter the
file
•
Save the
file
Every
were the
first
screen to display a
4.3
editable
file,
same
will not
an editor which
commands to perform may work on many lines of a
time, but only displays one line at a time. Line
editors developed. Full-screen editors use an entire
file,
and allow the user
to
move around on
the screen as a
modern editors are full-screen editors. The only line this manual is sed. which stands for stream editor.
field. All
editor utility presented in it
To modify an
format.
things:
in the file
A line editor is
two-dimensional
but
1
file
a block) at the
file (i.e.,
do four
ASCII
text editor will provide the user with different
these functions.
editors
written in
file
be introduced
until
Chapter
12.
The vi Full-Screen Editor Of the ed,
editors available, the
mail and is
vi
and easy-to-use full-screen
useful,
now
rn. assume
applications, particularly
We
have a basic working knowledge of vi.
to
it
will
to create a file, edit the file to get acquainted with cursor controls
and moving text around, and then save the
mands
one of the most useful, accept-
Many UNIX
editors.
as the default editor unless otherwise directed, so
even important,
vi
use
vi
(visual) editor is
are tabulated below, but see the
A
file.
few of
man page (man
the essential
com-
vi). or [Joy 1980] for
further details.
File Access: Creation or Editing
4.3.1
Before you
move around
tents of the file.
When
When
or alter a
the file exists, access
to files. In
file,
you must
first
a file does not yet exist, access is
gain access to the conis
called file creation.
called file opening. Every editor provides access
vi. the user accesses a
file
by opening
it
with the vi
command,
as
shown below:
vi [filenamel filename2 ...] means
a file
the top of the file
at
the square brackets,
I.
ASCII
is
an
vi
Opening cursor
acr
,' as shown in Table 5.3:
ps Status swap
Table 5.3:
The
value descriptions
field
w
Process
z
Process that
is
killed but not yet
bl
Process that
is
in
>
Process that has exceeded
is
swapped out
removed
core its
soft limit
third letter of the three to four character status sequence indicates
whether a process
is
nice (man nice). large job to run,
by your work.
CPU
running with altered
Nice
is
used when, for example, you have a particularly
and you lower
In addition,
scheduling priority, using
it is
its
priority so that other users are not
traditional to nice
hampered
your jobs when running them
remotely on someone else's console. As an aside, the use of nice follows:
nice
[
-number
command
]
and
[
arguments
]
usually applied as follows:
is
localhost> nice +10 more Exercise-6 1-output .
have omitted the output because
I
third letter in a process's ' ps uxa SZ USER PID %CPU %MEM jhodges 10067 83..0 0..9 530 vcsc2107 10060 13. .5 0, .2 282 homels32 10054 3, .9 0. 3 245 vcsc2096 10038 3, ,5 0, .9 627 vcsc2095 9561 0..8 0, .2 282 10004 0,.6 0, .2 283 vpcsc4 0.,6 0, ,3 268 pbourne 9160 0,,4 0,,0 6 root 255 78 0, ,3 0, ,1 93 root 8629 0, .3 0, ,3 268 j cheung 8562 0, .3 0,,1 106 root vcsc2185 9195 0, .2 0, ,2 290 vcsc2199 9858 0, .2 0..2 282 8571 root 0, ,2 0, ,1 96 vcsc2109 10040 0,.2 0, ,1 62 62 pbourne 10045 0..2 0..1 3473 oracle 0. .1 0, .8 1792 jhodges 10001 0. .1 0..2 236 vcsc2106 9401 0. .1 0..2 249 shahryar 7153 0. .6 345 .1 jchan 10014 0..3 256 .1 homels32 8428 0. .1 .2 250 root 9222 .0 .1 106 172 fbuenafe 8625 .0 .2 9867 azhao .0 .1 30 root 258 .0 .0 39 daemon 129 .0 .2 123 chance 9814 .0 .2 228 root 111 125 .0 .2 goldstei 7187 189 .0 .3 root 83 .3 145 .0
RSS TT STAT 369 71 119 383
qc p8 pe pc
R S S S
64 pb S 75 qb S
137 q7
I
4
7
S
46
7
s
127 q8 s 34 q7 s 80 q9 s 58 P3 I 40 q8 s 24 pa I 22 q7 I 322 7 s 68 qc s 63 q5 I 262 pO s 118 q4 s
®
TIME COMMAND 0: :00 ps uxa 0: :00 vi lexer. :00 bbs unix :00 emacs list.c 0: :01 vi create. :00 vi cost. mod :03 bbs :48 /etc/update :05 /etc/portmap :01 /usr/ local /bin /bbs :02 telnetd :02 vi dirutil.c :00 vi sos.c :01 rlogind :00 page :00 more -d :09 ora_dbwr_gold :00 -csh (csh) :00 -csh (csh) :08
i2
:00
ftp sfsuvm -csh (csh) telnetd learn sh /etc/cron /etc/syslog -csh (csh) /usr/etc/statd kermit /etc/mountd
pe pd P6 p9
s
:01
s
:03
I
:01
I
:00
7
I
:02
7
I
:04
qa
I
:00
7
s
:00
127 q2 I 105 7 s
:12
95 34 70 29 12 83 64 83
155
:00
®
®
®
Several valuable pieces of information can be gleened from the listing
above.
First,
top (item
note that the
1).
ps
process that generated the listing
Also notice that a user
may have many
is
shown
at the
processes scattered
throughout the table. Notice the variety of tasks the users are working on. and
how many processes are sleeping, how many are idle (e.g., item 2), and how many are running. Often you will want to know which process is most recent, so that you can
kill
the newest or oldest process. Notice (at item 3). that the user
jhodges has a shell process running, and that its PID that the ps command just executed has a PID of 1 67
.
mand must have been
issued after the shell
was
is
10 01. Also note
Because the ps com-
created, this
is
an informal
proof that PIDs increase.
None of this may make
sense
at first,
but
all
of
it is
useful in different con-
1
56
Command
Chapter 5 The UNIX
texts.
One
thing
you can
and you may be able
and Process Control
Shell
right
tell
to tell
how
away
is
whether someone
what they are doing. For example, someone who a file (4)
is
likely to
they are using
is
using
kermit
to transfer
be doing so for some time, but cannot be reached while
kermit.
process hangs,
If the user
was run
then
it
has stopped running and something
can create another shell
in the
background so
ground (see Section
5.3.8,
in
which they can do
that they
page 161), then
its
is
accomplished using the
status
is
to
a
may be wrong.
ps, or
if
ps command
can run a
cumstances, the only thing that can be done This
on the system,
Removing Processes
5.3.3 If a
is
long they might be on the system based on
the process in the fore-
can be known. In
remove, or
kill
many cir-
the process.
kill command. The example below
localhost> kill 10001
numbered 100 01. You need to know the process you wish to kill, which is one of the reasons why the ps command is valuable. Two versions of kill are used regularly. The first kills nicely, meaning it removes the process without doing damage to related processes. The second method is called the "blast" method, because it removes the process without any consideration for the side effects that removal may will terminate the process
number of the job
that
produce. The syntax for each
is
shown below.
localhost> kill pid
or localhost> kill -9 pid
You
should always
method has poor
try to use
are running subprocesses, and
pears to be hung. If a blast all
kill
alone,
(or devastating) side effects.
is
it
may
first,
as
sometimes the blasting
For example, many processes
be one of the parent processes that ap-
issued on this process, then
the child processes as well. This
may
it
will
probably
kill
not be something the user wants and
should be carefully considered. Sometimes a machine will appear to be hung
when
a renegade process
is
really using
up many of its resources, or has created machine, if someone can
a block to further processing. Instead of rebooting the still
login remotely, the offending process might be removed. This could po-
tentially save the
I.
Another
common
work of many users and should always be considered
term for stopped processes
is
wedged.
a
first
157
5.3 Shell Control
line of
defense
in
multi-user systems. Refer to Chapter 7 for the network solu-
tions to this kind of problem.
5.3.4
Process Sequencing
In Chapter 3, the notion of command sequencing was introduced. In UNIX, commands may be sequenced by separating them with semicolons. A common sequenced command is to cd to a directory and then Is its contents. One way is to issue the commands separately, as below.
localhost> cd tmac localhost> Is
However, the same
effect can be realized with a single line, as follows:
localhost> cd tmac; Is Another example
is
moving up
directories using the dot-dot
command
with pwd:
localhost> cd ../..; pwd This example moves up two directories and then displays the
name
of the
directory.
5.3.5
Redirecting Process Input or Output
There are two ways and
(2)
modify the I/O behavior of processes, by ( 1 ) redirection piping. Redirection means that a process takes its input from some to
place other than the standard input
some place
(i.e.,
the keyboard), or sends
other than the standard output
(i.e.,
the screen).
signs are used to perform the redirection. Options are
Table
shown
The in
its
output to
>,
< and >>
Table
5.6:
Process redirection options
5.6:
process
file2
Output
process
file2
process >> £ile2
To
illustrate
to
is
filel
file2
appended
I/O redirection, the
Normally the output from cat
is
filel and to f
output to
file2
ile2
cat command
concatenates two
files.
displayed on the screen (standard output).
By
1
DO
Chapter 5 The UNIX
using the
Command
Shell
> symbol with
and Process Control
a filename, cat can send
its
output to a
file.
localhost> Is -F TeX3 .0/ Texmacros/ bin/ blah localhost> cat blah
>
forward* ftp. list mail -elsewhere/ mail-probs* fooblah; Is -F
texput log tmac/ widget* widgetl* .
®
testl c* texput log tmac/ widget*
fooblah -* forward* ftp. list mail -elsewhere/
TeX3 .0/ Texmacros/ bin/ blah*
.
.
The first listing shows that "fooblah" does not exist. After the redirection to "fooblah," another listing is done, and now fooblah exists (item 1). If fooblah had existed already, then it would be overwritten with the contents of blah. All other lines in fooblah would be lost. An example of limited practical value which you can easily do yourself involves the echo command, which
echo
prints
its
arguments on the screen. Try typing the
command
with the arguments shown below.
localhost> echo my home directory is $home my home directory is /usr/f 1/ jhodges This should display the sentence directly. direct the output to a file called
localhost> echo my home directory is $home localhost> more HOME my home directory is /usr/f 1/ jhodges
If
ing
it
Now
>
you now do an Is on your
directory, the file if
it
mand. This
is
file.
That
is,
very useful
Suppose, for example,
my home directory is $home my user execution path is $path
you cat or
has the sentence
The symbol >> can be used of an existing
to
append
the file will be
when
thing, but re-
HOME
wasn't already there), and,
above, you will see that
do the same
"HOME."
in
the
will exist
file,
as
I
(assum-
have done
it.
the output of a
appended with
creating a small
we wanted
"HOME"
more
file
to create a file with
command
to the
the output of the
end
com-
without using an editor.
two
lines
5.3 Shell Control
where the two values in the
shell variabiles
file.
home
and
path
are
expanded out
159
to their true
This can be accomplished by typing the following:
localhost> echo my home directory is $home > HOME localhosc> echo my execution path is $path >> HOME
and. as before.
HOME
can be
listed to
show
that the
append worked:
localhost> more HOME my home directory is /usr/f 1/ jhodges my execution path is /usr/f 1/ jhodges/bin /usr/ucb /bin /usr/bin /usr/bin/mh /usr/local /usr/local/bin /usr/new /usr/hosts .
Process Redirection and Email Redirection can also be used with the standard
UNIX
mailer
(/usr/ucb/
mail). Suppose you have written a file "myf ile" with your favorite editor and you want to sent the file to a friend, "myf riend." on localhost. Instead of entering the mail environment, you can direct the input to the mail message from the file. Consider the sample scenario below: localhost> mail myfriend
fgrep SDRAW < Lisp/*.cl > items localhost> more items -*- Mode: Lisp; Package: SDRAW -*SDRAW - draws cons cell structures. - draws obj on the terminal (SDRAW obj (SDRAW-LOOP) - puts the use in a read-eval-draw loop in-package SDRAW and lines (vertical). They apply to all versions of SDRAW, SDRAW and subordinate definitions SDRAW-LOOP and subordinate definitions. )
(
"
"
Remember that fgrep matches
string along with
pathnames
their
takes a string and a path and returns each item that
its
end up
will
pathname. In
this
example, the matched lines and
"items." Notice somewhere.
in the file
that
each line
items
file
5.3.6
Piping Process Input or Output to Another Process
Whereas to
has the string
"SDRAW"
in
redirection performs process => file tasks, sometimes
perform process => process
one process directly
tasks.
in the
it
When you want
you
you use a pipe
to the input of another process,
will
want
send the output from
to
).
(
|
Process
piping eliminates the intermediate process of sending the output of the
first
process to the standard output and then using that as the standard input to the
second process. The symbol for a pipe
a vertical bar
take a long directory listing and send
is to
localhost> Is -1
is
it
to
(
). |
A
simple example
more.
more
|
The 'Is -1' command can produce a long directory listing because each is displayed on a line by itself. With many directories, this list may be too
item
long for a single screen display. directly into
step
is
more, you never see
eliminated. There
command,
making
on
no
I
abnormal
piping the output of the 'Is -
the initial listing at
limit to the
make
all,
1 '
command
since the intermediate
number of pipes you can use
in a
sense. For example, years ago (before
used the Troff text formatter. In Troff, there
was
a
I
tbl
pic utility for making figures, an eqn utility for refer utility for making bibliographies. It was not
tables, a
making equations, and at all
is
as long as the pipes
began using LaTeX) utility for
By
a
to take a file
and pipe
it
through
all
of these
utilities,
and then
to a printer.
localhost> troff parse
|
tbl
Although
|
pic
this is
|
eqn
|
refer
|
lpr
obviously an absurd example,
it
illustrates just
how
el fee-
5.3 Shell Control
tive process piping
5.3.7
Splitting
can be.
Output
to Different Locations
There are circumstances where a user send the output of a
command
That way you can see what is
a
may want
to
combine
the effects of pro-
and process-to-process behavior. For example, suppose you want
cess-to-file to
161
UNIX command
called
is
to a file
and
going on, and
to the standard output at once.
later
tee which performs
have
it
to
work
with. There
a branching of output.
copy goes directly to the standard output, whereas the other goes file. One example is shown below.
One
to a specified
localhost> fgrep SDRAW < Lisp/*.cl tee foo -*- Mode: Lisp; Package: SDRAW -*SDRAW - draws cons cell structures. - draws obj on the terminal (SDRAW ob j (SDRAW-LOOP) - puts the use in a read-eval-draw loop (in-package "SDRAW") and lines (vertical) They apply to all versions of SDRAW, SDRAW and subordinate definitions SDRAW-LOOP and subordinate definitions. localhost> more foo -*- Mode: Lisp; Package: SDRAW -*SDRAW - draws cons cell structures. (SDRAW obj) - draws obj on the terminal (SDRAW-LOOP) - puts the use in a read-eval-draw loop in-package SDRAW" and lines (vertical) They apply to all versions of SDRAW, SDRAW and subordinate definitions SDRAW-LOOP and subordinate definitions. localhost> Is CCode pcl-tar-orig. Lisp foo Items descartes pel |
)
.
(
"
.
where our previous example sent the the use of
tee
will also
ticularly useful during
5.3.8
result of the
fgrep
to the file
send the results to the screen. The use of
"items,"
tee
is
par-
debugging.
Process Backgrounding and Multi-Tasking
it be nice if you could perform many tasks at once, even though you may only have one command shell? The utilities that you have been looking at (command sequencing, redirection, and piping) allow a user to perform many
Wouldn"t
tasks with a single line of commands; however, they are put. If a user
wants
to run
many
all
related through out-
"unrelated" tasks, then something else must be
done.
When you
issue
commands
at the shell
prompt, they are run
in the visible
I
62
Chapter 5 The UNIX
Command
Shell
and Process Control
shell or what is called the foreground. Each process must take its turn, because you can only type one thing at the shell prompt at a time. Because the UNIX operating system treats each process uniquely, there is no reason why a process (let's call
it
a job) cannot be
machine when to continue
it
stacked
(i.e.,
put in a queue) and executed by the
has the resources to do so without tying up the user's ability
working.
When
a job
is
stacked,
placed in what
it is
is
called the
background. This "backgrounding" of a process simply means that the system will keep track of the job's progress and notify the user of status changes if desired.
When
a job
is
process and control
placed in the background, a is
new
subshell
is
created for the
returned to the user so they can perform other tasks in
the foreground or parent shell.
When you want to put a process in the background, you type (&) after the command and its arguments, as follows:
an ampersand
localhost> latex Unix-Man & [l] 93 8
-*
[job
number] process ID
The system then returns, as shown above, a job number and a process ID number. To poll the system on the progress of jobs currently running, you can use the jobs command: localhost> jobs [1]938
In this case, there is only one job running in the background and its process ID was "93 8." The system will tabulate the process and provide the user with information on its status. To retrieve the process, one would type
localhost> fg
1
where fg' stands for "foreground," and "1" is the job number. Let's say that you are running a job in the foreground but it turns out to be taking too much time and you have other things to do. In this case, you want to stop running a process in the foreground and put it in the background. This is accomplished '
by typing the following:
localhost> localhost> bg
An
actual
example
is
shown below, where
I
began
to run a latex job.
.
Command
5.4
stopped
and then placed
it.
it
into the
Manipulation
I
63
background.
localhost> latex Unix-Man This is TeX, C Version 3.0 Stopped localhost> bg [1] /usr/f 1/ jhodges/ latex Unix-Man &
where
(~Z) temporarily stops the job. and
'Jbg'
puts
it
into the
commands have the same effect as would '&' upon issuing command. You can see. when the command is pushed into the
background. These the original
background, that
you are creating forking a
this is
what
a subshell in
is
happening.
which
to
When you background
execute the process. This
is
a process, also called
shell, because, in the process hierarchy, the calling shell is a
parent
The number assigned to the subshell is what is returned when you background the process, and you can refer to it with this number, preceded by a percent sign (%). For example, if you decided that you wanted to kill this process, you would either have to do a process listing, find out its PID. and then issue a kill on the PID, or you could type the process, and the subshell
is
a child process.
following:
localhost> kill %1 [1] Terminated latex Unix-Man
5.4
Command
Manipulation many commands
Many
Every user
will issue
commands
issued will be repeated with minor modification, while others
in a
given login session.
be completely identical, differing only by what the current directory
is.
of the
may
There
commands when the system commands you have executed. By manipulating
should be no reason to retype (sometimes long)
keeps a running
commands
Command Command Command
• • •
5.4.1
list
of the
already issued, the user can be
more
efficient.
history repetition
modification
Command
History
make use of previously issued commands, the user must have access commands issued. This is achieved with the history command, history displays a list of the commands issued in the order they were issued at the command line. The shell variable history determines how far back the In order to
to the list of
history
is
retained and can be modified by the user. For example, the following
1
64
Chapter 5 The UNIX
is
1 ocalhost> 1
2 3 4
5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
39 40 41
Command
Shell
and Process Control
a history of a user's session:
history
scan cat login cat .cshrc .
ps ps uxa who rwho -a rwho ruptime Is
more README TeX3 finger jhodges .
.
cat .plan cat project cat .project chfn last jhodges whoami who am i ,
pwd Is
mv blah fooblah Is
mv mkdir Misc mkdir Misc mv fooblah Misc
Is Misc rm -rf Misc history
which shows many of the commands the mistakes,
if
history command
to see
that
you have seen
in this
chapter (even
As a user, you can always issue the what commands you have executed, and then reis-
you can find them
:
-
)
.
sue them or modify them to suit your current activity.
If
your prompt has the
command number in then you may remember, or even see on the screen, the command number. In such cases, the user need not issue the history command unless the desired command is off the screen already.
current
5.4.2
it.
Command
Repetition
The most often-used command modification is to repeat a command. The exis used to repeat a command. The exclamation point is of-
clamation point (!) ten
termed "bang." so the
command
given below might be called "bang 27."
Command
5.4
What follows the exclamation point tells the to
work with
Manipulation
operating system which
(called an event selector). Table 5.7
165
command
shows how event
selection
works:
Table 5.7:
Command
!N
history event selection
Repeat the Nth command;
command N back from
Repeat the
!
string
Repeat the most recent
last
the
command
For example, the following
which was
Is
number
is
preceded by a hyphen, then the is
executed
command
i
i
if
the current position
instance starting with
command
string
will repeat
command number
3 9.
Misc.
localhost> Is Misc Misc: No such file or directory
However, since in
command
that did not
work so well
(the directory
"40"). another example was tried. This time,
had been deleted
command 23 was
reissued.
localhost> !23 Is
forward
new-topics news-groups steph stud212 testl texput log
ftp. list
tmac
mail -elsewhere mail-probs new-net
widget widgetl
F90 Mail
cshrc-cognet dead. letter
Manual -in-LaTeX News README TeX3 TeX3 Texmacros bin blah core
fe
.
filterfile foo2
.
.
Notice that the
oed (item onto
it
.
.
command, as reinterpreted by the operating system, is echThe next step is to repeat a command, but to add
1) before running.
same command reissued example would not suggest it, this
with something else. For instance, below
with another directory after
method
is
®
it.
Although
this
often easier than typing the entire
is
the
command.
166
Chapter 5 The UNIX
localhost> !23 TeX3
Command
Shell
and Process Control
.
Is TeX3.0
README METAFONT README RT README SCORE README. WEB- to-C README WRITE-WHITE README distrib_inf README. vers ion Spiderweb TeXcontrib TeXdoc TeXemacs TeXfonts TeXgraphics TeXmacros aix
BibTeX. inputs COPYING. POLICY CWEB
fontutil make .history makefile makefile. bak mf mfware plain. log site .h site. h. bak stamp-web2c tex texput log texware usr utilityfonts web web2c
.
.
.
ChangeLog.WEB-to-C DVIware Install_INPUTS Install_INPUTS.bak LaTeX LaTeXfonts MACHINES MACHINES tex82 MFcontrib MFdoc MFtexf iles Man-pages PROBLEMS PROJECTS README README APOLLO
.
.
.
.
ams
bibtex cmfonts defaults
.
Notice that
TeX3.0
this
directory
.h
time the effect of the reinterpretation
command, which
is
what
is
listed
on the
is to
execute the
line after the
Is
com-
mand.
Last
5.4.3
Command
Sometimes you change one
will
want
thing, like the
Modification to
do the same operation over and over again, but file. This example was provided in the file
name of a
system security section of Section use of the up carat (~)
which k
will replace
it.
is
used to
3.6.2,
page 94, and
set off the string to
The template
is
is
repeated below. The
be replaced from the string
shown below.
patternl A pattern2 Examples changed
are
shown below, where
to illustrate
how
the access permissions for a file are
they look in a long listing.
5.4
Command
Manipulation
167
localhost> Is -IF blah 1 jhodges 44 Jul 18 13:46 blah -rw-r r localhost> chmod 700 blah; Is -IF blah 1 jhodges 44 Jul 18 13:46 blah* -rwx -* localhost> A 700 A 711 chmod 711 blah; Is -IF blah -rwx--x--x 1 jhodges 44 Jul 18 13:46 blah* localhost> A 711 A 722 chmod 722 blah; Is -IF blah -rwx-w w- 1 jhodges 44 Jul 18 13:46 blah*
——
®
—
command labeled (1). where the 7 00 (orig711 (new permission code) prior to reissuing the command. On the following line, the reinterpreted command is echoed, and then the result of the command is displayed. The same command is performed on the new "most recent" command. You can see that you could The
inal
change
string
permission code)
starts at the
replaced with
is
keep modifying the most recent command.
Command
5.4.4
More
often than not
Substitution
you
will find
minor modifications than
it
to retype
easier to reissue a specific
it.
command
with
you need a com-
In such circumstances,
command and the string modification command, in which you tell the operating system what part of the string to replace. This is accomplished with s." or search and replace command. Below is an example where both are used together. Note the use of two exclamation points ( ).
bination of the repeat
'
:
!
which means re-execute the is
localhoso
!
!
:
last
command.
In this case, the repeated
!
command
!23 TeX3 .0.
s/TeX3 0/tmac .
Is tmac
tmac. Franz
tmac.X tmac.XP tmac Xalg tmac .Xref .acm tmac .Xref ieee .
.
tmac.Xrefs tmac.Xtra tmac.Xtras tmac tmac. an tmac. an. new
tmac tmac tmac tmac tmac tmac
.
This example plished with
mand and last
The
the
i/N
is
TeX3
tA
'
.
.
.
an6n an6t ayday
.bib .
cp
.
.
.
imagen os
.
.
.
.
scover sdisp
tmac tmac tmac tmac tmac
.
.
.
.
.
skeep sref syb vcat vgrind
a bit overdone since the same result could be accomA tmac': however, the ":s" will work on any com.
will only
work on
example, the selection of what selection of
tmac tmac tmac tmac tmac tmac
the to
command argument
ence to the order of arguments.
most recently issued command.
modify was based on
to
In the
a pattern match.
modify can also be made with
refer-
1
DO
Chapter 5 The UNIX
Command
Table 5.8:
and Process Control
Command
modification word selection
Command name
First
argument
(e.g..
$
Last argument
(e.g.,
N
Nth argument
N1-N2
Range of arguments argument
1
The
Shell
' :
A
or
itself (e.g.,
(e.g.,
in the history.
!
:0)
A !
!
!
!
!
•
)
:
:
$)
4)
s' modifier after the event designator
working with commands
!
Below
is
is
(e.g..
!
!
:2-4)
also but one option for
a short reference of alterna-
tive modifiers.
Table 5.9:
Command
:s/patl/pat2
modification reference Replace regular expression
patl
slashes can be replaced by any
:h
Remove
a trailing
:t
Remove
all
:r
Remove
a trailing ".xxx" component, such as ".eps"
but a trailing pathname
(i.e.,
!
!
component
Remove
:g
Perform change globally
:P
For debug; print the modification but do not execute
but the trailing
from "rpO.eps,"
.-r)
:e
all
pat2;
pathname component
leaving the head "rpO"
5.5
with regular expression
symbol
component
it
Linking Files and Directories You have
how to recognize them, how to and how to control them. For each
learned about shells and processes,
customize them,
how
to
keep track of them,
of these, however, the assumption has been
command
made
that
you either have access
would obtain access by placing the proper directory path. Sometimes this can get unwieldy, though, and in your default execution you do not want to keep putting directories into your path. Why? Consider the following example. If someone develops a program that you want to use. then to a
they can
make
or that you
it
read/executable by you. Then, depending on
how deeply
the
5.5 Linking Files
program was nested
in their directory,
path. If they suddenly decided to
move
and Directories 1
69
you would be adding a long item to your the program, your path would no longer
be valid. In general, you do not want to place items in your execution path
which
are not in your
What you do
system.
own
A symbolic link is an
alias,
but
it
is
directories, or in directories higher in the
for the other cases
is
make
UNIX file
a symbolic link.
a pointer to a location in the file system.
A link is
like
a permanent part of your directory rather than something
which is started when you log on. It looks like a file, or a directory, and shows up on a directory listing. The command for creating a symbolic link is In, and the format for creating a symbolic or soft link follows:
In -s original-path your-path where the
's' option
means symbolic, "original -path"
the file or directory, and
"your-path"
directory file system, of the to link to a
program
to retain the
name
the location
is
the true path of
and name, within your
or directory. For example, suppose you want
UNIX directory called "latex,"
in the
in
file
is
your directory.
You
and
that
you want
could do that as follows:
localhost> In -s UNIX/CH6 /LATEX-STUFF/ latex latex
In general, case).
It
you
will
want
to retain the
does not matter whether the original
my
which has
ple,
consider
tial
long listing so that you can see a
directory,
same names
(i.e.,
latex
in this
examnumber of links in it. I will show a parfew such links and what they look like. is
a
file
or a directory. For
a
localhost> Is -1 total 35 -rw-r-r-rw-r-r-rw-r-r-
1 1 1
lrwxr-xr-x lrwxr-xr-x
jhodges 106 Jan 27 21:24 #blah# jhodges Jan 28 22:58 #diss.doc# jhodges 52 Jan 28 22:58 #foo#
1
1
jhodges 27 Nov 7 15:23 dvi2ps -> TeX3 0/usr/local/bin/dvi2ps jhodges 42 Sep 30 17:09 latex -> - jhodges/TeX3 O/usr/local/bin/latex .
.
local name
localhost>
In this example, tor
shows
link,
it
real
path
you can see
the link with an
'
that the first field of the permissions descrip-
1' flag.
displays both the local
real name
When
name and
the listing
shows the full path name.
the real path and
for the
170
Chapter 5 The UNIX
5.6
Exercises and Sample Quiz Questions 5.6.1
Command
Shell
and Process Control
Exercises
Exercise
1
Write a command that will display the message, "These are all the files in the directory ..." along with the list of all the filenames.
Exercise 2
Create two
two
test files
Redo
files.
with a few lines each. Use cat to concatenate the
have the output sent
this but
but this time append a third
Exercise 3
5.6.2
Problem
Run
file to
UNIX commands
a test on one of your
to a third file.
Do it again,
the concatenation of the in the
two
files.
background.
Sample Quiz Questions 1
to make some modifications/customizations to your ".cshrc" login script. Outline how you would go
Suppose you want
".login" about
this
or
and what considerations you should address along the way.
how
Problem 2
Explain
Problem 3
Consider the •
Which
the semicolon
ps is
listing
the
is
used
in
process control.
below and answer the following questions:
most recently used command
•
Explain the status fields of process 59
•
Can you
tell,
from
this table,
which
shell?
shell process
spawned pro-
cess 2608?
USER hodges root root root root root root root hodges hodges hodges root
PID 2630 2543 1
54 59 66 63
2539 2544 2608 93
%CPU %MEM VSIZE RSIZE TT STAT 1.5 1.4 1.34M 400K ql S 1.1 1.27M 312K pO S 0.5 0.0 0.8 1.31M 224K ? S 0.0 0.9 1.24M 256K ? S 0.0 1.4 6.37M 392K ? S N 0.0 1.0 1.25M 288K ? S 0.9 1.23M 248K ? 0.0 S R N 0.0 8.8 14.3M 2.45M ? 1.4 1.34M 392K pO S 0.0 0.0 1.4 1.34M 408K qO S 0.0 1.7 5.56M 480K qO S 344K ? S 0.0 1.2 1.34M
Problem 4
When
is
TIME COMMAND 0:00 -bin/csh (csh) 0:11 layers 0:00 /usr/etc/init -xx 0:00 /usr/etc/syslogd 0:22 /usr/etc/nmserver 0:00 /usr/etc/nibindd 0:05 /usr/etc/portmap 78hr (kernel idle) 0:00 -csh (csh) 0:01 -bin/csh (csh) 0:05 vi Unix-AppA-SF.tex 0:00 /usr/lib/sendmail -bd -qlh
I/O redirection most useful?
When
is
piping most useful? Pro-
vide an example which exemplifies each answer (can be written).
Problem 5
What
is
process backgrounding? Provide an example of stopping
process, backgrounding
it,
checking
its
status,
a
and bringing back into
the foreground.
Problem 6
Explain the use of the
history command
in
performing
command
5.6 Exercises
repetition
and modification. Provide an example of each.
When you
Problem 7
log on to a
der they happen
in,
UNIX
platform, explain what happens, what or-
and why.
Explain what an environment variable
Problem 8
and be
MAIL
What
variables are.
is,
and what the USER,
HOME,
most
likely to
shell are these variables
set in?
What
Problem 9
171
and Sample Quiz Questions
is
a
"command
alias," as
used
at the
command prompt or in the commands and explain
".cshrc?" Give two examples of legal alias
what they do.
Problem 10
Suppose you use the
are logging
vi editor,
on from a
modem
you have a hard time getting out of the most
likely to
home.
at
the backspace and escape characters
be?
List, in
editor.
When you
try to
do not work and
What
is
problem
the
order of priority, the steps you would take to
correct this problem.
Problem
1
1
Your last command was: % chmod 755 f ileA. Show how you would make your next command change permissions to owner read, write,
and execute, only, on a file named "fileB" using
command mod-
ification.
Problem
1
2
The sample process
status listing given
es running on a platform that
your userid
is
by
below
the userid hodges.
hodges, provide (1) a
sleeping processes, (2) give the
lists all
current process-
Assuming
list
for a
moment
the process ids for
UNIX command
to blast
all
one of the
process jobids.
USER hodges hodges hodges hodges hodges hodges hodges hodges hodges hodges
PID 202 216 215 210 208 206 214 211 212 217
%CPU 47.7 44.1 2.9 0.0 0.0 0.0 0.0 0.0 0.0 0.0
%MEM VSIZE RSIZE TT STAT TIME COMMAND 37.4 30. 4M 12. 0M ? R 30:59 - console (WindowServer) 5.4 4.15M 1, 72M ? R N 22 04 /LocalApps /Backspace app/Ba 2:46 / LocalApps /FrameMaker .app/F 15.7 8.61M 5 03M ? S 0:04 /usr/lib/NextStep/Workspace 6.9 5.56M 2 2 0M ? S SW 0:00 (appkitServer) 1.3 2.07M 432K ? 1.6 1.72M 528K ? S 0:00 /usr/etc/pbs -a 0:00 /Next Apps/Edit.app/ Edit -NX 4.2 4.92M 1.36M ? SW 0:00 /NextApps/ Preferences app/P 4.1 5.27M 1.31M ? SW 4.8 3 .88M 1. 52M ? SW 0:01 /Users/hodges/Apps/Date -NX 0:00 -csh (csh) 1.1 1.62M 368K pi S :
.
.
I
#2
5.7
Chapter 5 The UNIX
Command
Shell
and Process Control
Recommended Reading Shells Joy,
W.
[1980].
An
Introduction to the
C Shell, Computer Science
Division,
Department of Electrical Engineering, University of California, Berkeley, November.
6.1
Introduction and One of
Scope
the primary services a
computer provides
the ability to print docu-
is
ments. In this chapter, three techniques are presented for formatting and printing documents. For each, an overview and
along with examples for
its
use.
command
The examples
syntax will be presented,
will be illustrated
on
files
which
you should be able to replicate directly. You will be expected to understand the differences between wordprocessing and text formatting, and to be able to format and print simple documents. A simple document will be a code listing, the documentation for a program
in
manual format, and a simple paper
turning in as a term project. Samples al
examples are provided
in
will be provided
Appendix
L.
We will
and discussed. Addition-
leave up to you the decision
if any. to use on a long-term basis. Specifically, ment preparation techniques will be presented:
of which,
• •
•
suitable for
the following docu-
vgrind nrof f and trof f latex
Within the context of printing documents with a particular package, or a particular fashion,
chapter
is
is
the basic process of printing a
document
at all.
in
This
divided into sections that address the process of printing, and spe-
cialize in later sections to printing with a particular
method,
to achieve a par-
ticular end.
173
1 I *\ Chapter 6 File Formatting and Printing
6.2
Printing Etiquette Printing
is
very important to students in computer science, because programs
cannot always be read and edited (online) effectively. At some point you just
have
code
to see
in print. In the past
decade, the preparation of documents: for
assignments, reports, papers, and publications has also risen steadily. The need
which support all phases of code and document preparation, has become commonplace. Historically, high-speed line printers have been available to print out galleys of programs in draft mode, while dot matrix and laser printers have become available for higher=quality work. Although the trend has been toward higher quality printers, the cost of
for printing diversity, and applications
printing remains relatively high, so
it is
monsense guidelines when you decide •
important to keep in mind a few com-
document.
to print a
Being able to print code and documents
is
a privilege, not a right.
Never
abuse printing privileges with excessive or inconsiderate use. •
If
you need
to print source code, then use a line printer
•
If
you need
to print
vgrind •
Never
(or
print
code on standard-sized paper, say for a
man
on
don't print
it.
Printing large
documents
is
something
like a thesis,
line. In general, if
extremely cycle intensive.
you
are keeping other people
Remember
work, theses, or publications.
that, if
In general,
it is
who
considered in poor
taste,
and. at
ad-
documentation
When you
print out
from finishing home-
everyone thinks only of
themselves, then the network comes to a screeching •
report, then use
pages. System manuals are provided by the people
line,
exists
possible.
equivalent), in landscape format.
its
minister a system and generally exist on
•
whenever
halt.
many
institutions, not al-
lowed, to print online manuals. Before considering printing a manual, consult •
your local administrator and find out what the rules
When
printing a large job
is
justified, print
it
in
chunks rather than
so that other users with smaller jobs can get their •
Print large jobs
•
Text formatting (i.e..
•
nice
when CPU is
are.
cycle intensive. Try to run print jobs on lower priority
them) whenever possible.
Tagged
change Format (TIFF), and Bitmapped screen dumps take up a
lot
Inter-
of space.
takes a laser printer a long time to format pages with graphics. Try to break
up •
once
loads are low.
Pictures, such as PostScript. Ecapsulated PostScript (EPS).
It
all at
work done.
print jobs so that
Whenever you
print a
pages with graphics are printed separately.
and a previewer (when applicable) before save a lot of trees, and it will decrease your
possible, use a speller
document. This
will
edit time. •
When
your document has many figures
that
must be placed, often
efficient to test print the figures separately to
Then,
in the
document, reserve space
make
for figures, tables,
format the document without them until
all
it
is
more
sure the) look right.
and captions, and
figures and tables are correctl)
6.3 Printing Tools
placed.
Then
ing this with
insert
them
latex
into the
document
for printing.
The method
1
75
for do-
will be explained.
The more you work with text formatters and printing on a network, the more these rules will affect you. More often than not, you will be the brunt of someone else's negligence, so the more people who actively adhere to these guidelines, the better off the network community will be.
Printing Tools
6.3
Before a document
is
printed, every user should
ration utilities available to assist in preparing the
Ch6
know of the document prepadocument and using the print-
ing resources wisely. In this section several tools for checking and validating
document before printing will be introduced, along with the print commands The files and test output for these examples can be found in the "print- tools" directory. The following tools can be used to edit an ASCII document before it is formatted or printed, in order to help edit the file and eliminate or reduce excessive printing. For example, when you prepare a document, you always want to check the spelling in it before printing. UNIX has a utility called spell that can be invoked on a file that returns a list of misspelled words. Consider the small document presented below, called "realpaper." a
themselves. print-tools
localhost> cat real-paper Graduating in Four Years: A Forgotten Trust Bachelor O. Science Abstract A decade ago, the promise of a college education in four years was a real expectation for students in american universities. With the advent of
.
.
.
Introduction This year will mark a decade in the information age. And with this age has come a terrifying realization that students cannot hope to assimilate the information required of them in an undergraduate program, and somehow put themselves through school, in a four-year term... The Demise of Student Support for Higher Education The major threat to higher education in the past half decade has been the lack of educational support by the taxpaying public. The effective result is that students are, to an increasing extent, being forced to pay for their own educations. Tie this into increased costs and time to graduation and the result is an ever decreasing number of graduating students The Increase in Required Information
real -paper
represents a simple
ning of a fictitious term paper.
We will
ASCII
file,
what might be the begin-
use this text to illustrate the use of var-
1
#6
Chapter 6
File
Formatting and Printing
ious utilities in the following sections. For example, to run
paper you
localhost> spell real-paper localhost> more rspell american
This
spell on real-
type the following:
>
rspell
command
applies the
results of spell
spell
real -paper and re-
utility to the file
"rpspell." Had
directs the output to the file
the redirection been omitted, the
would have been displayed onscreen.
If the list
of misspelled
words had been long, this might not have been desirable. Note that there is only one mispelled word, "american." which should be "American." In general, the list of mispelled words is given alphabetically, and the correct spellings are not provided. Use
look
for finding possible spelling for
words flagged using
spell. Five additional UNIX utilities which can be used to prepare your document: wc, spell, style, diction, and explain are shown, with their arguments, in Table 6.1.
Table
6.1
:
ASCII
file
checking
utilities
spell FILE
Runs a
wc FILE
Counts words,
look WORD
Looks up
style FILE
Analyzes the readability of your
diction FILE
Looks through
explain PHRASE
Gives thesaurus entries for an awkward
Finding
6.3.1
The look its
spell
lines,
spelling
Word
utility is a
checker on the document
and characters
on
WORD
as
you
FILE FILE
in
spell
them
FILE
the file for outdated and
awkward phrases
PHRASE
Spellings
companion
argument, as you would
spell
to it.
spell, look and finds words
takes a particular
with the assumption that you can recognize which one
when out as
real -paper,
on the misspelled. Running look on
I
ran spell
file
this
the
word
as
that are spelled similarly,
word
word produced
is
correct.
For example,
american was
called
the following response.
6.3 Printing Tools
I
77
localhost> look american American Americana Americanism
Counting Word, Lines, and Characters
6.3.2
The wc program grams,
wc
is
probably used most often of the document preprint pro-
a paper for a class, or for publication, and have a
wc
will
file. When you are preparing word minimum or maximum,
counts characters, words, and lines in a
come
in
handy,
wc
can be invoked as follows:
localhost> wc real-paper 28 156 969 real-paper
column of this output is the number of lines in the file. The second number of words, and the third is the number of characters. Thus, real -paper is a whopping 2 8 lines long.
The
is
first
the the
6.3.3
Checking Document Readability
Although not used as often as spell or wc, the other four
utilities are available
them out. The style function is used to compare the readability of your text. The man page can give more detail, but the essence of the command is to show the approximate reading level of the sentences of the paper, as defined mostly by length and phrase complexity. The figure on the following page is an example of style invoked on real -paper. The display shows five different components: (1) readability grades, (2) sentence info., (3) sentence types. (4) word usage, and (5) sentence beginnings. for those
who wish
to try
In general, as a writer,
you
are trying to maintain the content of
your paper
while lowering the readability ratings (highlighted) of this output. The com-
mon the
belief
is
that the easier
more people who
it
is to
read a document, regardless of the content,
will understand, and. hence, appreciate
what you are
try-
ing to say. Notice that there are four different rating scales provided (Kincaid. auto.
Coleman-Liau. and Flesch), and
ent. In general, scores
that the score
under each scale
under 10 are considered good.
is differ-
1
78
Chapter 6
File
Formatting and Printing
localhost> style real-paper real -paper readability grades: (Kincaid) 15.3 (auto) 15.3 (Coleman-Liau) 12.8 (Flesch) 15.8 (31.5) sentence info: no. sent 6 no. wds 155 av sent leng 25.8 av word leng 5.05 no. questions no. imperatives no. nonfunc wds 87 56.1% av leng 6.78 short sent (36) 17% (1) longest sent 64 wds at sent 3; shortest sent 5 wds at sent 6 sentence types: simple 67% (4) complex 17% (1) compound 17% (1) 0% (0) compound-complex word usage: verb types as % of total verbs tobe 46% (6) aux 31% (4) inf 23% (3) passives as % of non-inf verbs 10% (1) types as % of total prep 14.8% (23) conj 2.6% (4) adv 1.3% (2) noun 29.0% (45) adj 20.6% (32) pron 1.9% (3) nominalizations 4 % (6) sentence beginnings: subject opener: noun (2) pron (0) pos (0) adj (0) art (2) tot 67% prep 17% (1) adv 0% (0) 17% (1) verb 0% (0) sub_conj 0% (0) conj expletives 0% (0)
Sentence
info,
and sentence types are both based on sentence length, while
word usage and sentence beginnings each sentence.
It
on the types of words found
in
Checking Phrase Style
6.3.4
The diction program or outdated. es with
ample
are based
even looks for expletives.
is
used to find phrases which are considered awkward
When executed on
awkward
your
file,
diction produces a listing of sentenc-
phrases, set off by asterisks and brackets
that applies diction to
real -paper
is
*[...]*. An
shown below.
ex-
6.4 Printing a
Document
179
localhost> diction real-paper real-paper Tie this into increased costs and time to graduation and the result is an ever decreasing *[ number of ]* graduating students.
number of sentences 12 number of phrases found
This output means that the phrase
1
"number of "
is
improperly used
in the
context of this sentence. This phrase can then be used as input to explain.
Correcting Phrase Style
6.3.5
The explain program takes phrases indicated as awkward by diction and returns thesaurus entries for the phrase which the author can replace the odd phrase with. The phrase "number of" from real-paper is applied to the explain program below: localhost> explain phrase? number of use "many" for a "a large number of" use "several, many, some" for "a number of" use "usually" for "except in a small number of cases" use "some" for "in a number of cases" use "enough" for "sufficient number of" use "often" for "in a considerable number of cases" phrase? -^
(T)
clear that explain leaves something to be desired, because a better re-
It is
at "is" and would be "fewer graduating students." Nevertheless, the program offers another viewpoint, and that is sometimes valuable. The program will continue to prompt for phrases until you kill the process, as shown at item (1).
placement would begin
6.4
Printing a
Document
Regardless of what means one chooses to print
must know how
to
invoke the printer,
how
to
(e.g.. laser
check the
or line printer), one
print queue,
and
how
remove a print job from the queue. When a document is prepared to a point where editing a hard copy would be useful, the command used to print the doc-
to
lpr. lpr (man lpr stands for "off line print" and has options for which pages to print, how many copies to print, what printer to select printing, and so forth, as described in the manual page. The general format
ument
is
telling
for is
shown below.
)
180
lpr
Chapter 6
[
File
options
Formatting and Printing
[file
]
where some of the "options" from the man page are
"file" Table 6.2:
is
below, and where
listed
the file to print.
lpr CO mmand option reference
-h
Do not print
-in
Indent printed output by n spaces
-Jjob
Print the
-m
Send a mail note
-Pprinter
Send the output
-Ttitle
Print the
-#N
Print
6.4.1
the banner page
argument to
ob"
as the job
you when
to the spool
argument
N copies
j
"
title
name on
the banner
page
the job has been completed
queue
at the
of the specified
by
for the printer specified
printer
head of each page
files
Line Printers
There are generally two types of printers available on a system: draft mode printers
and laser
printers.
The default printer
is
line
usually a line printer, so any
output sent to the printer will be printed on the line printer. The procedure for printing a file
command
on the
line printer is
shown below. Use
the following
UNIX
format:
lpr -J BIN## filename where '-J'
is
printer output. tion is
an
A
lpr
option used to print a banner on the
print banner
and can be anything
that
an example of a banner that
For example,
if
I
type the
is
page of the
simply a large font used for easy identifica-
is
can
first
assist in identifying
your
print job.
used as a sorting mechanism
"BIN##"
at this institution.
command
localhost> lpr -J BIN20 myfile.c
to print a file
named "myfile.c"
mechanism, above, pick up
"BIN20." The
bin
my
to a line printer and.
output
number allows
in the
the person
using the sample
designated location, sorted into
who
sorts the print jobs to split
Document
6.4 Printing a
them ed
into smaller,
more
identifiable bundles. Often print jobs
to a shared printer are sorted
of the user.
You
mechanisms used
should write
name being
which
181
are print-
by the name
by the
file
down
the locations of local printers and the
for sorting jobs here,
printed, or
and on the inside flap of
this
book.
Printer Location Line Printer Name Laser Printer Name
Banner Requirements /Options
6.4.2
Laser Printers
There may also be laser printers available for your use. Please reread the
print-
ing etiquette rules above prior to using a laser printer, because they are expensive
and someone has
mat
as a template for printing
to
pay for them. Use the following
on a
UNIX command for-
laser printer:
lpr -J BIN## -Pprintername filename where the '-P' option
is
being invoked to redirect the output to the printer
named "laser." The naming convention
change from loca-
for printers will
tion to location, but there will
always be a difference
laser printers unless there are
no
line printers.
in the
names of
line
and
The command can be used
in
conjunction with a text formatter and various preprocessors/postprocessors (or filters), as illustrated
below.
localhost> nroff real-paper .troff
In this example, the
per troff" .
When
|
nroff
and the output
document, and then
PostScript
tbl
|
text formatter is
sent into the
tbl
used on the utility to
file
"real-pa-
format tables
in the
to the printer.
your work files,
is
lpr -Plaser
is
PostScript, or includes PostScript or Encapsulated
then you must send your print
file to
a laser printer which sup-
ports PostScript printing. In the preceding example, this
is
why
the '-P' option
was used. 6.4.3
Checking Status of Print Jobs
When you
print a job.
you generally
where
in the stack
of jobs
it is
the status of your job easily
mand. An example
is
(i.e..
like to
know when
it is
done
the queue) waiting to print.
enough with
shown below.
printing, or
You can check
the line printer queue,
lpq com-
1
82
Chapter 6
File
Formatting and Printing
localhost> lpq -Plaser laser is ready and printing via lat Rank Owner Job Files active jhodges 34 um3.ps
The
first
Total Size 2246554 bytes
display column. "Rank." will
with the job which
is
printing on top.
list
the order of jobs in the queue,
The job which
tive." The "Owner" column is the userid that is an identifier, the "Files" column identifies
is
printing
labeled "ac"Job" column
is
sent the job, the the file that
is
printing,
and the
"Total Size" indicates how much space is required to print the job. Notice that um3 .ps, which is the PostScript of the current version of this book, is rather large (2.25
Meg). Normally
this all at once, so
low
to kill
I
it
would be considered
will use the print job
command
poor in
taste to print
an example be-
it.
Removing Print Jobs from
6.4.4
removal
in
the Print
Queue
when printing a job, you may have cause to remove the job from One such case would be if you were demonstrating how to use the printer and the printer status commands, as I am now. More realistically, if you realize that you have made a mistake, or if you change your mind, you might want to cancel the job. One way to gauge a mistake is by the size of the file. If you have a general idea of how large the file should be. then if it is much Occasionally
the print queue.
it might be worthwhile looking at the file. The remove a file from the queue is Iprm. When using the lprm command, you must identify which job to remove, and which printer to remove it from. If you do not, then all your jobs will be removed. An example that removes the job I sent to the printer, shown above, is shown below.
smaller or larger than that size,
command
to
localhost> lprm 34 -Plaser dfA034sfsuvaxl.sfsu.edu dequeued cfA034sfsuvaxl.sfsu.edu dequeued localhost> lpq -Plaser no entries
6.5
Laser Printing
Mode
in Draft
way to print a document is laser used when code is being turned in paper and look good. The utility
After a line printer, the next least expensive draft
mode. Laser
draft
mode
with a report and must be
vgrind file
"f
is
used to send
ilel" and
is
(in
generally
standard
Files to the laser printer.
save the output to a new
file,
For example,
to
vgrind
a
"f ileloutvg." you can type
6.5 Laser Printing in Draft
Mode
183
as follows:
vgrind filel -t
>
This
fileloutvg now creates
such as shown
in the
a PostScript
command
file that
can be sent directly to a laser printer,
below.
localhost> lpr -Plaser -J BIN20 fileloutvg
along with whatever options that are requested. For the case above, the
fileloutvg
will be printed
on a laser
printer.
Some
file
vgrind options
of the
are listed below.
Table 6.3:
vgrind Command option reference
-h HEAD
Causes the output
-1 LANG
Specifies the language, (C,
PASCAL,
to
have
HEAD
LANG,
to use in
choosing printing style
LISP, CSH, SH)
N
-SN
Printing point size given a
-t
Sends output
-W
Forces output to a wide printer
-X
Outputs index
Ch6
printed on every page
to the standard output rather than to
file in
lpr
pretty format
Consider a program
file
named "shortlispf ile.cl," which can be
in the "vgrind" directory. In the example below, the file is printed usvgrind. Because it is a lisp file, the '-1' option '-lisp' has been used 'to format it. Were the file a C program, this same option would have been lc' The '-t' option sends the output to the standard output, which has, in this
found ing
vgrind
example, then been redirected to a to
show you
that the file
an optional header.
is
file
ending with
a PostScript
"MISSIONARIES"
file.
" .
ps." This has been done
The command below
at the
localhost> vgrind -lisp -t -h "MISSIONARIES" shortlispf ile.cl localhost>
The
file
generated in this process,
also prints
top of every page.
vgrind2
.ps.
is
>
vgrind2.ps
shown
in Fig. 6.1.
184
Chapter 6
Figure
6,1
:
A
Formatting and Printing
File
PostScript
generated for laser draft with vgrind
file
shortlispfile.cl
Cannibals and
MISSIONARIES
shortlispfile.cl
Missionaries
There are three functions:
way
invoke
to
(this
depth-first
solution
breadth-first
branch and
the
solution
solution
best-first
missing
is
function
its
(dfs-mandc) (bfs-mandc) (best-mandc)
using
a simple cost
bound search since
I
cannot
function think
of a good heuristic)
A
state
missionaries on
bank,
right
by
represented
is
of cannibals on
the
and
fourth
the
the
bank,
left
numbers. The first number is the number second number is the number of
four
bank,
left
the
third is the number of cannibals on the number of missionaries on the right bank. missionaries on the left and 3 cannibals on
the the
is
For example, (0 3 3 0) means 3 the (3
The
righi.
3
start
therefore
state
3
(0
is
3)
and
the
goal
state
is
0).
Moves
The first number is subtracted from the number of cannibals on the left bank and added to the number on the right bank. The second number is subtracted from the number of missionaries on the left bank and added to the number on the nght bank. All (setq
;
All
(setq
;
are
represented
moves to the *neg-moves*
by
out
the
-!)
moves of
nice-pnnt-mandc
(defun
(null
(if
pair
of numbers.
left.
'((0
moves to the right. *pos-moves* '((0 1)
Print
a
history)
(-1
0)
(-1
(I
1)
0)
(1
the
-1) (-2 0) (0 -2)))
(2
missionaries
(0
0)
2)))
and cannibals problem
in
a
nice
way.
(history)
ni!
(progn (pprint
;
(car
history))
(fresh-line)
(print-statop
(car
history))
(fresh-line)
(nice-print-mandc
;
Print
(defun
the
print-statop
(
C
M
(print-bunch
(wnte-string (cdr
"
(wrile-stnng
(print-bunch
Feb 2 11:58 1992
history)))))
a
pretty
(caar
way.
statop)
(cadar
3)
statop)
3)
")
statop)
(wnte-stnng (pnni-bunch
in
statop)
(pnnt-bunch
(if
(cdr
state-operator
(pnnt-oper (cadr statop)) "
"))
")
"
C
M
(caddr
(car
statop))
(cadddr (car statop))
3)
3))
Pa#c
I
of slwrtlispfile.cl
versus Word Processors
6.6 Text Formatters
6.6 Text Formatters versus A
text formatter or
tained in a text
Word Processors
markup language
file into
185
is
used to organize the material con-
an attractive format similar to what a word processor
does on a personal computer. Text formatters are generally used when draft
mode produces
insufficient quality.
For example, a
a paper, a thesis, a report, an article, and a
uments which can be prepared with
book
letter,
a set of lecture slides,
are increasingly
text formatters. Until
every
complex doc-
UNIX platform
in "What You See is What You Get" document formatters will continue to be valuable tools.
has the ability to display documents
(WYSIWYG) Some
format,
people continue to use text formatters even though they have access to
word processing and page layout applications. Two of the primary reasons for this are: (a) the actual document is an ASCII file, so it is smaller and can be easily sent anywhere (and then recompiled) via email, and (b) the formatting is semantically clear, meaning that the author selects a style and
excellent
it up to the implementation to decide how to interpret that style. One of most popular text formatters today is the Hyper Text Markup Language (HTML), which is used to format documents for the World Wide Web (W3).
leaves the
HTML is used on the Web is that many different types used to interpret a document. HTML specifies the names of
The primary reason of computers are
commands used display text, text will
etc..
to
that
format a document, and suggests
but
it is
to
the local
browser
to
how
the
commands will how the
determine exactly
appear on a particular screen.
A text formatting ments
left to
commands writers use in their docuappearance when the document is later displayed
language
produce the desired
online or printed. All
clicks and
ing of text formatting
a set of
word processing programs use
guage, but generally hide the the like) to
is
commands from
improve user is
a text formatting lan-
the user (with menus,
mouse
friendliness. In this respect, an understand-
extremely useful to anyone
who
is
writing documents
on a computer.
A text formatter is a type of compiler. text,
and those expressions are placed
guage definition. Each formatter has
mat
is
its
It
has expressions which operate on
in the
own
document according
to the lan-
language, but an interchange for-
slowly emerging for transferring documents from one hardware plat-
form, and formatter, to another.
An
interchange format enables the translation
of a document written with one formatter into an application independent
ASCII
Another text formatting language can then be applied to the interchange file, and. whatever commands are supported by the new application will
file.
be translated properly. Those capabilities that are not supported are con-
verted to text.
1
OD
Chapter 6
File
Formatting and Printing
Text Source File
6.6.1
A user begins using a text formatter by creating the text which will be formatted using an editor such as vi. This text file the text
which
will tell the
pared, the text formatting application
new
result is a
called the text source
is
file.
When
commands may be inserted into the document formatter how to format the file. When the document is pre-
written, formatting
is
file
is
invoked
to
compile the document. The
with the interpreted document translated into a form a print-
er can understand.
Sometimes
this stage
can be printed directly, while other
times another program, a translator/converter, will be needed to produce a printable
6.6.2
file.
Features of Text Formatters
Text formatters are generally very
versatile, usually sacrificing user friendli-
ness for the sake of feature versatility and user control.
needed ers
to
produce high quality documents are
do not have an immediate need
kept in mind
when you choose
for
listed
some of
Some
of the features
below. Although most us-
these features, they should be
a text formatter or
word processor, and they
should be compared to the overall cost of the formatter in terms of required
memory, performance, ease of use, Page layout (margins,
tabs)
Document organization
Document
etc.
(outlining)
control (type, page numbering)
Paragraph control
(line spacing, indentation, styles)
Font control (type,
size,
spacing)
Table control (generation, placement, labels)
Graphics control (generation, placement, labels) Equation control (generation, placement, labels)
Cross referencing (sections, Lists
and indexes
cites, figures, tables)
(table of contents,
list
of figures and tables, index)
Bibliographic support
Document
sharing (incorporating and exporting text and graphics in differ-
ent formats)
Header and footer control User customization
This
is
not an exhaustive
list,
but should provide a basis for the capabilities
that are required in constructing a
document.
Many
of these features are not
needed by everyone, or by every application: however, they are fundamental to text production. In the following sections, text files with
be presented because ual pages.
We
we
will address the formatting of
two popular formatting languages. The it
is
a standard
will also present the L
UNIX A
nroff/troff
utility that is
package
will
used to prepare man-
T E X package (pronounced
LAH-TECH
i.
6.7 nroff
and
187
troff
it easier to use. in some respects, and is specifically directed at high document preparation [Lamport 1986]. For those who are interested. A A is similar to L T E X. and learning to use L T E X will simplify the process
because quality
HTML
HTML.
of learning
nroff /troff
For both
we
formatted without any commands, and then that
new
and latex, a
users can develop an understanding of
will slowly
how
text file will
be
add commands so
to use the utilities to their
best advantage. "&»
nroff and troff
6.7
nroff is
a text formatting language that formats text in files for typewriter-
like output devices
such as display terminals. The
nroff
matted using nroff.
and uses macro
ease,
is
designed
packages
to
UNIX
manual pages
produce documents with relative
to increase its versatility,
around for a long time. and. to some degree, has outlived Ch6
ertheless,
nroff is all UNIX
learned by
*a-
UNIX
a part of a standard users. After
nroff
is
nroff
found
tion can be
nroff-troff
6.7.1
in the
"nrof f- troff "
has been
Nev-
usefulness.
its
distribution
and should be
understood, other text formatting
and printing applications can more easily be appreciated. Examples
ip>"
are for-
in this sec-
directory.
Simple Application of nroff on a
File
One strength of nroff is that it can be applied to an unformatted file command line. Thus, nroff can be applied to "real-paper" as follows:
at the
localhost> nroff real-paper
nroff
produces a result similar to
cat
in that
it
scrolls off the page.
problem can be resolved by piping the nr o f f output through the
The nrof fed
window
size.
through
mail
file is
minimally justified
This can be useful
when
in
or posted to newsgroups.
sends this
new form to
This
utility.
80 columns, the standard terminal
writing documents which will be sent
When
the
nroff
does not change, since the text formatter merely reads the
nroff
more
the standard output, to a
is
file
file, to
issued, the file
and reformats
it.
another process,
or to a printer, depending on what the user wants to do.
6.7.2
Simple
In general,
ways than this,
1
.
A macro
is
nroff
is
used to modify the appearance of
just forcing
nroff
has
a series of formatting
with a specific name.
nroff Commands them
to a particular
command options which
commands
page
files in
size. In
more dramatic
order to accomplish
can be embedded into the text
associated with a particular function and invoked
file
by
1
OO
Chapter 6
File
Formatting and Printing
the user. For example,
Ch6
spaced, the
W&
spacing) alone
mands
nroff-troff/
into
you wanted
at the
beginning of a
real -paper and
the document.
real-paper
if
produce a
to
embedded nroff command
The
file
see
line.
how
to
do
file
this is '.
with the lines double
Is
2' (for double line
nroff com-
Lets put a few simple
they affect the display appearance of
below can be found
in the
"real -paper"
directory.
localhost> cat real -paper. nroff •ce
@
1
Graduating in Four Years: A Forgotten Trust .
ce
1
Bachelor •
.
ce sp
0.
Science
1
(2)
1
(3)
Abstract ti + 75in .11 -.75in A decade ago, the promise of a college education in four years was a real expectation for students in american universities. With the advent .
.
of
.
.
.
.ti -.75in
(4)
.11 +.75in
(f)
Introduction .
sp
2
@
.Is 2
This year will mark a decade in the information age. And with this age has come a terrifying realization that students cannot hope to assimilate the information required of them in an undergraduate program, and somehow put themselves through school, in a four-year term. .
.Is
1
sp
5
.
The Demise of Student Support for Higher Education The major threat to higher education in the past half decade has been the lack of educational support by the taxpaying public. The effective result is that students are, to an increasing extent, being forced to pay for their own education. Tie this into increased costs and time to graduation and the result is an ever decreasing number of graduating students The Increase in Required Information
.
command starts with a dot/period " " at the beginning of a new line, and that the command is not followed by any text. I have flagged six nroff commands that are worth noting in this sample. First, at item (1), the command " ce 1" tells nroff to center the next line, so as to look like Notice that every
.
a title.
rently
The next item is
(2) sets a
length has been reduced by the
mand 1
ent
temporary indent of whatever the indent cur-
work on both margins, the line same amount with the " 11 - 7 5 in" com-
plus .75 inches. In order for this to
.
.
(item 3). This will create an abstract for the paper. After the abstract, the
and
line length are returned to their starting values.
Item (4)
" .
sp
2"
6.7 nroff
simply
tells
nroff
to space
two
lines in the output,
line
spacing so that this paragraph
The
line
spacing
is
is
file is
and item
troff
(5)
189
changes the
double spaced, as previously mentioned.
returned to single spacing with
execution and output for this
and
" .
Is
1"
at
item
(6).
The
shown below.
localhost> nroff real-paper .nroff Graduating in Four Years: A Forgotten Trust Bachelor O. Science
Abstract A decade ago, the promise of a college education in four years was a real expectation for students in american universities. With the advent of ... Introduction This year will mark a decade in the this
age
information
age.
And
with
has come a terrifying realization that students cannot
hope to assimilate the information required of them in an
under-
graduate program, and somehow put themselves through school, four-year term.
in a
.
The Demise of Student Support for Higher Education The major threat to higher education in the past half decade has been the lack of educational support by the taxpaying public. The effective result is that students are, to an increasing extent, being forced to pay for their own education. Tie this into increased costs and time to graduation and the result is an ever decreasing number of graduating students. The Increase in Required Information
6.7.3
nroff, troff, and Macro Packages
nroff is used for displaying on typewriter-like devices, such as terminals. When you want to prepare a document that will be displayed in WYSIWYG format, or be printed on a high quality printer, then troff can be used. The differences between nroff and troff are related to the quality of the output device. On a workstation or laser printer, where the size, type, and resolution of characters can be controlled, troff will produce superior quality over nroff. For the most part, troff and nroff have compatible command sets, the output just looks different. For example, in nroff emphasized text
I
90
Chapter 6
Formatting and Printing
File
comes out on
as an underline, because
Using trof f emphasized
able.
a file,
you are using what
nrof f /trof f
is
an awkward
is
on a typewriter device
text
is
italics are
not avail-
When you execute nrof f nrof f The major drawback of
italicized.
called plain
command
.
set that is difficult to
remember. Sev-
macro packages have been developed that make it easier to format documents, where a macro is a mechanism whereby the commands needed to perform a particular function are clustered together and invoked with a single command. The user can use the macro packages or even develop their own macros. When nrof f is invoked with the '-m' option, a macro package is loaded that can be applied to the text file. Whatever appears directly after the 'm' must be a file located in the path " /usr/ local /tmac," and the file must be named "tmac CH." where "CH" stands for the file identifier. For example, the following command: eral
.
localhost> nroff -me real-paper .nrof
should execute
nroff
on the
file
real-paper
using the
"/usr/lib/
tmac /tmac e" macro package available to nroff. You can find out more how to get macro packages to work by reading the man page for nroff .
about
(man nroff). Four macro packages which are generally supported are the me (man 7 me), ms (man 7 ms). mm (man 7 mm), and man (man 7 man) packages. Examples in this section will describe how to load and use the me macros. Following the examples in this section, some plain nroff commands and some me commands will be tabulated for reference.
Loading a Macro Package or Input
6.7.4
own macro
In order to load your the one
below
at the
File
package, or input
top of your document source
file,
place a line similar to
file:
so ~/tmac/memacros
nroff command means switch the input source to the file folso command. In this example, the path and filename following "so" command point to a macro package where user-defined macros re-
The
Ch6
"
.
so"
lowing the the
.
j
wC^
side. In this
>rf-trofT/
ros" ros"
memacros
file
example, some macros from the user's personal
have been loaded.
directory. Inside the
(some commented) macros which can be used
The rate
so command can also be used nrof f / trof f commands. .
"tmac/memac-
A copy of this file can be found in the "memacmemacros file, there are several user-defined to
to input files
format complex documents.
which have
their
ow
n sepa-
6.7 nroff
191
troff
Creating a Simple Document File
6.7.5
Each
and
nroff document
age load
line, if
is
created with an editor such as vi.
such a package
is
used,
is at
The macro pack-
the top of the page,
and the
re-
mainder of the document and nroff (or macro) commands. As previously mentioned, an nroff command starts with a period in column of any particular line, and is immediately followed by the command name. The same is true of me macro commands. Most macros have different comconsists of text
l
mands
to begin
and end the context of the macro. For example, 1'
to
begin a
list
1' is macro used. Macros that are part of me are generally lowercase. Macros that are user defined are generally uppercase. Try running troff on the sample file "real -paper me" shown below.
item with me. the macro
*
.
(
is
used, and to end the
list,
the
'
.
)
.
localhost> cat real-paper .me .so UNIX/Ch6/nrof f-trof f /memacros/memacros .Is .
(T)
1
in +
.
5m
.11 5.7i .fi .
ce
1
Graduating in Four Years .
ce
A Forgotten Trust
1
Bachelor
0.
Sc ience
.sp 1 .
ce
1
Abstract •(q
(2)
A decade ago, the promise of a college education in four years was a real expectation for students in american universities. With the advent of •
.
.
.
.
)q
sh
(3) 1
"Introduction"
This year will mark a decade in the information age. And with this age has come a terrifying realization that students cannot hope to assimilate the information required of them in an undergraduate program, and somehow put themselves through school, in a four-year term... .sh
1
"The Demise of Student Support for Higher Education"
The major threat to higher education in the past half decade has been the lack of educational support by the taxpaying public. The effective result is that students are, to an increasing extent, being forced to pay for their own education. Tie this into increased costs and time to graduation and the result is an ever decreasing number of graduating students .
sh
1
"The Increase in Required Information"
(4)
1
92
Chapter 6
File
Formatting and Printing
This example
primary is
will
related to the text.
command. The what
is
is
paper. In this example, the
what
a set of locally defined macros and
com-
be effective for the entire document, but are not specifically
At item
abstract
called a quote
command
A
preamble
me
and references. Item
(1) begins
are those for headings
called the preamble.
mands which
typical of a simple
is fairly
commands
is
(1), a
macro
file is
being loaded using the
" .
so"
created by centering the abstract line and then using
paragraph environment, which
(item 2) and ended with a
heading. The heading level (here
1)
' .
)
q'
command
is
begun with
a
q' ' .
(item 3). Item (4)
indicates to the formatter
how
to
(
is
a
number
wrapped in double quotes. This heading is thus a first-level heading, or section. The output of running this example with nrof f is shown below:
the sections.
The
text of the
heading
is
localhost> nroff real-paper. me Graduating in Four Years: A Forgotten Trust
Bachelor
0.
Science
Abstract A decade ago, the promise of a college education in four years was a real expectation With for students in american universities. the advent of .
1.
.
Introduction
And This year will mark a decade in the information age. with this age has come a terrifying realization that students cannot hope to assimilate the information required of them in an undergraduate program, and somehow put themselves through school, in a four-year term... 2.
The Demise of Student Support for Higher Education
The major threat to higher education in the past half decade has been the lack of educational support by the taxpaying public. The effective result is that students increasing extent, being forced to pay for are, to an their own education. Tie this into increased costs and time to graduation and the result is an ever decreasing number of graduating students.
3.
The Increase in Required Information
6.7 nroff
nroff and troff Formatting
6.7.6
The
nroff
postprocessor
is
es
utilities,
at the
end of
nrc
Table 6.4:
In the
Although we
the interested user can find
and references
6.7.7
nroff
ff
Formatting
eqn
Generates equations
tbl
Generates tables
refer
Generates bibliographic references
nroff /troff Command
following sections, several plain the
man
pag-
utilities
Generates line graphics
Use commands.
in the
this chapter.
troduced.
prepro-
will not illustrate the use
more information
pic
Plain
A
itself.
whose output you can send to nroff before printing, and a a utility through which you pipe the output from nroff be-
fore sending the final output to a printer.
of these
193
text formatter has four utilities for generating specialized text
a utility
is
troff
Tools
types that can be used as pre or post processors with
cessor
and
Reference
nroff /troff commands
will be in-
examples as guidelines for understanding the usage of these
PAGE CONTROL One
of the
first
things a writer wants to do
the page:
Table 6.5:
" .
pi" and
* .
when
formatting a document
bp."
nroff Page control option reference
.pi ± Ni
Page length, where Ni
.bp
Start a
.pn r N
The next page
is
the
number of inches
new page will be
is to
me package, a couple of commands are used to define
set the pagination. In the
numbered N
(default
is
1 1
I
94
Chapter 6
File
Formatting and Printing
LINE Table 6.6:
CONTROL
nrof f Line control option reference
.11 Ni
Ni
number of inches
Line length, where
na
t=>
No
adjust on justification; leaves ragged right page
.nf
No
fill;
.fi
Fill;
ce N
Center the next
ul N
Underline the next
Continuous underline
sp N
Space
.Is N
line spacing (e.g., single spaced:
Turn on output
.
.
•
.cu .
.
nm N M S
I
same
same
N
is
the
as verbatim (left justify; for
as full justification left
N
vertically
N
6.5)
code display)
right
lines
lines
N
=
1)
numbering
line
Line number
starts with, or
at
incremented
to,
N (e.g., N=3
starts line
3)
Line numbers for lines of multiples of M
M
is
lines
N
numbering
and
(default
(e.g.,
M=2 numbers
every other line)
S
Line numbers and lines are separated by S spaces
I
Line numbers are indented by I spaces
MARGINS AND TABS Table
6.7:
nrof f Margin and tab control option reference
•in ± Ni
Indentation,
•ti ± Ni
Temporary
Tab;
•
ta Nt
N
when Ni
specifies the
indentation.
number of inches
+ before paragraph and
identifies location
after
paragraph
and t the type as declared by the characters below:
Right justified tab
Center adjusted tab Left adjusted tab
-
offset.
6.7 nroff
and
troff
195
FONT TYPE AND SIZE Table 6.8: .
ps ± N
.
ft
nroff Font type/size control option reference
F
fp N F
ps ± N
Set point size, can also use
Font changed to F. where F
R
-
Times roman
I
-
Times
B
-
Times bold
-
Symbol
is
inline
the font
name
(default
roman)
italic
Font position, where font
is
loaded (1-4) where the correspondence
between character font type and font position
Fl
-
roman
F2
-
italic
F3
-
bold
F4
-
courier
is
tabulated below
commands can be issued inline with the use of the command. For example, the following command changes the font types using both Point size and font
the character
\fB See
f?
and font position methods:
Spot \fl run \fR
will
produce
See Spot run
The same \f3 See
s
*fl
effect can be
Spot \f2 run \fl
produced with the font position
1
9b
Chapter 6
File
Formatting and Printing
HYPHENATION To prevent a word from being hyphenated,
%
type
at the
beginning of the word
with no space.
Table 6.9:
nrof f Hyphenation control option reference
.nh
«•
.hy N
No
hyphenation
Automatic hypenation on; the number
N indicates how many
characters to keep
together before splitting
MISCELLANEOUS nrof f Miscelaneous options reference
Table 6.10: .tl
'
left center right '
'
Three-part
title;
text
between quotes
fied, center justified,
and
.
ds xx string
Define a macro
End
Define a string
a
macro
be
left justi-
right justified with the cur-
rent margins of a line
.de XX
will
xx
definition
xx containing
string
6.7 nroff
Command
6.7.8
Reference for the
and
troff
197
me Macro Package
SECTIONING Table
6.1
1
:
me Heading control option reference
Sh LEVEL "LABEL"
Create a section heading, with its
LABEL
as
its title,
and
LEVEL
as
depth; level values are associated with the following section
types (0
< LEVEL
cat sample -man. nroff .TH FOOBAR 1L "vax version 1" "CSC212 Demo Man" SH NAME
FOOBAR
99
Documentation using nroff 1
A sample program documentation file
.SH SYNOPSIS
foobar file
(I)
[
listl
]
...
[
listn
]
.SH DESCRIPTION
Foobar doesn't do anything. This is simply a syntax example. .SH AUTHOR
Jake Foobar .SH EXAMPLES
Try:
EX %
(4)
foobar sampman
.HE .SH SEE ALSO .SH FILES .SH KNOWN BUGS
There are no known bugs, yet, but there isn't any code either.
The headings are taken from the standard cluster of man page headings that was mentioned in Chapter 3 and page 65. The .TH (item 1) creates the title bar of the online document. The first and second items are the program name and section, respectively. They should not be quoted. The third item is a reference to the architecture the program is running on. The fourth item is a comment written in the footer. The third and The noted items
are described below.
fourth items must be quoted. Notice that this three-part
is
an adaptation from the
nroff
title.
Item (2) uses the .SH text associated with the
command to produce
heading for item
(2).
a subject header. Item (3)
Without other commands
is
just
text is
200
Chapter 6
File
Formatting and Printing
justified as ting an
normally the case in
nr o f f
.
Item (4) identifies a macro for format-
example. The .EX begins the example, and the .EE ends the example.
The macro formats an example by
commands
Additional
setting a
temporary indent for the paragraph.
more elaborate documents
for designing
are tabu-
lated below.
Viewing the Online Documentation
6.8.2
You can view
the
arguments. The
document you
create
File
by calling nroff with the appropriate
commands provided below
are the ones used
when man pages
are displayed:
nroff -man
tbl file
The tbl
more
col
-
was mentioned
utility
in a
previous section.
postprocessor which formats tables. The
col
playing multiple column
text. If
as that above, then these
commands can be
use
is
the
-man macro
more. The hyphen
call
you want
to
utility
It is
an
nroff
works with tbl
pre/
for dis-
produce a simple document, such left out.
The
part
which you must
and the subsequent (recommended) pipe through
in the sixth position in this
command
is
often used with
which expect a filename as an argument, and represents the standard input even though the utility is getting its input piped in. Below is an example of this command applied to the sample document file "sample-man. utilities
nroff." more localhost> nroff -man sample-man. nroff FOOBAR(IL) UNIX Programmer's Manual |
NAME FOOBAR
-
A sample program documentation file
SYNOPSIS foobar file
[
listl
]
.
.
.
[
listn
]
DESCRIPTION Foobar doesn't do anything. A complete fake.
AUTHOR Jerk Fc bar continued
FOOBAR(IL)
6.8 Online
Documentation using nroff
201
EXAMPLES Try: %
foobar sampman
SEE ALSO FILES KNOWN BUGS There are no known bugs, yet, but there isn't any code either.
The output for man formats is the standard 80 columns by 66 lines. Page numbers appear at the bottom of each output page with odd page numbers appearing on the right side and even page numbers appearing on the left side. I have collapsed some of the lines from this output to save space, but you should get the idea.
Making
6.8.3
When you
the Online
finish writing the
Document Available
program and the online document
is
also correct,
then you need to place the document into the directory where the application will reside
and make sure
that the
These considerations
also.
guage
is
will
command
to read the
be continued
when
the
document is available C programming lan-
introduced, because program writing and documentation are very
closely related.
6.8.4
Some
nroff Man Macro Package Command of the
commands
below pertain only
to
for creating online documentation are included in the
(man nroff.
following table. Refer to
Reference
7
man)
for additional
commands. The
entries
202
Chapter 6
Table 6.14: .B
File
Formatting and Printing
nrof f Man macro options reference
[TEXT. ..]
set in
•CT CHAR
TEXT
Sets text
in boldface; if
no
text
is
specified, the next text line
Prints the
;
keyboard control character indicator
CW
Sets text in constant width font until another font change
.EE
Ends an example and
.EX
[i]
width font with 'no allowed)
HB [words
fill'
mode
.
EX and EE
printed in a constant
is
.
(no text wrapping and blank lines
in effect
mode
Sets the text in underline
.11 word
Sets a temporary indent to the length of the specified
word
Reverses one line and then
.
.12
.
.
.
specified
•
PN x[y]
Sets
x
PP
.R
sets a
temporary indent to the length of the
constant width typeface and then reverts to the previous
y
Starts a block paragraph; sets the prevailing indent to
Sets the text in a
roman typeface
until
tered. Also ends nrof f underline
.RN .RS
[i]
Prints the return character indicator,
Shifts the left .
RS
margin
5i is
encoun-
amount of i
indents.
was or
in effect
to the right (relatively) the
macro can be nested up to nine levels deep
TEXT
Creates a section header with
.SS TEXT
Creates a subsection header with
TEXT
Begins a new reference page and
sets the
TH ncafx
if it
.SH TEXT
.
.
another font change
mode
The
word
word
in a
typeface,
•
found
is
and indents
restores basic text defaults
example. Text between
Starts an
for
A prints
example. .CT •
name; c
to the section;
"VAX",
f to a footer
page
title:
n refers
to the file
a to an optional machine architecture, such as
component; and x
to an extra optional
commen-
tary such as "unsupported"
,TP
[i]
Sets the prevailing indent to i, then begins the indented paragraph
with a hanging tag given by the next text the
is
boldface
macro places
line: if the lag
the next text on a separate line
docs not
fit.
6.9
LATEX
203
6.9 L A T E X L A T E X and T E X are text formatting languages similar to
tex and tex
are the associated compilers
nrof f/trof f la.
which are widely used
for produc-
ing articles, books, and letters which can be sent directly to a phototypesetter
such as a Linotype®. Their popularity partly
on a more mnemonic
set
is
based partly on their semantic
clarity,
of commands, partly on the development of bit-
map previewers, and partly on the fact that the source file remains in ASCII. latex and tex can be used on a wide variety of hardware platforms: Sun, HP. DEC. NeXT. Apple, and IBM, to name a few. The commands are fairly easy is gradual for complex documents. In compared to the competition, latex (tex) is quite reasonable. Presented here are a few examples for using latex. There are six
to follow, although the learning curve
general, and especially
Ch6
important things to consider source
file,
command
(3)
supplementary
(6)
when
latex:
using
(1) the
environment, (2) the
running latex, (5) printing the file, and Sample examples will be provided, annotated, and
files, (4)
utilities.
The files used in these examples can be found in the "latex" diMore examples appear in Appendix L along with some comparisons
explained.
latex
rectory. to
HTML.
L TgX Source
6.9.1
As
File
,
have a terminal equivalent, so Ch6
Paper
nrof f and trof f the basis for latex is a source file containing text latex commands. Unlike nrof f, however, tex and latex do not
in
and
for a Simple
Script
and then placed into
signed to send
nrof f The .
its
all
this
examples must
output to a terminal,
simplest form of
first
be converted to Post-
document. Also, because it
latex
latex
cannot be run on raw text source must
still
is
not de-
files as
have a few
can
latex
commands embedded in it. Consider a simple modification to real -paper, called "real -paper tex," and located in the "real -paper" directory, which can be compiled with latex. All that you do is wrap the text inside the
latex/
real-paper
.
following
lines:
\documentstyle{article} -* \begin{ document}
(l) (2)
% %
.
©
BODY OF REAL -PAPER
% \
end {document}
—
-«
The latex document called the
1.
Unless a
command
file is
preamble.
used.
is
comprised of three sections. The
In the preamble, the type of
document
is
first
(4)
section
is
established. For
204
Chapter 6
File
Formatting and Printing
example, the is
first line
of the
a required line in the
file,
file, at
to
an "article"
mands,
title
style,
which
is
(1),
defines the documentstyle. This
because the document must be formatted
particular style. In this example,
can be found
item
real -paper
is
to
some
being formatted according
the format for a journal article. Other items
which
preamble are user-defined macros, page numbering cominformation, and so on. in a
The second
document
part of the
is
the
document body
itself
(item 3), and
must be identified by a beginning (item 2) and an end (item 4). The text of the document, and its formatting, are written where item (3) is. The percent-sign (%) is the tex and latex comment character. All three segments are required
latex
Given these minimum requirements, real -paper can be formatted. Granted it will not look too pretty, because nothing else has been added, but will be equivalent to running nrof f on the statements in a
plain version of
source
file.
real -paper.
The last section of a latex document is the closing segment, or postamwhich is located between the document ending item (3) and the end statement (4). In the example above there isn't anything of note in the closing segment, but this is where the bibliography style would be identified. ble,
Executing
6.9.2
Once
latex on
the Simple Paper Source File
the source file has been created,
ply the function
la tex to
it is
a simple matter to use
latex. Ap-
the source file as follows:
latex source filename Below
is
the trace of
latex'ing "real -paper
.
tex":
localhost> latex real-paperO.tex This is TeX, C Version 3.0 (T) (real-paperO tex (2) LaTeX Version 2.09 (/usr/f 1/ jhodges/TeX3 /usr/ local /lib/ tex/ inputs /art icle sty (5) Document Style 'article' . (real -paper .aux) /usr /local /lib/ tex/ inputs /art 10 .sty) (/usr/f 1/ jhodges/TeX3 (T) (real-paperO .aux) [1] (s) Output written on real-paperO dvi (1 page, 1628 bytes). Transcript written on real-paper0.log. sfsuvaxl [272 dvi2ps real-paperO > rpO.ps [/usr/f 1/ jhodges/TeX3 /usr /local /lib/ tex/ tex. ps] [1] .
.
.
)
.
)
.
©
]
.
6.9
LATEX
205
The execution trace shown above should be explained. First, item (1) shows the tex version number, which may be significant if you ever have problems with it. Second, item (2) shows what file latex is compiling. Item (3) shows where latex has found the requested documentstyle. In this case, all files relating to
tex reside in the
tory off the directory nifies that
page
1
where tex
"/usr/ local /lib/ tex/ inputs" is
installed.
At item
has successfully been compiled.
the compilation has gotten because the page
(4),
You
note the
can always
numbers are listed numbers in
the compilation proceeds, tex also displays line
something interesting
is
detected.
and context will be displayed. In
When
this
an error
is
"real-paperO .dvi."
This
file
direc-
This sig-
tell
in this
how
far
way. As
the text file
detected, the line
if
number
example, there was only one page of text,
so the compilation was successful. The final output written to
[1].
is
noted
at
item (5) and
dvi stands for device independent
is
file.
can be translated to forms suitable for display and printing, such as
PostScript.
Item (6) case, file,
I
a
decided to
which
Note
is
that
I
I
new command, one of many possible "next run a conversion from real -paper dvi .
file, is
1.
2.
it
have directed the output
the compilation phase,
lists
shown
to a PostScript
have included below. This was done with the function dvi2ps.
the page
numbers
as
"rpO ps." This file can be book with the special command."
to the file
printed with Ipr or, in this case, input into this
As with
steps." In this
.
when dvi2ps begins working on the "dvi" completes its translation. The final product
it
in Fig. 6.2.
Some versions use a function called dvips. One way to incorporate a postscript figure into
a
latex document
is
shown
in
Appendix B.
206
Chapter 6
Figure 6.2:
File
Formatting and Printing
Result of minimal changes to Graduating
in
real-paper
Four Years:
A
for latex compilation
Forgotten Trust
Bachelor O. Science Abstract
A decade ago. the promise of a college education in four years was a real expectation for students in american universities. With the advent of ... Introduction
This year will mark a decade in the information age. And with this age has come a terrifying realization that students cannot hope to assimilate the information required of them in an undergraduate program, and themselves through school, in a four-year term...
The Demise
somehow put
The major threat decade has been the lack of educational
of Student Support for Higher Education
to higher education in the past half
support by the taxpaying public. The effective result is that students are. to an increasing extent, being forced to pay for their own education. Tie this into increased costs
number The
6.9.3 If the
and time
to graduation
and the
an ever decreasing
Debugging a latex Compilation
latex compilation
runs into problems, then they will be flagged as the
If the
compilation runs into a problem
then n stops and waits for your instructions. will sec a question in
is
Increase in Required Information
compilation continues.
you
result
of graduating students.
mark
(?). If
you type an
debugging the compilation. Some of
others aren't.
You can always
When
'h' after this, its
it
cannot resolve,
the compilation stops,
comments
tex will
you
try to assist
are pretty useful
and
type a carriage return and try to resume the com-
LATEX
6.9
You can
pilation.
also type an 'x' to quit, or an 'e' to edit, at
default editor will be invoked and place at the line
When you
Wrapping detracts
will be able to recompile the file.
File
a couple of commands around the text in a
file is fairly
document.
intent of the writer; to prepare the to the file, then
discern the administrative
and
last
commands
rate file, called a
commands,
command
it
the
command
file
and commands somewhat separate, and
j^y^ latex/
comfiies
A
in the
As a result, sometimes it is easier preamble in particular, into a sepa-
itself.
the
commands
are always exe-
way of formatting
cuted the same way. and they do not get in the
Although unnecessary, a
difficult to
preamble and postamble)
Then, the general
file.
harmless, but
When you add
becomes more and more
(i.e..
segments from the paper
to consider placing these
Ch6
which point your
where the error occurred.
from the
many such commands first
you
The Command
6.9.4
it
exit the editor,
207
the
document.
enables the user to keep a document to define useful
macros for massaging
command file, "rp0com.tex," one that can be used to format "real -paper tex." is shown below. This file can be found in "comf iles" directory. In addition, two sample command files ("com. tex" and "sfsu.tex") are provided and explained in document appropriately.
the
simple
.
Appendix
L.
localhost> cat rp0com.tex \ document style {article} \begin{ document} \include{real-paperO} \ end {document} \end
Note
it
lines labeled item (1)
the "\
like the template
and item
(3). the
include" command
provided above. The
paper
(item 2).
real -paper is
will require that the file
file named "real -paper tex." prompt the user for another name, latex name end in " tex." but you do not have to type the it
6.9.5
The
file
Preparing the L
.
will
.
real -paper latex commands only.
suffix.
being read into
The include com-
searches the current directory for a
does not find one. then
line"
much
instead of placing the text associated with
document with
mand If
(3)
is that,
between the
this
(2)
that this file looks very
difference in
(T)
.
tex
can
now be comprised
TrX Document
of text and
'"in-
File
you have seen how to execute latex on a minimally altered file. The reason for that was to prove to you that you can do it. Preparing a document
So
far,
with a formatting language can be as frustrating as writing a computer pro-
gram, maybe more
so.
You want the
result to
look a certain way, and you sense
2Uo
Chapter 6
File
that it
Formatting and Printing
it
can be done, but you keep getting compile
works or
to locate
it
doesn't, but in a
document
errors. In a
program,
at least
errors aren't always obvious, nor easy
and eradicate. As an introduction
to text formatting with
real -paper
latex, we
tex. This time, the required formatting commands will be embedded, and we will look at each one and explain what it does. We will then execute latex on the file, and print out the result as before. After that, you can simply take a copy of the source file and replace it with your own text, and it should prepare a nice looking class paper for you. There are two types of commands that you should be familiar with in latex: (1) preamble commands, which you have seen a few of already; and will take a look at preparing
(2) text (inline)
mands title is
.
commands, latex commands
in that they look like
called "title," and the
are different
words. For example, the
from
command
nrof f comfor
making a
command to make a section-level heading is called
"section."
Preamble Commands
latex is designed to produce a document whose page layout is precommands that either initialize latex and select the paper layout, or tell latex how to override or add to the paper Although
defined, the preamble consists of all the
format as a whole. These are page layout commands. For example, consider the first line of the file
\
rpOcom. tex
above.
document style {article}
The essence of a latex document
is
the documentstyle.
The document-
"article sty," which is a template or style file for the document. The style file tells latex how to format the document. For example, article sty tells latex that the docustyle file, in this case
"article"
stands for
.
.
ment
will use a technical article format.
page
size,
that
style file for articles then sets the
margins, font type and sizes, headings, text spacing,
might go into a journal. The
pers, since
The
it
is
macros defined
article style file
already a formal presentation. in
tex and
latex. The
idea
A
is
style file is
to
etc. for a
paper
can also be used for term padesigned using the
hide from the user the task of
building a page and a paragraph and leave the author the task of preparing the
content of the paper.
If
you change the
style file of the
document, but keep ev-
erything else the same, then the document will look very different. This
of the strengths of latex.
A
latex document
style
can be modified by adding other styles on top of it. That
is
is,
documentstyle
are hierarchically organized and support property inheritance.
syntax for documentstyle use in a
\
document style
[
latex
optionl, option2,.„.
]
file
follows:
{style}
is
one
only a template, and files
The general
6.9
where "style" can be any of the following
LATEX
209
(partial list):
book.sty article. sty
report. sty
msthesis.sty diss. sty letter.sty
Notice that the the suffix
is
files
have the suffix
" .
For example, the default type size for the
"article"
user can elect to have the document printed in tional
\
latex
sty." As with other
files,
not required but assumed. Options can be used to modify a style.
1
style
lpt or 12pt
lOpt. But
is
the
by providing an op-
argument:
documents tyle [12pt] {article}
A
few of the options available are tabulated below: • •
lOpt lpt
1
•
apalike
•
tvvocolumn
When Ch6
the author decides to
modify the
style of their
need change the documentstyle and future versions of the This capability makes
latex
an extremely powerful
document, they only file will
tool.
be changed.
Of course,
a docu-
mentstyle can be edited to suit one's particular needs, but then you must understand tex
much
better. All the standard
"/usr/ local /lib/tex/ inputs" ested, there are
documentstyle
files are
located in the
directory. In addition, if
some home-brewed document
styles in the
you
are inter-
"texmacros"
di-
rectory.
Text
Commands
There are two types of text commands
embedded
commands
or inline
in latex: commands, latex commands
in that they
backslash (\)
at the
off with curly braces
low.
look like words.
beginning of a (
{ } )
new
within the
( 1 )
line
commands
are different than
and
(2)
nrof f
A latex line command starts with a line. A latex inline command is set
text.
An example
of each
is
provided be-
210
Chapter 6
File
Formatting and Printing
\ small this text would appear smaller than normal
\normalsize this text would appear normal again
However,
the
same thing could be done,
inline, as follows:
small this text would appear smaller than normal} \\ \normalsize} this text would appear normal again}
{\ {
Line
commands
changes, while inline other references.
6.9.6
are used to
commands
change paragraph are
most often used
The double backslash (\\)
The Source
File for a
styles
and more global
for emphasis, cites,
and
forces a carriage return.
Simple Paper
Now we will look again at real -paper tex, a version which has latex commands for formatting it in article style. I have not used a command file so .
that the reader
can appreciate the difference.
localhost> cat real-paper.tex \ document style {article} \begin { document
\title{Graduating in Four Years: A Forgotten Trust} \author {Bachelor 0. Science} \date{} \maketitle \vspace{0 5in} .
\begin{abstract A decade ago, the promise of a college education in four years was a real expectation for students in american universities. With the advent of
.
.
00 (2)
® (i) (5)
(6)
.
\end{abstract \section{ Introduction}
(j)
This year will mark a decade in the information age. And with this age has come a terrifying realization that students cannot hope to assimilate the information required of them in an undergraduate program, and somehow put themselves through school, in a four-year term... continued
6.9
LATEX
21
\section{The Demise of Student Support for Higher Education} The major threat to higher education in the past half decade has been the lack of educational support by the taxpaying public. The effective result is that students are, to an increasing extent, being forced to pay for their own education. Tie this into increased costs and time to graduation and the result is an ever decreasing number of graduating students
\section{The Increase in Required Information} \ end {document} Vend
This example I
will
The
show
is fairly
a sample
typical of
command
file
latex papers
not using a
for the paper after
line labeled (item 1) indicates the
paper
title.
I
command
file.
discuss this version.
Titles cannot be
broken into
you cannot use two backslashes, as mentioned above, to The paper's author is formatted with the \ author command at item (2). With this command, you can use backslashes to break up the author field in \ author. This will allow you to put your address, school, or course in the same format and location. Item (3) illustrates the use of \date to label the date of the publication. This can be made blank as shown. The \maketilines arbitrarily, so start a
new
line.
tle command
(4) generates the
title.
Note
that maketitle
must be
after the be-
ginning of the document. The line in which item (5) occurs illustrates the creation of vertical space using the
\vspace command.
Vertical and horizontal
space require an argument. The argument must have units, and the units can be
An abstract \begin{abstract}
inches, centimeters, millimeters, picas, and points.
with
paragraph
the
stract},
command
.
can be created .
.
\end{ab-
and end and an indented paragraph in
as noted at item (6). All paragraph styles start with begin
with end. The abstract paragraph creates a smaller type.
The
There are
heading depths
six
line in
which item
subsection are numbered. In
title
(7) occurs creates a first-level heading.
latex, but only part, chapter, section, and latex, all numbered sequences are generated in
during the compilation, so the author need not worry about section numbering.
moved, latex reorders the numbering the next time the document is compiled. The final note (item 8) illustrates how the document is ended, which ends the document paragraph style, and the paper. If sections are
As an
aside, the file
real -paper tex need not have a preamble if the command file. A command file for this paper .
author had decided to use a
would have been simple
to produce, as
I
have done
in the
next example.
212
Chapter 6
File
Formatting and Printing
document style {article} \begin document \title{Graduating in Four Years: A Forgotten Trust} \author {Bachelor O. Science} \date{} \maketitle \vspace{0 5in} \begin{ abstract} A decade ago, the promise of a college education in four years was a real expectation for students in american universities. With the advent \
{
.
of
.
.
.
end {abstract} \ inc lude { real -paper \ end document \
{
6.9.7
Running L TrX on the Simple Paper
The
was compiled with latex
file
as
shown below and
printed as illustrated
in Fig. 6.3
localhost> latex real-paper This is TeX, C Version 3.0 (real-paper tex LaTeX Version 2.09 /usr/f 1/ jhodges/TeX3 O/usr/local/lib/tex/inputs/article sty Document Style 'article' . (real-paper aux) /usr/ local /lib/ tex/ input s / art 10 sty) /usr/f 1/ jhodges/TeX3 (real-paper aux) [1] Output written on real-paper .dvi (1 page, 1720 bytes). Transcript written on real-paper.log. localhost> dvi2ps real-paper > rp.ps [/usr/f 1/ jhodges/TeX3 O/usr/local/lib/tex/tex.ps] [1] localhost> .
.
(
.
(
.
)
.
.
.
)
.
6.9
Figure 6.3:
213
LATEX
PostScript output from latex applied to real -paper tex .
Graduating
in
Four Years: Bachelor
().
A
Forgotten Trust
Science
Abstract
A real
1
decade ago. the promise of a college education in four years was a expectation for students in ameiican universities. With the advent of
Introduction
This year
will
mark a decade
in
the information age.
a terrifying realization that students required of
them
through school,
2
in
:
umot hope
And
with this age has come
to ascimilate the information
an undergraduate program, and somehow put themselves
in a four-year term...
The Demise
of Student Support for Higher
Education The major
threat to higher education in the past half decade has been the
lack of educational support by the faxpaying public.
The
effective result
that students are. to an increasing extent, being forced to pay for their
is
own
education. Tie this into increased costs and time to graduation and the result is
an ever decreasing number of graduating students.
3
The Increase
in
Required Information
Preparing Complex Documents
6.9.8
To use latex on a complex document, document has at the
several passes
must be executed.
If the
a bibliography, then the bibliography can be included directly,
end of the document, or be included separately,
in
which case
it
will be
placed on a separate page. The format for creating bibliographic entries, index entries, start
run
and cross references are discussed
in
Appendix
L. All included files
on a separate page. Either way, you must run bibtex on the
latex
on the
file.
have prepared the index
Likewise,
if
entries, then
you have an index
in the
file after
you
document, and
you must run makeindex on the
file after
214
Chapter 6
File
Formatting and Printing
latex on the file. The order between bibtex and makeindex is unimportant. Next, if you have any cross references in the file, then you have to run latex on the file again just to generate and resolve the references. you have run
They
are gathered together after the first pass and then placed on the second
one. Finally,
if
you
are planning to put a table of contents,
latex again,
of tables into the document, you have to run tions lag the processing
of figures, or
list
list
because these func-
by one cycle. Of course, a single pass can accomplish
latex compilations are required in adsuch as bibtex or makeindex. At each
any of these tasks, but generally two dition to stage,
any postprocessor runs,
latex produces a file called "file .dvi" which can be sent to an
put device. So you need not run
all
these passes
when you
are
out-
trying to
still
you have done all these passes will the document print correctly. It should be noted that if you do not modify the index entries or the bibliographic entries, then you do not have to keep running makeindex and bibtex. The overall template is shown below: compile the document. Nevertheless, only
Run latex on
•
("file.tex" •
the
the
("file.aux," "file.idx"
=>
the
Run latex on
the
document
=>
file
"file.ind")
document
("file.aux," "file. bib" •
file
"file. toe," "file.lof," "file. lot," "file. dvi")
Run makeindex on Run bibtex on
•
document
=> "file.aux,"
after
file
"file.bbl")
document
to import ".ind"
("file.aux," "file.ind," "file.bbl"
=> "file.aux,"
and ".bbl"
files
"file. toe," "file.lof." "file. lot."
"file. dvi")
Run latex on
•
the
document
to resolve references
("file.aux," "file.ind," "file.bbl"
=> "file.aux,"
"file. toe," "file.lof." "file. lot."
"file. dvi")
Run dvi 2ps on
•
("file. dvi"
=>
the
document
"file.ps")
latex is the executable which compiles the document file in a manner nrof f latex produces a number of files which are editable but not, in general, of much use to the latex neophyte. One of these files is called "file aux," which is required by the bibliographic reference utility bibtex. An embedded preamble command, makeidx. produces a file called "file idx." This file can be used with the program makeindex to produce similar to
.
.
.
an index. The resulting passes are required
is
called
file is
"file, ind." The reason
man)
and
to re-
to build the "file.bbl" file (the reference file),
solve cross references within the document.
can execute the
that so
utility
dvi2ps
takes the device independent
file
this
process
on the "file. dvi" created by
Script suitable for printing with Ipr or
ports a PostScript previewer.
When
latex
file
is
done, the user
dvi2ps
created.
and translates
it
to Post-
previewed on a workstation which sup-
LATEX
215
dvi2ps
does not
6.9
dvi2ps file
>
file.ps
which
will write the output to the file
need you
to type the ".dvi" suffix
bibtex
postamble, and
"file
on the
.ps." Note that
file,
latex
does not need the ".tex"
does not need the ".aux" postamble. All of these are
the default expectations for their respective utilities.
properly formatted,
it
When
the
document
is
can easily be printed on a laser printer with the following
command:
dvi2ps file
lpr -Ppr inter-name or you can use the
"file .ps"
created from a
dvi2ps
run directly, as
fol-
lows:
lpr -Pprinter-name file.ps
6.9.9
L A T £ X Command Reference
N
measurement and the associated value. If unspecified, the unit is assumed to be pixels in 72 dots per inch (dpi). Inches can also be specified as "in." Every command is asIn each item in the following tables, an
sumed
to follow the
(\) in
column
slash.
An
1
of a
format above. That line,
or
it is
refers to a unit of
is,
enclosed
each item
"item" assumes curly braces around
are used, they
must be used only
entire paragraph style.
propriately.
at the
starts
with a backslash
in curly braces ({ }) it.
and the back-
When "begin" and "end"
beginning of lines since they reflect an
Paragraph styles can be nested but must be balanced ap-
216
Chapter 6
File
Formatting and Printing
DOCUMENT CONTROL Table 6.15:
latex Document control options reference
\documentstyle [options] {style}
Basic document page layout, font selection, etc.
\pageheight {N-units}
Length of page
\pagewidth{N-units}
Width of page
\leftmargin{N-units}
Size of
\rightmargin{N-unit s
Size of right margin
\title{TITLE}
Document
\ author
{TEXT}
Names, addresses,
\ thanks
{TEXT}
Acknowledgments
\begin{ abstract}
.
.
.
\ end
{abstract}
margin
left
title
No
\listof figures
No argument
\listof tables
No argument
\date{item}
Current date
.
.
.
\ end {
of authors
Abstract environment
\tableof contents
\begin{ document}
etc.
document
argument
Document environment
\maketitle
\ inc lude { DOC
Put the Insert
title
DOC
here, after to
\begin{ document}
compile here;
DOC name
ends
with ".tex" \ appendix { TEXT
Chapter level header with
\print index
Print the index
\bibliography{FILE}
Name
of
file
of bibliographic entries:
FILE
".bib"
Type of bibliography formatting
STYLE
text
now
name ends with
\bibliographystyle { STYLE
TEXT
type ends with ".sty"
desired;
6.9
LATEX
217
PAGE CONTROL Table 6.16:
\newpage
latex Page control options reference Break page here and
start a
new page
FONT TYPE Table 6.17:
Lat ex Font type options reference
{\bf item}
bold font
{\em item}
emphasis,
{\sf item}
san
{\sl item}
slant type
{\tt item}
typewriter (constant width)
italic
serif
font
FONT SIZE Table 6.18:
latex Font
size options reference
\tiny
tiny text
\ footnotes ize
footnote-sized text
\normalsize
normal-sized text (for style option)
Marge
large text
\Large
Large text
\huge
huge
\Huge
Huge
text
text
218
Chapter 6
File
Formatting and Printing
HEADINGS The following for the
list illustrates
the font type and size, and
"book, sty" documentstlye.
cle. sty, will differ in
Table 6.19:
how
numbering schemes,
Different documentstyles, such as
headings are formatted.
latex Section header options reference
\part{Part}
Parti
\chapter{ Chapter}
3 Chapter
\ sect ion
1.1 Section
\ subsection {Subsection}
t=>
1.1.1
\subsubsection{Subsubsection}
Subsubsection
{Sect ion}
Subsection
\
par agr aph { Paragraph
Paragraph
\
subparagraph { Subparagraph
Subparagraph
arti-
6.9
LATEX
219
PARAGRAPH STYLES Table 6.20:
latex Paragraph
\begin{ enumerate}
.
•
style options reference
.items.
.
.
\ end
{enumerate}
1
.
Numbered
list
of
items
\begin{ itemize}
.
items
.
\begin{description}
.
.
.
.
.
\end{ itemize}
.items.
.
.
• Bulleted list
\end{description}
Item
of items
text in the
paragraph
is
indented under item
\begin{abstract}
.
.
.text
.
.
.
\end{abstract}
abstract Indented
and
justified
text
with "abstract" centered above
\begin{quote}
.
.
.text
.
.
.
\end{quote}
Indented and
justified text
\begin{verse}
.
.
.text.
.
.
\end{verse}
Indented and not
justified text
\begin {verbatim}
.
.
.text.
.
.
\ end
{verbatim}
Format exactly as typed in constant width font
For each item
in a
paragraph
style, the
item
is
designated as shown below
for the itemize paragraph style:
\begin{ itemize} \item{Iteml text} \item{Item2 text}
\item{Itemn text} \end{itemize}
•
Bulleted List of Items
•
Iteml text Item2 text
•
...
•
Itemn text
The itemize and enumerate paragraph styles have optional arguments which enable the writer to modify how items will appear. The optional arguments are used in the same manner as for the required argument to the description format.
22U
Chapter 6
File
Formatting and Printing
begin {description} \item[Iteml] {Text for iteml. We show how the description format plays with these items.} \item[Item2] {Text for iteml. one we will make extend to a new also } \ end { deer ipt ion} \
Iteml Text for iteml. We will show how the description format displays with these items.
will dis-
— This line
Item2 Text for item2. This one we will make extend to a new line also.
.
where the description
in
square brackets, for the description format,
For the itemize and enumerate paragraph
numbering mechanism, are placed
in the
is
bold.
styles, the optional bullet type, or
square brackets. See [Lamport 1986]
for details.
FIGURES and TABLES Table 6.21
:
latex Figure
control options reference
\begin{ figure} [htbc]
.
.stuff. \end{f igure}
\begin{ table} [htbc]
.
stuff.
.
.
.
Figure environment
\end{ table}
Table environment
h
Place the figure or table here
t
whoami jhodges
Alternatively, the
guments), which will
who command
tell
!
7.3.2
who am
i (with
two
ar-
the current user/host combination, the login port, the
date and time of login, and port
localhost> who am i sfsuvaxl jhodges ttyqc Jul 18 13:35
has an option,
(e.g., tty)
of login.
(modeml3.sfsu.edu)
Anyone's Status
The finger utility was introduced in Chapter 3. finger is useful on a network to find out who is on the system, and can be used when trying to find out who is logged on to a host whether you know a user's account name or not.
finger
can also be used as a general rolodex for users, because
it
displays
i
230
Chapter 7 Network Processing
make available shown below:
general information about a user that the user has decided to the general public.
The standard syntax
for
finger
is
to
finger [name] @host where "name"
is
optional, and can be any single string of characters, repre-
senting a person's userid, their
from the command, finger
ted
host
can be a local host, a
a local host
host
is
name
and a
list
will
name,
list all
their last
name,
etc. If
name
is
omit-
users logged onto "host." Likewise,
symbolic name, or an IP address. On finger command without a host and the local you can issue the finger command without a
full internet
you can issue the
assumed. Likewise,
first
of all users on that system will be displayed. For example,
say you want to see
if
anyone
is
logged on to the host
let's
humpback:
localhost> finger ©humpback [humpback]
Login hodges hodges
TTY Idle When co lOld Fri 15:05 pi Fri 15:05
Name Jack Hodges Jack Hodges
Office
finger used without a "name" argument will humpback) for all users and return a list
this case,
tion) currently active
on the
host. In this
poll the host selected (in
of userids (plus informa-
example, the
full
symbolic address for
humpback was not necessary, because the system had an alias for humpback in its "/etc /hosts" table or equivalent. The information that is displayed along with the userid resides
The contents of the
command user
is
name
file
finger
file.
when
are displayed differently
in real
part of their life is
Jim.
finger finger a
the
issued for a specific user. For example, you can also
knowing only
user's
finger
in a
name. For example, suppose you know
that a
Then you can type
localhost> finger jim@sf suvaxl which "Jim"
will
produce a display of finger
files for all the users
in their finger file (i.e., in their userid
or their
tem. In this particular case, seven user's finger
.shown here). si
You could
king information
then try to identify which It
should be noted that
with the string
alias)
on the sys-
were displayed (but not
"Jim"
is
finger
the person
you are
does not guarantee
on the network; however, you will be able to tell when they were logged on and whether they are currently logged on because finger will
thai a user last
for.
files
name
is
finger is that a user may have a different finger file on different hosts. When you "linger a user." every host on which the user has active processes on localhost will display display the user's login status.
A
significant aspect of
7.3 Single-User Status
its "
finger
for the user. Consider the
file
jhodges"
below.
On
the
sfsuvaxl
finger command
231
issued for the user
the finger file looks as follows:
localhost> finger jhodges -« In real life: Jack Hodges Login name: jhodges 338-2335 Home phone: NA Office: TH 970, Shell: /bin/csh Directory: /usr/fl/ jhodges -* On since Jul 18 13:35:36 on ttyqc from modeml3.sfsu.edu login Project: Lets find out what makes this thing tick.
— alias .
Status
Plan:
Assistant Professor (Artificial Intelligence) Computer Science Department Office Hours: T 1-4
whereas the same user has a different account the host
(i.e.
hodges) and
finger
file
on
futon:
localhost> finger hodges@futon [futon. sf su. edu]
Login name: hodges In real life: John Hodges Directory: /Users/hodges Shell: /bin/csh On since Aug 20 15:30:33 on ttyp3 from huckleberry sfsu -* Project: Pull the wool over your own eyes!
-«
,
alias
.
.project
Plan: I plan to use this machine as little as possible, so don't plan on finding out when I log in by using this finger
.plan
-«
file.
I could have used the finger command with "Jack" "Hodges" just as easily as the userid on that host. In both cases, finger displays the login name (i.e., the userid), what is called a name alias (what follows "In real life:"), the user's office location, their home phone, the path to the user's home directory, the path of their login shell, a line
In these examples,
or
"John"
or
describing their
".project."
last login session,
and the contents of two
files,
sponse can be modified/customized by the user with the (Section 5.2.4, page 150) and
chf n
"
.
plan," and
command recommands chsh
All of the information displayed in the finger
232
Chapter 7 Network Processing
Changing Your finger
File
Users can modify the contents of their finger
file
using the
chfn command,
as
shown below. localhost> chfn Changing finger information for jhodges Name [Jack Hodges] Office number [Thornton Hall 970]: Office Phone [338-2335] Home Phone [NA]: :
:
where the bracketed contents, and the In this case,
items are the defaults from the existing finger
( [ ] )
s on
no modifications were made
lows "Name"
the
fills
name
the other fields in the finger
file,
handlers to indicate
who
email
is
some
it
may
bizarre
it,
fol-
like
and many
used by mail
not be very valuable to a
name
alias.
File
.plan and .project how the finger display a message to those who would fin-
There are two user created and configured which, in addition to
ger
in
that often this alias is
from, and
mail recipient to receive mail from
will appear.
The item that The name alias,
need not have your real name
Remember, though,
file
the user.
to the finger file.
alias field for the finger file.
users insert aliases.
The .plan
by
the right indicate the responses given
Both
chfn and chsh,
files are
used to display
files,
determine
you, and can contain whatever text you want to place in them, although a
certain
amount of discretion would be wise. The plan shown for one of my old " plan" files below: .
lines, as
file
can have multiple
.
localhost> cat .plan Assistant Professor (Artificial Intelligence) Computer Science Department Office Hours: T 1-4
This example tion that file
is
is
basically a
form of signature, where
not normally found in the finger
quite elaborate, and
one fingers them.
It
was providing informa.
plan
sometimes even have a program executed when some-
should be mentioned that w hen someone fingers you. they
often just want to find your login
Deluging them with taste,
file.
I
Users often make their
lots
ID or
to find out
when you
last
logged on.
of information could be considered a form of poor
so you should be careful in selecting the content of these
files.
7.3 Single-User Status
The .project The
pro j ect
.
233
File
file is
a shorter, one-line,
file,
as illustrated below:
localhost> cat .project Lets find out what makes this thing tick.
New
accounts are generally given a stock
resident
somewhere
want
modify the information
to
likely
have
to create these files
placed in your
home
.plan and .project which
system other than the user's account, so
in the file
in
your
.plan
and/or
if
.project, you
are
you will
with a suitable text editor. They should be
directory.
Your Login History
7.3.3
One of the
ways to determine a user's usage is with last, last lists the from some date (usually since the last time the wtmp file the present. Each entry in the history looks as follows:
best
user's login history
was deleted)
to
userid terminal host login-date time-on-off (login-time)
last
is
often used to determine whether a user
is
currently active. If the
"time-on-off" and
" login-time) " entries of the last response history cannot be filled in. and will say "still logged in." For example, consider the login history below:
user
is
currently logged on. then the top-most
localhost> last jhodges more jhodges ttyqc modeml3.sfsu.edu jhodges ttyp2 huckleberry sfsu jhodges ttyp7 modeml3.sfsu.edu jhodges ttypO Matterhorn COGNE jhodges ftp toaster.sfsu.edu jhodges ftp sf suvaxl sf su.ed jhodges ttyp3 huckleberry sfsu jhodges ttyp2 oad-8csld. sf su. e jhodges sutro.sfsu.edu ttyp7 jhodges couch.sfsu.edu ttypa jhodges ttyq5 telnet.sfsu.edu jhodges ftp huckleberry. sfsu jhodges Maui.CS.UCLA.EDU ftp jhodges ftp Maui.CS.UCLA.EDU jhodges ftp huckleberry. sfsu
(
|
.
.
.
.
Thu Jul 18 13:35 Wed Jul 17 17:53 Tue Jul Mon Jul Fri Jun Sun Jun Sun Jun Sat Jun Thu May Sat May Mon Mar Sun Feb Thu Feb Thu Feb Tue Jan
16 1
21 16 16 8
30 11 11 24 14 14 29
09:06 23:44 00:30 15:47 13:12 11:37 17:03 14:47 10:22 14:08 13:45 13:43 20:20
still logged in 17:54 (00:01) 09:06 (00:00) 23:45 (00:00) 00:33 (00:02) 15:47 (00:00) 16:57 (03:45) 11:50 (00:13) 17:09 (00:06) 14:48 (00:00) 10:36 (00:13) 14:08 (00:00) 13:45 (00:00) 13:43 (00:00) 20:28 (00:08)
® (2)
(3)
(5)
(5)
wtmp begins Wed Dec 31 16:00
The
login history above
items selected. The history
is
a truncated version of the real listing, with a
shown above
indicates a
number of
few
things about a
2o4
Chapter 7 Network Processing
you look closely at a last response, you can tell what a user's schedwhere they do their work, where they hide, and other good things. Consider the output above. When the history was taken the user was still logged in from modeml3 sf su edu (item 1). The other entries show the amount of user. If
ule
is,
.
.
"login-time" in parentheses (hours:minutes). For example, on Sunday, June 16
"jhodges" logged on
huckleberry
at
at
13
:
12
(i.e.,
noon) for (03:45), or 3 hours and 45 minutes (item
"jhodges"
terhorn
logs in from
[item 2],
many
Maui
1:12 in the after-
3).
Also note
that
huckleberry, Mattoaster, sutro, couch [item 4]),
different hosts (e.g.,
[item 5],
from different networks (e.g., sf su, UCLA), and by different modes (e.g.. direct and modem). Finally, note that "jhodges" logs in at any time of the day between 9 a.m. and midnight, usually for a short amount of time, but sometimes for extended periods.
One
to display all login sessions
note of caution. The
from the
last
last command
time the system
file
is
going
"wtmp" was reHence the use of
many cases, this could result in a very long listing. more above. You could also use the head command, but there are options to the last command so that this isn't necessary. For example, if you use the '-N' moved.
In
option,
where
will
'N' is a
be displayed
number, then only
N
lines of the total
localhost> last -2 jhodges jhodges ttyqc modeml3.sfsu.edu Thu Jul 18 13:35 jhodges ttyp2 huckleberry sfsu Wed Jul 17 17:53 .
7.4
number of
lines
in the response:
-
still logged in 17:54 (00:01)
Network-User Status to look for activity on the network. The more obvious which you are looking to see if specific users are logged in, so that you can get in touch with them. The other case is one in which you are trying to find out what the big jobs are, who is running them, and when they run their jobs so that you can do your work some other time (or on some other host). Either way, it is important to be able to look at the activity on the network and understand what it means. "Activity," in this context, means ( ) the users who are logged in and (2) what they are doing. There are two ways to
There are two reasons case
is
one
in
1
determine activity across the network. The
who
first is to poll
the
machine on
the
The second is to poll the machines on the network for how active they are. These methods can be accomplished with the who. users, and w commands. network
to see
is
active.
7.4
235
Who Is Out There
7.4.1 If
Network-User Status
you already know who a user
is
on the network, having determined
their use-
finger, then a simple listing of who is on the system tells you if they are there or not. You can then decide whether you want to be on the system or not. Two commands are used to poll the network for users. The who command polls the local host for active users. A sample who is shown below for the host localhost: rid with
localhost> who shahryar ttypO vcsc2087 ttypl vcsc2162 ttyp2 arichard ttyp4 rlee ttyp5 fbuenaf ttyp6 davidm ttyp7 vcsc2107 ttyp8 azhao ttyp9 vcsc2095 ttypb vcsc2123 ttypd homelsl3 ttype vcsc2177 ttypf vcsc2085 ttyql goldstei ttyq2 vcsc2014 ttyq3 jchan ttyq4 vcsc2106 ttyq5 pbourne ttyq7 ttyq8 j cheung vcsc2185 ttyq9 chance ttyqa vpcsc4 ttyqb jhodges ttyqc vcsc2105 ttyqd
Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18
09 13 13
11 12 12 10 13 13 12 13 13 13 11 11 12 10 13 13 13 13 13 13 13 13
01 13 12 10 02 49 42 12 01 22 27 38 18 20 22 04 46 28 20 21 26 32 34 35 37
(rincon. sfsu. edu) (hh-3 02dync sf su (bus212dl0 sf su. (gisps2-l sf su. ed (ac-pc2 .sfsu. edu) (oad-8csld. sf su. (
(
.
(
.
{
.
(
(
(lib-el45csla.sfs lib-maccs2a. sfsu (ac-allen. sf su. ed (bus212d21 sf su. (telnet .sfsu. edu) futon. sfsu edu) (oad-8csld. sf su. (lib-maccs2a. sfsu (mdmsrv4 sdsu edu (oad-8csld. sf su. (ac- jchan. sf su. ed (138.202.2.1) (modemll sf su edu (russian. sf su edu (bus212dl9 sf su (net -chance .sfsu. (telnet .sfsu. edu) (modeml3 sf su edu (modemll sf su edu (
(
(
(
.
(
(
.
(
(
(
.
.
.
.
(
(
(
(
(
.
(
.
.
(
(
(
.
.
(
.
.
This was only a partial
list!
The who command
is
output with a simple for-
mat:
[USERID] [TTY] [LOGIN-DATE] [LOGIN-TIME] [LOGIN-SITE] Note
that the user
IDs appear
to
be listed
who
listing
can be useful
random
(i.e.,
when the who command is may be dramatically different.
a host. In contrast,
number of users
in
in
order, since the
TTY, determining how much load
ordered by which device they logged in from
list is
the second field). is
likely to occur
A on
issued on a workstation, the
236
Chapter 7 Network Processing
huckleberry [1 who hodges ttypO Aug 13 14:31 (modeml4.sfsu.edu) ]
The remote who command, rwho, performs
rwho traverses the local network that command on our example LOCALHOST
the
same function
as
who ex-
cept that
the host
this
proves fruitless because
no
local
network
to
which
it is
is
attached
to.
Issuing
has
it
connected:
localhost> rwho localhost>
Conversely, issuing the same
worked
to
many
command from
a workstation
which
net-
is
other (and sometimes heterogeneous) hosts, can be entirely
different:
huckleberry [2] rwho eisman toaster ttypl Aug 13 13:43 :08 Jessica futon:ttyp3 Aug 13 14:23 :01 sheehan walnut ttypO Aug 13 14:02 :
:
In this example, notice that the syntax of the table
is
slightly different:
[USERID] [HOST:TTY] [LOGIN-DATE] [LOGIN-TIME] [IDLE-TIME]
so this
command, when
effective, additionally provides information about
long the user has been idle
command).
(i.e.,
Specifically, the user
how "
j
for about
one minute, so we know
'-a'
option to
(all)
rwho
is
long
it
essica,"
in the table
above, has been idle
that she is currently very active.
issued, all local networks are polled
ing users are shown, again for a workstation
how
has been since the user issued a
hooked up
When
and the
the
result-
to a local network, as
follows:
huckleberry [3 rwho -a eisman toaster ttypl Jessica futon: console Jessica futon:ttyp3 sheehan walnut ttypO sherif futo/ ttypO ]
:
:
:
Aug Aug Aug Aug Aug
13:43 08 14:22 99 59 01 13 14:23 13 14:02 13 09:33 4:35 13
13
Notice that the display for to
on
rwho is different from that of who, but rwho lists the userids, the host the\
and longer than that for last, lo.
similar
logged
the data and time they logged on. and the amount of idle time they have
7.4
had since
their last
command. This
Identifying User
7.4.2
The second command
is
similar to the
Names on
to find out
the
last command
to the
entry.
Network
what users are on the network
is
users,
whoami for a single user, lists the userids of users currently system. An example for localhost is shown below:
which, like
on
237
Network-User Status
logged
localhost> users epearson jason jhodges jkelley ramberg schwartz vcsc2195 vpcsc21
However, when
rusers)
is
issued,
the similar remote version of the
we
see that the
command
is
users command (called
not supported:
localhost> rusers Command not found.
Issuing the
users command on
a workstation provides the following re-
command rusers
now
sult:
huckleberry [4] users hodges and the remote
is
supported:
huckleberry [5] rusers stars. sfsu.e cook carpente gibbs.sfsu.e gronerts esmeralda s f aragons futon. sfsu.e Jessica sherif Jessica walnut sheehan homer. sfsu.e jason lisa.sfsu.ed goel lentil. sfsu. levine bart.sfsu.ed bregler kumquat.sfsu kroll marge. sfsu.e goel quark. sfsu.e ramberg huckleberry hodges .
t
t
host
userids
work, while the remaining items to the host.
For example. Jessica,
sf su edu. This means .
name of a host on the netgiven row are names of users logged on
any row
In this example, the first item in in a
sherif,
is
the
and Jessica are logged on
that Jessica has
two active command
to
shells.
futon
.
-
Also note
2oO
Chapter 7 Network Processing
logged on to both
that user goel is
lisa. sfsu.edu and
marge, sfs-
to
u edu. .
Host Information and User Information
7.4.3
Finally, the
the
w command
w command
is
integrates
an integration of who and
uptime
uptime. As mentioned,
and who. The command displays who
logged in and what they are doing. Below
is
an example:
is
localhost> w — 4:14pm up 9:15, users, load average: 1.04, 1.01, 0.76 User tty from login© idle JCPU PCPU what -* pbourne pO modemll sf su. 4:13pm mail vpcscll pi dh9.uccs.edu 3:59pm 2 2 rn jhodges p2 huckleberry sf 4 06pm w shahryar p3 rincon. sf su. ed 2 :10pm 18 i2 robinson p5 modemll sfsu. e 12:12pm 12 17 -csh jchan 2:08pm 05 -csh p6 ac- jchan. sf su vpcsc21 p7 lib-maccs2a sf 3:18pm vi dis.out -* wkwong -csh p8 oad-8csld. sf su 4:07pm
line 1 like
uptime
,
.
.
:
other lines similar
.
to
who
.
.
As you can
see, the first line is identical to
on the system. Notice
lines represent users
user
is
doing. Very nice!
The columns
w
1
The
Field 2
The name of the
Field 3
The host from which
Field 4
The time of day
Field 5
The number of minutes
Field 6
The
CPU
time used by
Field 7
The
CPU
time used by the currently active process
Field 8
The name and arguments of the
Field
a.
A
the remaining
shows what
the
are explained as follows:
7.1
Table
uptime, while
that the last field
Display field descriptions
tty is a special
user's login
device
file
name tty
a
(userid)
the user the user
is
is
the user logged
logged
in
on
logged on
on
since the user last issued a all
command
processes and their children on that terminal
current process
associated with a terminal
line.
You can force w to display only information for a particular user by issuing command with the userid (e.g., w userid). Because the w command provides such a variety of information, is often the only command used to check
the
it
US(
is
and system
status.
7.5
239
Network Status
7.5
Network Status The commands who and users on
to a particular
ever, all
you
really care to
the network, or the
are excellent
ways
to find out
know
whether a particular host
is
what the processing load on
a particular host
commands ping, uptime and ruptime can be
Sometimes you
will
have a
difficult
performance of the network
that the
In such cases,
is
logged
you
will probably
is
is.
accessible on In such cases,
used.
Host
Statistics for a Specific
7.5.1
who
system and what tasks they are working on. Sometimes, how-
time connecting to a host, or you will find isn't
want
what
might be on other occasions.
it
to find out if the
problem
is
with you or
with the machine you are trying to work with. This can be easily accomplished with the
ping command,
as
shown below:
localhost> /etc/ping huckleberry.sfsu.edu PING huckleberry.sfsu.edu: 56 data bytes 64 bytes from 198.137.218.1 icmp_seq=0 time=5. 64 bytes from 198.137.218.1 icmp_seq=l. time=l 64 bytes from 198.137.218.1 icmp_seq=2 time=l. 64 bytes from 198.137.218.1 icmp_seq=3 time=l. 64 bytes from 198.137.218.1 icmp_seq=4 time=l
huckleberry PING Statistics packets transmitted, 5 packets received, round-trip (ms) min/avg/max = 1/1/5 localhost>
0% packet loss
5
When
the
ping command
is
issued with the
local host sends a data packet to the
make
doesn't is
a difference to the
screeen. This process
for the
[
-r
]
[
remote host
name of a network (it
command), which
host, the
could be the local host,
is
then sent back.
it
A packet
sent every second, and the statistics for each packet are displayed on the
ed by the user
ping
ms ms ms ms ms
-v
is
is
ping command ]
host
continued
until the
number of counts that was requestis terminated. The general format
reached, or until the process
[
is
shown below:
packetsize
In this template, the '-r'
[
count
and '-v' options
tions) will not be discussed here,
man page
]
]
(i.e.,
routing and verbose op-
and the interested reader should see a
refer-
more details (man ping). The host is the required argument to the command, and both the packetsize and the count (the number of transmissions to make) can be provided to the command. For example, the
ence or the
for
240
Chapter 7 Network Processing
previous example could be modified as follows without changing the response:
localhost> /etc/ping huckleberry.sfsu.edu 64
5
The results of a ping command can be very useful to even the novice network user, because it provides network load information. While packets are being sent, each packet has a status line that looks as follows: 64 bytes from 198.137.218.1: t
t
packet size
icmp_seq=0
time=5. ms
.
t
originating nost
The
t
time (msec)
trip
.
count
last item, the trip time, is a
reasonable indicator of the status of the
connection between the two machines (your local host and the one you are
pinging). The of
from the command shows a cumulative summary where the number of packets sent and received are not-
final display
the packets sent,
all
ed, and, importantly, the percentage of packets lost in the process.
huckleberry PING Statistics packets transmitted, 5 packets received, 0% packet loss round-trip (ms) min/avg/max = 1/1/5 5
good indicator of the network connection. When the remote number will typically be 100%. The last value gives an effective measure of the network load, since, over the time it takes to run the number of packets sent, the average load can change measurably. This
host
also a
this
Host Processing Load
7.5.2
When
is
down,
is
a host
formance
is,
is
up and running,
in
terms of
it
is
how many
often useful to look at what
its
actual per-
effective users are running jobs, over a pe-
riod of time. This can be accomplished with the
ample below shows the format which
uptime
uptime command. The
ex-
displays:
localhost> uptime 4:26pm up 9:42, 8 users, load average: 0.84, 0.74, 0.62 k
1
There are ten second lon
minute
5
minutes The
fields in the display.
field tells if the host is
the host has been
up
"up"
(in this case
(i.e., (
>
15 minutes
first field is
running).
the current time.
The
third field tells
and 42 minutes). The fourth
The
how
field tells
7.6
how many
users are active, and the fifth field
the fourth field.
The
sixth
and seventh
is
Remote Processing
a text field
"average")
for the last three fields. Fields eight => ten
ages over the
last
5,
l,
(i.e.,
fields are text fields
and 15 minutes, where load
of users. The following example shows that
"users") for "load" and
(i.e.,
show
the load aver-
measured
is
in the
number
LOCALHOST cannot display load
ruptime, because it isn't on commands rwho and rusers:
information for other machines using
work, as was seen with the
241
a local net-
localhost> ruptime no hosts
!
?
The same command, when issued from
a workstation on a local network
produces a different result and form:
huckleberry [6 nipt ime couch up 6+20:39, cs
futon huckleberry springfield toaster walnut
up 6+00:25, up 5+22:34, up 3+16:03, up 112+13:01, up 99+00:32, up 24+14:07,
1 1
1 1
There are nine
users, users, user, user, users, user, user,
load load load load load load load
fields in this table.
00, 1
00,
00, 1
00,
00,
00,
00,
00,
00,
00,
00,
00,
00,
00,
The
1
first field is
00 01 00 00 00 00 00
the
name of a
huckleberry. The second field tells whether the host The third field tells how long the host has been up. The "N+" as
is
host, such
currently "up."
syntax means that up for "N" days "plus" whatever time follows in hours:minutes format. The fourth field tells how many users are logged on to the host. The fifth field is just the text field (i.e., "user") for the fourth field. The sixth field is the text field (i.e., "load") for the seventh => ninth fields, which tell what the average load (in users) has been over the last 1, 5, and 15 the
host
has been
minutes.
7.6
Remote Processing The notion of multi-user computing environment is associated with the ability to run many shells at once. The system does not know who the owner of each shell process
ular machine,
is,
so any particular user can be running processes on any partic-
and any particular user can be running multiple processes on any
machine he/she can log on
to.
In this section, the different
user can run multiple shells or shell troduced.
methods whereby a
commands on remote machines
will
be
in-
A remote machine is any machine other than the one the user is cur-
The notion of running a shell on a machine which is located somewhere other than where the user is located is called remote processing.
rently logged onto.
242
Chapter 7 Network Processing
Remote processing
the essence of a shared resource network, since a user
is
can run processes on machines which are less heavily loaded as well as on the one where they are sitting. In this section, you will learn how to perform three tasks:
send and receive
•
with other (remote) hosts
remote login to or from another host on a network
•
create and execute shell
commands on remote
hosts
Experimenting with Remote Processing
7.6.1
Because you most this
files
•
chapter
do not have accounts on other machines, and since specifically about performing work on other machines, do not
is
likely
UNIX does not care, when you issue a command, if you are sitting at machine you issue the command from. Thus, if you want to test any of the commands you will learn about in this chapter (except anonymous ftp), you despair.
the
can issue the
command
with the host argument being the machine on which
your account resides. For example, f oobar
.
if
cs oz edu, and you want .
.
you have created lowing command: as
a
" .
rhos ts"
localhost> rep foobar.es. oz.edu: blah
f oobar
you have an account on the host experiment with rep, then (as long your directory) you can issue the fol-
to
file in
.cs.oz.edu rblech
"blah" appear
and you should see a duplicate of the
file
home directory. All of the commands this way (i.e., using your own host as
in this section
File
7.6.2
The
"blech"
in
your
Have
fun!
Transfer Protocol
File Transfer Protocol, or
one machine
a target).
as
can be experimented with
ftp,
is
commonly used
to another without the use of
to transmit files
magnetic tape media,
cially useful for single-file (vs. directories) transfers, so
it
is
ftp
good
is
from espe-
to use with
Tape ARchival (tar) and compress utilities. Some of the command ftp is given below, and more details can be found in the man page (man ftp). the
syntax for
ftp
[
-v
]
[
-i
]
[
host
]
The '-v' option stands for verbose mode, in which case ftp will display it does. The '-i' option turns off interrogation when multiple file transfers are being made (e.g., with mput or mget). Normally when perform-
everything
ing these tasks client host
ftp
will ask for authorization
(machine) that
ftp immediately
ftp
is
to
on each
file. If
communicate with on
the
you specify
command
the
line,
attempts to establish a connection to an ftp server on that
Remote Processing
7.6
host. Otherwise,
from you.
it
typical invocation
command
will enter its
When ftp
is
may
interpreter
awaiting commands,
it
and await instructions
prompts you with
'f
tp>.' The
look as follows:
localhost> ftp huckleberry.sfsu.edu Connected to huckleberry.sfsu.edu 220 huckleberry FTP server (Version 5.18 (NeXT 1.0) Wed Aug 23, 1989) ready. Name (huckleberry.sfsu.edu:): hodges -* 331 Password required for hodges. MY -PAS SWORD ** Password: 230 User hodges logged in. ]
[
243
(T)
@
ftp> Is 200 PORT command successful 150 Opening ASCII mode data connection for file list. .
.
.
DIRECTORY LISTING
.
.
-« ftp> quit 221 Goodbye.
(T)
The user typically is provided with a default login which can be overridden as shown in the example (at item request
is
made and
the user completes the login
in square brackets, 1).
Then
a password
on the remote host (item
2).
commands the user wants to execute, such as 'Is' in this example, the quit (or bye) ftp command is issued to log off the remote host (item 3). Some of the more commonly used commands are described in After issuing whatever
Table
Table 7.2:
7.2.
ftp Command reference
open HOST
Establish a connection to the specified host
pwd
Display the
name of the
cd DIR
«•
Change
the
working directory
to
DIR on
the remote
led DIR
Change
the
working directory
to
DIR on
the local
Is DIR
Display a directory
listing
of
DIR
on the remote machine
lis DIR
Display a directory
listing
of
DIR
on the
mkdir DIR
Make
rmdir DIR
Delete a directory on the remote machine
delete FILE
Delete the fde on the remote machine
get FILE
Retrieve the
a directory
file
ftp
server.
HOST
current working directory on the remote machine
DIR on
the remote
named FILE and
local
machine
machine
machine
machine
store
it
on the
local
machine
244
Table
Chapter 7 Network Processing
7.2:
ftp Command
mget FILES
reference (cont)
Retrieve the specified
FILES from
the remote machine:
FILES
can be
specified with wildcards
put FILE
c=>
Store a local
mput FILES
Transfer multiple local
file
named FILE on files
the remote
(FILES) from
machine
the current local directory to the
current working directory on the remote machine:
FILES
can be specified
with wildcards
send FILE
Perform the same functions as put
status
Show
Invoke a
? command
Obtain help for the
command
binary
Set the
mode
bye, quit
Terminate the
ftp
session with the remote server and exit
close
Terminate the
ftp
session with the remote server and return to the
i
the current status of shell
file
command
One
ftp
on the local machine
transfer
to support binary
image transfer
ftp
of the truly amazing capabilities offered by networked computers
our access to other machines and applications across a network.
ample
I
ftp
interpreter
will present is
trieve files
anonymous
ftp
which
is
used to send
is
One such
ex-
and
re-
files to
from remote hosts where you do not have an account, usually called
public archives.
Copying Archived Remote
Files
Occasionally you might hear of software databases (perhaps in a newsgroup, in
email, or from friends) and want to gain access to the software located there.
you might find yourself looking
for useful information
on
other institutions and other locations.
Many machines have
an
In other situations
machines
at
anonymous ftp
capability that enables users without accounts to log in and
acquire software. Machines with this capability are generally called archives. I
have included the protocol for using
this utility in the next
example.
Remote Processing
7.6
245
localhost> ftp huckleberry.sfsu.edu Connected to huckleberry.sfsu.edu. 220 huckleberry FTP server (Version 5.18 (NeXT 1.0) Wed Aug 23, 1989) ready. -* anonymous Name (huckleberry sfsu. edu: jhodges) 331 Guest login ok, send ident as password. -* Password: [[email protected]] 230 Guest login ok, access restrictions apply. .
(V)
:
[2J
— ftp> Is 200 PORT command successful. 150 Opening ASCII mode data connection for file list.
(T)
bin etc
pub forward .hidden .
.places 226 Transfer complete. 37 bytes received in 0.01 seconds ftp> quit 221 Goodbye.
(3.6 Kbytes/s)
example above, the ftp prompt is "f tp>," and my responses are shown after these, in bold. What I did was to issue the ftp command with a In the
local
machine,
a version of
huckleberry,
"TeX3.0" on
(i.e.,
as an argument.
The procedure
when asked
user ID (at item 1) and,
address
it.
I
already
to use the
typed
I
as
had
your
your mail
"jhodges@sf-
It
listed the available directories (item 3).
anonymous
this host
was not echoed because this login is part of and expects a password. Once I was accepted, I then
(item 2).
the standard login script
knew
"anonymous"
for your password, to type in
userid and host address). In this case,
suvaxl .sfsu. edu"
al,
is
Really quite straightforward. In gener-
users are given severely restricted access to the machine, usu-
ally to specific directories
ample, the directory
I
where public software
have been given access to
is
is
kept. In the preceding ex-
the
"pub"
directory.
To use anonymous ftp to look around on the network, you must know to look. One way to find out where archives of a particular sort are is through newsgroups, at least this is the way it was done until very recently. If where
you were
later
chives for that topic reside.
around.
and subscribe to the appropriate newsyou would see mention of locations where the ar-
interested in a particular topic,
group^), then sooner or
Of
course, this
You
method
could then
still
ftp
to the archive
and look
works, and you can also ask friends
who
might remember where an archive resides. Recently an online database has been developed to keep track of
ftp
sites
around the globe, along with servers to query the database from anywhere on the network.
The
utility is called
archie
and was originally written by Alan
£hO
Chapter 7 Network Processing
Emtage and Peter Deutsch
at
archie
McGill university,
has
become an
valuable aid to help users around the globe locate and acquire software.
It
in-
also
is now a consistently updated command line like any UNIX supported on your host. To do
helps to reduce replication in archives since there
database of
ftp
utility. First
you have
so, use the
archie
sites,
used
is
archie
to find out if
which command
at the is
"archie"
with
as
its
argument:
localhost> which archie /usr/ local /bin /archie
This will work
if
archie
is
located in any directory in your search path.
it may still reside on the system, in which case you might try invoking man page (man archie). If all else fails, you could request assistance from the system administrator. Below is a list of options for using archie.
If not,
the
followed by a simple session using
Table
7.3:
it.
archie Command
reference
-c
A
-e
An
-r
A
regular expression search
-s
A
case insensitive substring search
-1
List
-t
Sort inverted by date
-m #
Specifies the
-h HOST
Specifies the server
-N#
Specifies query niceness level (0-35765)
case sensitive substring search exact string match
one match per
line
maximum number.
#.
of hits to return
HOST
Using these options, you can now access archie and locate an archive site. For example, I have decided to look for no more than five (5) sites which support latex. I use the -m' option to select the number of sites to display, and '
"LaTeX"
I.
Many
systems have
interested users.
a
is
"help"
You can send
want
the string
I
newsgroup
that
is
to search for.
Since
I
did not use an option, the
monitored by the system administrator and other
mail to this address
if
it
exists, or
>ou could send mail
to "root."
7.6
search
is
based on an exact match. Below
is
Remote Processing
the output
24 1
from the command.
localhost> archie -m 5 LaTeX Host cnam.cnam.fr Location: /pub/TeX/TeX3/TeX3 DIRECTORY drwxrwxr-x 512 May 4 1991 LaTeX Host emx.cc.utexas.edu Location: /pub/mnt/ source/ tex/tex-3 14/TeX3 14 DIRECTORY drwxr-xr-x 512 Jan 15 00:00 LaTeX Host ics.uci.edu Location: /mac DIRECTORY drwxrwxr-x 512 Aug 17 04:15 LaTeX Host liasun3 epf 1 ch Location: /pub/tex/tex3 14-imake202 DIRECTORY drwxr-xr-x 512 Aug 16 20:15 LaTeX Host walton. maths ted. ie Location: /src/TeX/TeX3 14 DIRECTORY drwxr-xr-x 512 May 8 1991 LaTeX .
.
.
.
.
.
.
.
There are three
lines in
of the response. The
each response, as illustrated by the shaded portion
on the
the host symbolic address
first line identifies
net-
ics.uci.edu. The second line identifies the directory in which the search item resides, such as "/mac." The third line describes the directory in which the search item resides, which should have the search item in work, such as
its
name. This
will essentially
be the output from "Is
-1' on the directory.
mind that ftp uses the network and that you should use some discretion about when to transfer files from one site to another. Generally speaking, the later the better. Most users agree that anonymous ftp should be used only during nonprime hours (i.e.. 1900 - 0600 hours local time for the remote site, although it might be considerate to select non prime-time hours on both ends of the network, when possible). Bear
7.6.3
in
Remote Copy
Remote copy
is
probably more useful than
ftp
in
some circumstances, be-
cause you needn't actually log on to the remote host to copy a
however,
file;
you must have an account on the target machine. In order to use remote copy, you must have, or create, a file in your home directory named " rhosts." This file must reside on the machine which you wish to log onto, so you must .
have an account on that machine. The tries,
.
rhosts
each of which has a machine address and a
file
list
consists of a
of userids.
list
Any
of en-
user/ma-
rhosts file can rlogin to your account without a password and can use rep to copy files and directories from your account or to your account. As you have probably heard. rhosts files chine combination that exists in your
.
.
are dangerous for system security, because they allow
one of your accounts the potential
to log
on
to other
anyone with access
machines
easily.
to
However.
24u
Chapter 7 Network Processing
there are advantages, and
A rhosts .
file
rep is
one of them.
must reside
in
your home directory, and has a
each user and platform that you want to have access to your account.
dummy
file
line for I
have a
below:
hobbes.cogent.ucla.edu freebie fuzz.cogent.ucla.edu zorch laina.cs.ucla.edu corrigan huckleberry.sfsu.edu smith sfsuvaxl.sfsu.edu cohen f
t
host address
userid Note
list
"zorch"
that user
which the sample but not from other
.
at
UCLA has been given access to the
account in
rhosts file resides when she logs on from the host fuzz hosts. You may want other users to be able to test software
or edit files that belong to you, without the hassle of copying files through other
means. One very important thing about a
.
rhosts
file is that
it
be made readable by others, since they might then obtain access
who
must never to
someone
has privileges to your account. They might also be able to break the pass-
word of someone who has access yours. Using remote copy
is
to
your account even
quite simple, as evidenced
if
they cannot break
by the following
ses-
sion:
localhost> rep -r maui.cs.ucla.edu: /usr/lib/tex/macros sf suvaxl:Texmacros
This example illustrates the coping of the entire tree
(i.e.,
recursively) from the host
macros" host as
"macros"
.
.
sf suvaxl. The colon ) is used after each template below. You need not be logged on to either ma-
directory on the host
shown
in the
directory sub-
maui cs ucla. edu over to the "Tex(
:
chine to perform this task, as long as your userid exists on both systems in a .
rhosts
file:
rep flags hostlrpathl host2:path2
and
When you really rep all the time.
you end up using
way.
It
UNIX
just turns out that,
you
will
probably use
when you have
kinds of machines to do
it.
and
rlogin
lots
of work to do.
rlogin
enables you to
commands on any machine as though you are sitting at the madirectly, telnet is similar to rlogin, but I will present a session any-
execute shell chine
all
get rolling with
Remote Processing
7.6
249
Remote Login
7.6.4
r login stands tem looks
for
"Remote LOGIN." When you
into a file called
"/etc /hosts"
chine) that you name. If you have a
.
issue the
command,
the sys-
(or equivalent) for the host
rhosts
file
and entry on
that
(ma-
machine,
then you are logged in as though you had logged into that machine directly. If
you do not have a rhosts file and entry on that machine, then you are asked for your password on that machine, and. if you provide it correctly, you are .
logged on. the
I
will first
show you how
to use
it,
and then
I
will describe
some of
commands:
localhost> rlogin sfsuvaxl.sfsu.edu -1 jhodges last login: Thu April 18 17:36:16 from huckleberry sfsu ULTRIX V4.1 (Rev. 52) System #2: Thu Jan 17 11:53:02 PST 1991 You have mail sf suvaxl> .
command
This
could have been issued without the "-1
jhodges' and
it
would have worked, but it would have asked for a password. For the purposes of this example I created a rhosts file. With that file I was able to log in .
without using a password. Notice .
login and cshrc .
that, as
soon as
I
execute the
command, my
on the host sf suvaxl execute and the new
with the standard output on
my
local terminal.
Once you
shell is run
are logged
on
to the
remote host, you can perform any task that you can perform from that account
when logged
in directly.
hence your path variable,
etc.. will
that the shell scripts
which
are run,
and
be those on the target machine.
telnet
7.6.5
telnet same
Remember
as
is
very similar to both
rlogin. and
ence between
telnet
the syntax
and
rlogin is
rlogin
and ftp. The functionality
is
the
very similar to ftp. The primary differis
that
no
.
rhosts
file is
used/required
telnet. On the other hand, you cannot use rep (or rsh) either. When you use telnet, the entire login script will be executed, so you will be for
prompted
for both
your ID and password. Consider the sample session and
commands shown on
the next page.
^DU
Chapter 7 Network Processing
localhost> telnet sfsuvaxl Trying 130.212.10.102... Connected to sfsuvaxl.sfsu.edu. Escape character is ,A ] Ultrix-32 V3.1B (Rev. 10) (sfsuvaxl.sfsu.edu) login: jhodges Password: Last login: Thu Apr 18 17:37:14 from huckleberry sfsu ULTRIX V4.1 (Rev. 52) System #2: Thu Jan 17 11:53:02 PST 1991 You have mail. sfsuvaxl [1] '
.
.
Should you not succeed will
In such a case the
Table 7.4:
in
logging on to the machine of choice,
'telnet>' prompt following commands will be useful:
communicate with you with
telnet Command
Open
close
Close a
quit
rsh sfsuvaxl -1 jhodges Is F90/ Mail/ README. TeX3.0* TeX3 .0/
TeX3.0.tar* bin/ core*
localhost>
local terminal,
and
quits.
is
as-
Notice that
have access will
need
to
to.
have provided the remote host name, the account which
I
is
I
command to execute. To use rlogin and rsh. you rhos ts file on the remote machine with an entry for you.
and the
have a
because there
251
System Management
7.7 File
.
no request
for a
command
note that, after the
password from the
shell using this utility.
has executed, control
Also
returned to the shell on
is
localhost.
7.7
File
System Management All network users have the responsibility to maintain their accounts in such a
way
as to optimize resources for all users. This
to clean
be able
up
after yourself
and back up your
to generate statistics
on your
means
that
files regularly.
local host
you should be able To do so, you must
(where your directory
is
ed), as well as
network-wide. In general, you should strive to eliminate
your director,
that are
if
unneeded. such as binaries not being used regularly
the binaries are being used regularly, the source files.
need both
at the
".log." ".bbl."
same
and
time.
".dvi"
As an example,
You
or,
generally do not
latex files ending with ".aux."
can be removed since they are produced as a result of
tex compilation. These and other
cussed already. In
locat-
files in
this section,
you
commonsense
guidelines have been dis-
will be introduced to utilities for obtaining
information about your disk space allocation, your usage of disk space, the sys-
tem overall usage of disk space, and two
utilities for
managing your personal
system.
file
Account allocation Disk free space Disk usage
7.7.1
File
and directory archival
File
compression
Account Allocation
Each account can
potentially be allocated a specific disk space limit.
system administrators invoke disk space
amount of disk space allocated
to
limits, and.
when
Not
all
they are used, the
accounts varies with institution, with depart-
ment, and with users. There are two important things to remember about ac-
count allocations (or quotas):
and current usage with the allocation increased
if
(
1
)
that
you can determine what your quota is and (2) you can possibly get your
quota command,
you can
justify
it.
The quota command is invoked under different circumstances. You may to see how close you are to your limit before trying to transfer a large file
want
You might also want to check your quote before executing which might exceed your quota, because you may lose some of your work. The easiest way to find out is to issue the command.
on
to
a job
your account.
2o2
Chapter 7 Network Processing
quota [user] where user represents a userid and
mand
is
is
When the comown userid, as il-
an optional argument.
issued without a user argument, the default
your
is
lustrated below:
localhost> quota Disc quotas for jhodges (uid 326) Filsys current quota limit fwarns /usr/fl 94474 95000 100000 /usr/cp2 19 95000 100000
quota
will return a table
being used on the various
quota
files 4438
limit
#warns
10
showing what your allocation
is
and
how
it
is
systems your account current has disk space us-
file
and a limit which is generally set to 5you exceed your quota, then you will be notified at not be allowed to perform tasks that would increase
age. Generally an account has a quota
10% beyond your quota. the terminal and
you
If
will
your disk space usage.
Table 7.5:
quota
Field description
Filesys
The
file
current
The
current disk space usage on this file system (in 1024 byte blocks)
quota
The
user's quota on this file system
limit
The maximum disk space
#warns
The number of warnings
files
The number of files using
system for which allocation
is
being reported
limit for user
that
on
this file
have been posted
system
to the user
the current disk space
Display Free Disk Space
7.7.2
Users can determine the current status of mounted drives on the local host with the
df command. A mounted device
is
knows how
drive or a tape drive, which the computer
with
it
a device file
tecture are
df has and
and a
encouraged
local directory.
to take a
space
is
Those
available, as
tile
to address
interested in
course speciali/ing
displays a line tor each mounted
how much
a peripheral device, such as a disk
in
computer
in
archi-
the topic.
system, telling how
shown
by associating
Table
7.6.
much space
it
7.7 File
Table 7.6:
df
System Management
20o
Field description
Filesystem node
Location and name of associated device
Total kbytes
Total (unformatted) space for the device
Kbytes used
How much
of the space has been used
kbytes free
df Filesystem Total node kbytes /dev/raOa 15551 /dev/raOd 201919 /dev/raOe 201919 /dev/raOh 257407 /dev/rala 7423 /dev/rald 201919 /dev/rale 201919 /dev/ralh 265575 /dev/ralf 404127 /dev/ra2a 7423 /dev/ra2d 201919 /dev/ra2e 201919 /dev/ra2h 265575
The
kbytes used 7514 127494 15061 168621 3
21216 14029 184084 299980 10 597
101245 189453
kbytes free 6482 54234 166667 63046 6678 160512 167699 54934 63735 6671
181131 80483 49565
df command
is
shown below:
used
Mounted on
54% 70%
/
8%
73% 0% 12% 8% 77% 82% 0% 0% 56% 79%
/usr /var /usr/users /usr/rala /usr/sl /usr/s2 /usr/cpl /usr/fl /usr/ra2a /tmp /usr/f2 /dba
entry in the table always represents the device
©
file and file system from which you are issuing the command. Thus, the host is associated with the device "/dev/raOa." All devices which are attached to (mounted on) the host are associated with such an entry. Notice, on the far on." This entry means that the device right, the column titled "Mounted which is associated with device file /dev/raOa is mounted on the local directory "/" (item 1). Thus the drive associated with the device file /dev/ first
for the host
254
Chapter 7 Network Processing
raOa is the root how much space
the device can hold.
in kilobytes (i.e..
thousands of bytes) and usually refers to an unformatted ca-
disk, or root volume.
The second entry in the table indicates capacity. The value is measured
i.e.. its
pacity rather than an actual
(i.e..
dev/raOa) has 15, 551 of which 6,482 bytes are
kilobytes, or 15
By issuing home directory space
is
a
(i.e..
megabytes of
can determine on which device your
Then you can look
on the device
total storage space,
46%).
pwd command, you
resides.
available
free
usable) capacity. In this case, the slash disk (/
df
in the
table to see
The
that includes that directory.
how much
third
column
how much space, in kilobytes, is currently being used on the device. For example, the "/dev/ralf " device (item 2). on which my directory resides (/ usr/f 1). has a total capacity of 404, 127 kilobytes (i.e.. 404 Megabytes). and 2 99,980 kilobytes (300 Meg) are being used. The fourth column tells how many kilobytes are still available, and the fifth column gives that value as tells
a percentage of the total disk space on the device. Notice that these
do not add
up.
When
a device
is
formatted,
some of its space
generally reserved, and this accounts for the discrepancy.
reaches 100%. no users will be able to execute any
age of additional space until some
which
director} subtree
commonly low
mentioned
with the device
file in
mounting point
the first entry. This
7.7.3
du
is
is
is
The
last entry- identifies is
for the device. All directories be-
on the device associated
how you
will find "your" device
pwd command.
Finally, note (item 3) that there
but
10%)
commands requiring the us-
files are deleted.
in this entry physically reside
system associated with the "/tmp" ers,
say
the utilization
associated with the device on the system. This
referred to as the
the one
with the
is
(e.g.,
When
numbers
is
is
a physically separate device
director}'.
This space
is
on
this file
writable by
all
us-
being used,
du
for temporary, rather than permanent, storage.
Disk Usage similar to
df insomuch
as
it
tells
how much
storage
is
amount of Disk Usage" and will display the disk space takdirector}'. For example, if you want to know how much space
stands for "display
en up by a local
your
localhost> du
home
directory uses, type the following:
-
An example "
jhodges".
is
shown on
the following page for the director} of the user
7.7 File
System Management
200
localhost> du /usr/f 1/ jhodges/bin 171 /usr/f 1/jhodges/tmac /usr/f 1/ jhodges/mail-elsewhere 9 /docs /manual 120 /usr/f 1/ jhodges /TeX3
2
.
3
/usr/f 1/ jhodges/TeX3 0/usr/spool /usr/f l/jhodges/TeX3.0/usr /usr/f l/jhodges/TeX3.0 /usr/f 1/ jhodges/News /usr/fl/jhodges/AKCL /usr/f 1/jhodges/Mail /usr/fl/jhodges/lib /usr/fl/jhodges/F90 /usr/f 1/ jhodges/Texmacros /usr/f 1/ jhodges /Manual -in-LaTeX /usr/fl/jhodges/Archive-TEX /usr/fl/jhodges/Code /usr/fl/jhodges/MX-TESTS /usr/fl/jhodges/LATEX-TESTS /usr/fl/jhodges .
15040 15167 2
27551 728 1067 33
362 2649
31654 11965 3
34
91746
(T)
du will display an entry for each subdirectory below the directory where the command was issued. The display will use full paths. Thus, the "TeX3 0" directory is listed as "/usr/ f 1/ jhodges /TeX3 0" (at item 1). The du command displays directories In this example.
I
have spared you the entire
list,
.
.
from the inside
out. or deepest to shallowest.
Thus, the
last
term
in the display
of any particular directory will show the space used by that directory subtree.
For example, the
last line
of the
TeX3
.
directory (item 1) has as
its first ar-
"15167," which stands for 15,167 disk blocks, or 15+ megabytes. Each of the following entries shows the value for the home director)' (the others were omitted for the sake of brevity). The last entry is the space used by the directory originally called with the command (tilde expands to "/ usr/fl/jhodges"). which is a soaring 91+ megabytes. This is an excellent example of a disk hog. Not a nice term, and maybe not so deserved, since this I am responsible for the port of a number of utilities to the host SFSUVAX1 You can use the du command to keep tabs on your own directories and maintain them as necessary by deleting files or decreasing their size with the tar
gument
and
1.
1
the value
compress
utilities
presented in the next section.
This means that the block size on this device
is
1024 bytes, or
1
kilobyte.
2o6
Chapter 7 Network Processing
7.7.4 I
and Directory Archival
File
have noted
will
that
ftp
As ftp can
files
take a while to send large
or directories over to a remote machine. files,
and cannot send directories
except file-by-file, one must use other methods with
Tape ARchival (tar) of
files
tar key
[
a
is
method
onto magnetic medium.
from one machine
blocksize
[
]
where the keys
to another.
name
]
ftp to
at all,
achieve such ends.
originally designed for efficient archiving
It is
also
commonly used
The standard form
for
tar
to
move
directories
is
files
are described
defined better in the
However, often you
useful for sending a file or files.
is
be interested in sending large
below and the blocksize and name arguments are
tar man
page. Generally, the use of
tar
will
be as
fol-
lows:
localhost> tar cvf tarfile.tar filenames localhost> tar xvf tarfile.tar
The a single
first
file
command
will take the files (or directories)
"tarf ile tar." The .
'c'
and put them
all
onto
key means create an archive. The sec-
same order, ownership, and protection starting in the now-present location. The 'x' key means to extract the contents of the archive. A sample session is shown below, followed by a description of the keys that can be used. It should be notin the above examples is a verbose key which eched for emphasis that the
ond
line will then take
them out of the
tar file
and place them
in their
V
oes everything that tar does. The 'f ' key
be the name of the
tar
file to
tells
write the files
tar to.
that the next
argument
will
7.7 File
localhost> cd tmac; Is tmac Xref ieee ditroff tmac. Xref pic tmac.Xtra psroff tmac tmac.Xtras tmac tmac Franz tmac an tmac tmac. an. new tmac.XP tmac an6n tmac.Xalg tmac.an6t tmac .Xref .acm localhost> tar cvf tmac. tar * a ditroff 272 blocks a pic 272 blocks a psroff 18 blocks a tmac. Franz 3 8 blocks a tmac.X 16 blocks a tmac.XP 2 blocks a tmac.Xalg 2 blocks a tmac .Xref acm 5 blocks .
tmac ayday tmac. bib tmac cp tmac syb tmac imagen tmac os tmac tmac tmac scover .
.
.
.
.
.
.
.
.
.
System Management
257
tmac.sdisp tmac skeep tmac.srefs tmac.vcat tmac vgrind .
.
.
.
.
.
.
(T)
(2)
.
localhost>
Because the
V option was used
displayed on the monitor as ?
chived there file.
it is
is
an 'a in the
command
(at
item
1),
each
file is
file ar-
column (item 2), followed by the name of the name, and this number represents the size of the
first
A number follows the file When
in this
being archived. Notice that for every
command has completed, every file in the "tmac" di"tmac tar." The files are not removed, copies have been made and put into tmac tar. Also note that I used the wildcard
file, in
rectory
blocks. is
now
copied
the
in the file
.
.
(
*
)
to
denote
TOASTER,
all
to
items in the directory.
demonstrate
how
Now I will create a directory on the host
to extract, or "untar," files using
tar.
258
Chapter 7 Network Processing
localhost> ftp toaster other responses omitted ftp> mkdir tmac 257 MKD command successful. ftp> cd tmac 250 CWD command successful. ftp> pwd 257 /Users/hodges/tmac" is current directory. ftp> binary 2 00 Type set to I. ftp> put tmac. tar 200 PORT command successful. 150 Opening BINARY mode data connection for tmac tar 22 6 Transfer complete. local: tmac tar remote: tmac tar 237497 bytes sent in 1.6 seconds (1.4e+02 Kbytes/s) (
.
.
.
.
.
.
1
"
(2)
(3)
.
.
.
ftp> Is 200 PORT command successful. 150 Opening ASCII mode data connection for file list. tmac tar 226 Transfer complete. 12 bytes received in seconds (0.012 Kbytes/s) ftp> quit 221 Goodbye. .
localhost>
In this example.
item
1).
and cd
I
ftp
to the host, create a
to that directory.
new
Notice (item 2) that
director)' for I
my
files (at
used the binary option
more reliable file transfer, even when the file isn't binary. If the file is binary and you send it in ASCII mode, then you end up with a corrupted file and you may be able to extract some, but perhaps not all. of the files in the tarfile. Once in binary mode I issue the with ftp.
I
find that this option provides a
put command (item 3) to transfer the tarfile. Now. after ftp logs me out. I go r login in to toaster (procedure not shown, but notice the different C shell prompt) and
I
can type the following.
7.7 File
System Management
259
toaster [1] cd tmac; Is tmac tar toaster [2] tar xvf tmac. tar x ditroff, 139264 bytes, 272 tape blocks x pic, 139264 bytes, 272 tape blocks x psroff, 9184 bytes, 18 tape blocks x tmac. Franz, 19444 bytes, 3 8 tape blocks x tmac.X, 8113 bytes, 16 tape blocks x tmac.XP, 841 bytes, 2 tape blocks x tmac.Xalg, 639 bytes, 2 tape blocks x tmac Xref .acm, 2317 bytes, 5 tape blocks .
.
toaster
[3
When place the
logged on to
I
files.
Had
I
toaster.
created the tar
created the directory where
I
file
I
wanted
to
with embedded directories, they would
fly when I untar the file on toaster. Then I change direc"tmac" and untarred the file. Here are some of the keys used with tar
be created on the tories to
on a
Table 7.7:
common
basis:
tar Options command
c
«
Create a
t
List the
X
new
archive
names of
Extract the
reference
the files in the archive
all
named
items from the archive;
recursively extracts the directory and
£
FNAME
The key must be followed by want
V
tar to
a space
ceded by a function
file
-
.
tar
it
contains
name of each
in the
what
file
it is
is
archives or extracts, prefile
that
to that
(cd todir; tar xv
command,
it
doing with the
you can copy the contents of a user diof another. The syntax follows, but you man page for tar.
system (disk)
can find the example
In this
a directory, tar
and the name, FN/aME, of the archive you
letter that indicates
nifty thing about
rectory on one
cd fromdir; tar cvf
is
and subdirectories
read from or write to
This option makes tar display the
One
one of the items
if
all files
the first part of the
-
)
command
gets
you
to the directory
2b0
Chapter 7 Network Processing
The second
command
you want
to archive.
to create
and archive with the 'f option, but the
stands for the standard output.
Now
part of the
The
files selected are
the results are piped into another
cating to the
new
issues the tar
selected
file
those in the
"
command sequence
command '-'
is
which
.
"
directory.
that starts with lo-
directory and ends by extracting the archive from the stan-
dard input. Note that the verbose option
is
being used throughout so the user
can watch the progress of the transfer. 7.7.5
File
Every once
Compression you look
in a while,
at a tarfile
cide that you are simply using too conscientious. This files to a
much
and
it
a time for file compression,
is
more compact
version, and replaces the file
in a smaller file.
press "tmac tar"
and see what happens, but
its
original size so that
of
compress:
we can
tell if
compress name with
For example, we could
compression resulted .
looks enormous, or you de-
disk space and want to cut
first
we
down
to
be
compresses
"file.Z" if the try to
com-
should take a look
at
anything good results from the application
localhost> Is -1 tmac* -rw-r r 1 jhodges 471040Apr 18 14:19 tmac tar localhost> compress tmac. tar localhost> Is -1 tmac-* -rw-r— r— 1 jhodges 237497Apr 18 14:19 tmac. tar.
—
.
original copy of tmac tar is 470K bytes and the compressed 237K bytes. You can see that using compress amounts to quite a
The sion
is
.
ings in space. The next question that you might ask is "HOW FILES BACK!" Simple: type uncompress tmac tar .
prompt
ver-
sav-
DO GET MY I
at
the
C
shell
as follows:
localhost> uncompress tmac. tar localhost>
Notice that you do not need to provide the for
uncompress. The tar and compress
chives, since
and then use
it
is
" .
Z," as
it
is
the expected suffix
utilities are naturals for file ar-
far easier to transfer a single, smaller file
uncompress
and tar,
over the network
locally, to restore the archive.
7.8 Exercises
7.8 Exercises
and Sample Quiz Questions
261
and Sample Quiz Questions
7.8.1
Exercises
Exercise
1
Using your own directory as
a source
and
target, try the
rlogin.
telnet, ftp, rep, and rsh commands. Find an anonymous ftp site
Exercise 2
and bring something small back to your local
Issue the
host.
who, rwho. uptime, ruptime, and w commands. Note
their differences.
Exercise 3
Practice
file
system management by tarring and compressing a
file
and
a directory, and then reversing the process.
7.8.2
Problem
Sample Quiz Questions 1
List three different real
Problem 2
name
is
ways
to find out information about a user
Dr. Jerk Foobar, assuming Dr.
Explain what tar.
Problem
3
is
compress, and uncompress
why. What are the options •
Foobar
are used for and
to
create an archive
•
view the contents of an archive
•
extract an archive
What is the minimum set of commands, and their correct order, to anonymously log onto a host named sumex-aim Stanford edu, .
.
retrieve a file in binary format,
and exit?
Problem 4
Approximately how much space
Problem 5
What UNIX command do you use named cl3.1.2.tar?
Problem 6
whose
logged on.
is
saved using
compress.
to list the contents
of a
tar
archive
LOCALHOST. You have an account on the [email protected]. and have a ".rhosts" file. You want to retrieve the files in "/usr/lib/tex/ inputs" to a directory called "texinputs" at localhost. What command(s) do you issue from your terminal and fuzz? You
are logged onto the
host
Problem
7
Which of the following provides the most explicit information about a Which is the fastest way to find out if they are logged on? Which is the fastest to find out how long they have user and what they are doing?
been idle? •
who
•
rwho
•
users
•
rusers
•
w
•
finger
•
last
262
Chapter 7 Network Processing
What does
Problem 8
notion of
file
resource environment?
regard and
this
You
Problem 9
X
are
how can you
system management mean and
system management
file
why
Which
in
your day
to
apply the
day use of a shared
four utilities are particulary useful in
are they useful?
working with a Computer Science faculty member
to port
an
VAX. The tared and compressed file takes up 6 and the uncompressed directory uses 16+ Megabytes.
server to the
Megabytes,
Assuming your
Which
•
likely
faculty
member
has access to these filesystems,
filesystem(s) in the
want
two
listings
to try to bring the server
•
Why?
•
Which UNIX command produced
below would you
least
up on?
these listings?
A:
Filesystem node /dev/raom /dev/raoa
Total kbytes 15551
kbytes used
7423
3
Total kbytes 201919 265575
kbytes used
kbytes free 6482 6678
7514
used 54%
Mounted on /
0%
/usr/raoa
used
Mounted on
B:
Filesystem node /dev/ratn /dev/raon Problem 10
597
189453
You
are logged onto the
task,
when
the time
the screen goes
way
kbytes free 181131 49565
SFSUVAX1.
0%
79%
/tmp /nonl
doing some minor processing
between what you type and what gets echoed on up. After a
few minutes, reponse time returns
to
normal, only to become sluggish again, and again, over the entire period
you
•
What
•
How
type of problem you think it is you would identify what type of problem this is What one could do to avoid this from happening in the
•
7.9
are logged on. Please identify
future
Recommended Reading UNIX
References
Sobell.
Mark, [1989]. A Practical Guide to the UNIX System, 2nd Ed., Benjamin-Cummins. Redwood City. CA.
Advanced UNIX Rochkind. Marc
J.
Principles
[
1985].
Englewood
Advanced UNIX Programming,
Cliffs, NJ.
Prentice Hall.
PART II Programming Languages and ANSI C The intent of this part of the book is to introduce the student to the concepts associated with programming. You are not expected to become fluent in C, nor are you expected to become a good programmer by writing a few simple programs. However, you are expected to learn how to write a program, which includes proper code presentation, debugging, and function. You are also expected to learn the concepts in programming that are applicable to any programming language. No aspect of the C programming language is presented in very much detail. Generally, a topic is introduced that covers a broad range of programming situations. An example is discussed that illustrates how that topic fits into the programming environment and how it is used. Throughout the chapter the example will be modified to add to its functionality. Where alternative approaches are possible, the alternatives will be tabulated so that you can modify the example code to experiment with them. By the end of each chapter, you should have a few template examples that enable you to construct simple working programs that exhibit behavior commensurate with the topics leading up to and including those introduced in the chapter.
Introduction to
C Programming
8.1
Introduction and
Scope
Programming is an empirical skill of writing instructions to control a computer. Programming is both rewarding and fun. but you will never understand a programming language without writing and correcting (i.e.. debugging) computer programs. In this chapter, the C programming language will be introduced, along with methods for writing, compiling, executing, and debugging simple programs. In subsequent chapters, more complex notions will be introduced which will enable you to write programs that can perform more elaborate tasks. The following topics will be covered in this chapter: •
Programming languages
•
The C programming language
•
A
simple
•
C
language operators
C
program
At the conclusion of this chapter, you should be able to look at a C program how and why it is laid out. You should know how the file is named, how a compiler is invoked to translate it. and what the executable file and understand will be called.
You
should understand the differences between syntax and se-
mantics of a programming language, and you should
and semantics of C. Finally, you should that
know how
make use of simple expressions and
know some
of the syntax
to write simple
C
programs
variable assignments.
265
266
Chapter 8 Introduction to
8.2
Programming Languages
C Programming
A programming language is a set of rules for evaluating instructions to a A computer program is a set of instructions (like a recipe) the
computer.
computer follows
to
perform a specific
task.
An
instruction can be as simple
as a variable assignment or a call to a function. Expressions are combinations
of operators and their operands (data).
A
programming language
consists of
operations on data, and the rules for combining and evaluating the resulting expressions.
The
instructions
which comprise a computer program can be exI am conveying to you can
pressed in different languages just as the thoughts
(spoken) languages. The underlying concepts grammar and style for expressing concepts change. The essence of programming is to understand the underlying concepts that are be-
be expressed
in different natural
are the same, but the
ing expressed, and to recognize
how
they are captured in any particular pro-
gramming language. There are different kinds of programming languages. The computer hard-
ware manipulates
electrical signals,
and the instructions which
tell
the
comput-
machine language. Machine language is appropriately called a low-level language. It is both awkward and unnecessary to communicate in low-level languages. Most programmers com-
er
what
to
do with
electrical signals are called
municate with the computer with high-level languages such as LISP,
FORTRAN,
or
PASCAL,
C,
COBOL.
The computer cannot execute directly. They must first be
the instructions of these
programming
lan-
machine language. The translation process is called compilation. The translation is performed by another program, called a compiler, which reads the expressions from the guages
translated into low-level
high-level language, interprets them, and rewrites them in machine language. The executable expressions of a computer program are called code. A program in a high-level programming language has two code components: source code and object code. Both source code and object code reside in files. The source
code
file is
the editable version of the
The object code er programs,
8.2.1
is
file is
program and
is
the file that
is
compiled.
the translation produced by the compiler, and, for small-
often the executable version of the program.
Programming Language Syntax and Semantics
A
programming language evaluates expressions by first recognizing the symbols which comprise expressions. Once recognized, the symbols can be clustered together into meaningful units, which the computer can then execute. The symbols and their organization into expressions is called the language's syntax and grammar, whereas the interpretation and meaning of expressions is the language's semantics. Writing a successful computer program requires an understanding of the language's syntax and semantics.
The C Programming Language
8.3
Grammar and
Syntax
The compiler must read
a
program according
These rules are called grammar it
267
rules. If the
and consistent
to specific
compiler
is
rules.
able to read the data,
can then use these rules to construct, or parse, the data into recognizable ex-
pressions, interpret their meaning, and continue the translation.
To
parse ex-
pressions, the language expects certain symbols to have specific meanings. For
example, the language must be able
to
when
determine
the
end of an expres-
It must recognize the end of expression character. Likewise, each language has a specific character sequence which it recognizes as a comment,
sion occurs.
after
which
symbols
is
it
does not
try to parse
meaning. The interpretation and order of
which
a language's syntax. Programs
rules will not
compile and will produce
errors.
violate a language's syntax
Programs with syntax errors
cannot be executed because the compiler never succeeds in creating the object
code
file.
Structured Languages
A structured language has an extensive syntax and grammar. The more structured a
programming language,
sult, the errors (or
easier to find and
the
more grammar
rules
it
will have.
bugs) detected by the compiler during translation
debug
(resolve).
guages: however, their advantage
is
The opposite a freedom
is
from
As a remay be
true of unstructured lan-
strict rules for
how expres-
programming languages have their advantages, but this is an issue which is best addressed by a course in programming language design. C is considered a structured programming sions are presented. Both structured and unstructured
language, because
it
has a rich syntax, although
it is
less structured than, say.
PASCAL.
8.3
The C Programming Language C
is
a versatile language, and
ware platforms, meaning computers with the
ANSI C
little
that
its
structure enables portability
programs written
or no modification.
standard. Although
ANSI C
We is
in
will
C
between hard-
can be used on different
be using examples based on
not currently the most portable ver-
new. it may become the most porThe language will be introduced by looking at some simple examples which illustrate some of the language's syntax in proper pre-
sion of the language, because
it is
relatively
table version in the future.
sentation format (called style). Following the examples will be an introduction
C
to
operators,
program compiling, execution, and debugging.
C
program
•
Writing a
•
Basic syntax of a
•
Compiling a
•
Executing a
C
program
C program C program
268
Chapter 8 Introduction to C Programming
Before tive.
we
begin,
should be reiterated that C,
it
like
UNIX,
is
case sensi-
This means that lowercase and uppercase characters are unique and are
not interpreted as having the same meaning in any context. For example, an is different from "TOTAL" or "total." When you use you must be very careful to reuse it exactly the way it was
identifier such as "Total"
a variable name, originally used.
8.3.1
Writing
Programming to
how
it
style
concerns
how
a
program
presented visually, as opposed
is
executes. Style includes comments, format, and object
ventions, none of
guidelines
Ch8
C Programs
is to
which
affects
how
make programs
a
naming conprogram executes. The purpose of style
easier to read, so that
programs can be used
properly, fixed, updated, and rewritten in different languages by different people at different times. Consider the following properly written program,
"UNIX/Ch8 /First
.
c,"
which
is
nothing more than
comment
lines
and an
empty program:
Figure
8.1
Use
:
of input parameters in First.
/••••••A*************************************************/ First.
AUTHOR: DATE: /* FUNCTION /* APPROACH /* COMMENTS
J.B. Hodges August, 1993 NONE NA Demonstrates the use of banner comments for program documentation.
*/
/* /*
/* /
©
*/
/* FILE: /*
*/
*/ *
*/ */ */
******************************************************** /
int
main (void) {
return
(
)
This example program does nothing
mally necessary to
start
do with the program and comments are being made. have nothing
to
at all,
but
it
illustrates
programming. The numbered items will
what
in the right
is
mini-
margin
be used to identify lines where
Banner Comments Even an empty program should have some statement indicating what it is intended for. Although it is not a formal component of a computer program, a program's documentation plays a crucial role in how the program is present-
The C Programming Language
8.3
ed. In the
program
in Fig. 8.1. item (1) is called a
269
banner comment, and
is
comments. Ever}' programming language has a mechanism for making a comment, which is a line of text that is not parsed or evaluated in any way by the compiler. In C. comments are delimited by /* at the beginning and */ and the end. and can span more than one line. The banner comprised of a
series of global
comment shown This
is
has
comment opening and
partly for readability, as the reader
closing characters on each line.
need not search for the end of the
comment, and for security, since the programmer can delete a cidentally removing a comment's beginning or ending.
The banner
itself
line
without ac-
contains information which documents the program's de-
main function banners, etc. Each banner
sign and implementation. There are different kinds of banners:
or program banners, function banners, header
file
documents the aspects of the program specific to its associated procedural component. A list of information which is commonly placed in a main and other function that is
banner comments
provided
is
shown below.
in the banner, the easier
program was intended
to
do and how
it
it
In general, the
more information what a
will be later to understand
was done.
FILE: The most obvious part of the banner should be the name of the pro-
gram or in
file.
In the
example above,
which the source code
FILE LOCATION:
It is
resides.
easy to lose
the
program
"First
files,
and
tify the location in the banner, in case
.
is
it
named
is
after the file
c"
good idea to idena hard copy but not
often a
someone has
the soft copy.
VERSION sion
and DATE:
When many
program
iterations of a
number and modification
exist,
then ver-
date are important data to maintain in
the banner.
AUTHOR
(account ID): The person
should always get credit for
it.
who
designs and writes a program
The account ID
only important to
is
lo-
cate the files later.
CLASS and INSTRUCTOR: er,
the
CREDITS:
This
is
clearly academically motivated;
same mechanism could just
If
as easily
an algorithm or code segment
grammer,
it
is
only
common
is
work
howev-
for department, etc.
borrowed from another pro-
courtesy to give them credit for their
work.
PURPOSE: What
the
program
is
supposed
to do.
and should be a component
of eveiy banner comment. Purpose, or function,
program takes
METHODOLOGY how
it
is
as input
and what
it
is
generally what the
produces as output.
(algorithms/theories used):
What
the
accomplished need not be differentiated
program does and
in
simple programs.
2LJ\j Chapter 8 Introduction
to
C Programming
mechanisms invoked
In complicated programs, the
gram's function
FORMULAS: Where source code
up
may
not be at
appropriate,
it
is
much
the equations/logic being
if
good
to
achieve a pro-
obvious and should be documented.
all
easier to follow someone's implemented are documented
where the equations were found. Otherwise, even the author may forget what is being implemented and where it was found, making corrections, updates, and extensions diffront.
It is
also
to cite
ficult.
VARIABLES which
and
PARAMETERS: A
created, the scribed.
parameter
When
is
an input/output variable
program banner is main function's variables should be declared and de-
specific to a function.
is
When
a function banner
is
the primary
created, the function's parameters
should be identified as input, output, or input/output parameters dition to their declaration
and description.
ables/parameters in a banner than inline because all
the documentation
is in
IMPLEMENTATION NOTES: a
implementation
Even mentioning
USAGE:
may
reduces clutter and
the
methodology or algo-
not be explicit enough to re-
program was developed, so
may
it
one place.
rithms used to implement a function
member how
in ad-
better to describe vari-
It is
specific
comments on
the
be appropriate.
In function banners, a
sample function
call
could save a
lot
of time
Programmers often forget how a function was supposed to be used, so providing an example of its use on legal data, and naming the function(s) which call it, is often later appreciated. One way to provide later on.
a usage
comment
is to
provide the calling function (say, in square
brackets) followed by a sample function call with real arguments.
A
banner should be obvious and
clear.
A box
is
used
in the
example ban-
ners in this text to clearly distinguish documentation from source code, and the horizontal lines of asterisks provide clear boundaries between these sections,
making
the documentation and the associated source code easy to locate.
There are actually two types of comments, of which banner comments are used for documentation. There are also inline comments which you will see
embedded within the source code, often for debugging purposcomments can be problematic in C. because the language doesn't support comment nesting (i.e.. comments within comments). If you have com-
soon, which are es. Inline
ments inside the source code, and you decide to comment out (i.e. block out) a section of the source code, then you have to specifically edit out the comment characters within the block. sible,
much
By
placing as
many comments
of this type of problem can be avoided.
into banners as pos-
The C Programming Language
8.3
2#
1
Program Blocks Every
C program
is
comprised of a main function component and additional
function components
C
ponent of every
when
appropriate.
gram. In every other respect main structure
The main function
program and coordinates
which performs a
like
is
whose
task,
is
a required
com-
the procedural elements of the pro-
any other function.
definition
is
A
function
is
a
semantically comprised of
component blocks: 1 a header. (2) declarations, and (3) statements. The header block is used as an interface to other programs or functions through argument passing. The declarations block identifies the objects and processes which will be used. The statement block consists of the expressions which will be executed by the program. Although the simple program presented above will do nothing, it is a legal C program: it has a single main function which has a header, declaration, and statement block. three
)
(
case on a line by itself, is a declaration. what type of value a variable or function can take on. Every object or function in C can have a type. An object, such as a number or a character, always has a type. For example, an integer is the type
Item (2) in Fig.
A
declaration
int. and a
real
"int."
8.1.
in this
a definition of
is
number
is
the type
A
double.
function in
C
is
a procedural
which can be called with arguments and can return a value. In mathematics, square root is a function which takes a number and returns its square root. A C function works the same way. and the language
construct, like a program,
know what
needs to
kind of value will be returned. The reason
tions, like square root,
human its
can be embedded
use makes sense in the expression. In
may
before
C programming, the
"main" simply means
that
C
func-
value returned by
to help the
embedded in. and this programmer find errors. The
main
a kind of function that will re-
not be appropriate for the expression
can be checked during compilation
is
mathematics, the
and (hopefully) knows whether
interprets the result of the square root
a function
int
in expressions. In
that
is
it
is
value. turn an inteser "&*
Item er.
As mentioned,
functions.
"main" and the
is
It
is
the
says
done through what
identifies this as the
always required
program and
pear as
"main (void) " header identifies how a
(3). the line that
it is.
in a
is
called
C
program,
interfaces to other programs.
in the figure.
delimits the parameter
There
list.
ration,
nor a
is
The
void
in the
function's head-
parameter
list.
The
string
It is
organizes other components in
The
string,
"main." must ap-
and the open parenthesis,
it
set
of parentheses next to
a single item
A void in a parameter list is
parameters for the function.
main
function interacts with other
its
main
though not shown that way rameter
the
primary or organizational function of the program
and there can be space between
list.
is
void
in this particular pa-
simply a statement that there are no
a matter of style. Neither a
parameter
list is
al-
main
void
as a decla-
required by the language semantics.
However: both make sense because they declare
to the compiler,
and
to
anyone
272
Chapter 8 Introduction to C Programming
who reads the program something which Items (4) and
source code what the programmer's original intent was,
often lost in the documentation.
is
open and close curly braces
(6), the
( {
and
}
comprise the
)
statement block for the sample program. Although the only expression in this
segment
is
a return statement, this
is still
the use of statement blocks like this
You
a legal statement block.
when stubbing
will see
discussed in Chapter
is
9.
Each function can return a single value to the function or environment it was called, as specified by a return statement. Item (5) is a return statement, and specifies what value will be returned by the function main. Since no function called main, the value is returned to the environment which called the program, namely the UNIX command shell. This particular from which
return the
of
int is
statement returns a value of 0, which
(i.e.,
is
(and must be) consistent with
integer) type associated with the function's definition.
chosen because the information sent back
to the operating
associated with "success." In general,
teger)
is
turn
statement in
that
main
is itself
example programs,
all
a function.
I
ments and, where appropriate
embedded
A
what value
function,
will use
sequence of statements
A
of-
negative in-
(e.g., 0, 1,
will adhere to using a
re-
only to remind the programmer
to signify success in
main
return state-
to the interpretation of a return statement of an
will use a
I
if
we
is
program executed
ten used as an error function, to signify whether or not the successfully. Operating sytems will vary in
The value
system
1,
is
because
C
views
1
as a "true" logical value.
often grouped together functionally, called a
is called a compound statement. In C, the beginning and ending of a functional context, such The as compound statement sequences, are marked with curly braces { } open curly braces after the "main" function header denotes the beginning of
functional context.
sequence of executable statements
).
(
the context of the function, and
Because
this
program
is
balanced at the end of the context. marks the end of the function.
is
tional context, the closing brace
not intended to do anything,
we
In a func-
will not
show
it
being compiled or executing. In future examples, the compilation will be
shown
if it illustrates
a facet of
programming which
will be valuable to the
novice. In most cases sample input and output will be provided.
Simple Program Statements: Display Information
Now
that
you have seen
a
program
that
works, but does nothing,
statement which does something and see what
it
takes to
pose that you want to display a greeting to the user of
would add
a printf
C
make
this
function to our sample program as
it
let's
add a
work. Sup-
program, then you
shown
in Fig. 8.1.
8.3
Use
Figure 8.2:
of input
in
273
FirstOut.c
FirstOut.c
/* FILE: /* /* AUTHOR:
(Y)
*/ */
*/ J.B. Hodges */ August, 1993 */ Displays a string */ Uses stdio.h function library Demonstrates the use of the I/O function */ */ library and the printf function on a */ simple string.
/* DATE: /* PURPOSE: /*
parameters
The C Programming Language
LIBRARIES:
/* COMMENTS: /* /*
/*****•**************************************************/
©
#include int
main (void) printf "\nHello there future ANSI C wizard! \n\n" (
return
(
(4)
)
0)
}
This program. "FirstOut c," will print the line "Hello there future ANSI C wizard! " by itself and stop. Nevertheless, it introduces four .
new
concepts: (1) the inclusion of standard libraries, (2) the use of standard
functions, (3) the executable expression,
and
(4) the expression terminating
The notes labeled item (1) and (2) in the margin of the program inchanges to the banner which result from the change in program name
character.
dicate
and functionality.
To
use the printf function, which clearly was not written in this program
(or there
would be
a function definition for
it), it
is
output
I/O or 10) and resides in
(called
"stdio h"
must be made available
one of many functions which
program, printf
a
is
to this
associated with input and
library
stdio h
of functions called 1
header file and represents a standard library that is provided with the C programming language, but, like other libraries, it is not made available to the programmer's environment unless the programmer specifically requests it. The request is done .
or "standard input and output."
with the include statement. Item (3)
is
.
is
called a
a type of header statement, because
defines an interface between the current program and other programs. called an include statement, and the
clude
1.
A header file
is
so
statement
named because
it
is
called a
pound sign
(#) is required.
The
#
it
It is
in-
precompiler directive, because the inclusion of
includes the function headers for each function in the library.
2 #4
Chapter 8 Introduction to C Programming
this library
must be performed before the source code
in the file is
compiled.
Other precompiler directives will be introduced and discussed as their functionality merits. Although not shown that way, there can be space between the
pound sign and
the the string
ed to the program, to
Firs tOut Item (4)
is
.
"include." When
c for local use. One of these functions
list,
is
no exception: although
printf is
many arguments, depending on what required argument
any
set
The
it
is
is
identified
it is
results
items which are to be printed,
In the
used to display strings
it is
sample
told
re-
it
argument but may have
programmer
is
The The number
trying to dispay.
by the contents of the control
string.
A
seen as a single object.
argument, interprets
where and how the if
string dictates.
its
con-
Each of
the
any, must be identified in the control string
to print.
printf function call, printf is not instructed to display
items, only the string ever,
the
printf function takes the control string
and displays the
and told how
As
of numbers, characters, spaces, and special symbols delimited
by double quotes. As such,
tents,
is printf.
a string, called a control (or format) string.
is
of additional arguments is
add-
is
available
associated with a type and has a pa-
turns nothing to the program, printf takes at least one
string
made
the application of the printf function, called a function call.
previously mentioned, every function
rameter
the include statement
of the functions in the stdio library are
all
how
any
"Hello there future UNIX wizard!" How-
to print the string, with the special characters
(\n) in front
of the string and the two (\n)s in back of the string. These characters have special
meaning
first
one simply moves the
in the control string: to print a line
feed or carriage return. The
print position to the first character of the next line
before printing the string, and the last two print a blank line after the string.
Were we to print a variable using printf. the variable type and formatting commands for printing the variable would have to be included in the string, as well as the name of the variable as a separate argument. Before compiling and running this program,
let's
look
at
some of the other options
for controlling printf.
8.3
Table
8.1
The C Programming Language
2/0
printf formatting options
:
\n
=$
Print a line feed
\t
=>
Print a tab
%d
=>
The argument
is
%c
displayed in decimal notation
=>
The argument
is
displayed in character format
%s
=*
The argument
is
displayed in string format
%£
=>
The argument
is
displayed in fixed point format: [-]mmm.nnn
%e
=>
The argument
is
displayed in floating point format: [-]m.nnne [±]xx
%g
=>
Display
in
%f or %e
format, whichever
is
shorter
printf "There were %d rainy days in the U.S. last year\n" (
control string with one integer print argument
printf
At the end of the semicolon
many
(
;
other
Long
integer variable
statement in the example (item 4) there
is
a
). The semicolon is the expression terminating character in C (and programming languages). Every line inside the statement block
must begin or terminate lon.
foo)
,
lines
a functional context or be terminated with a semico-
can also be extended to the next
line
with a backslash character
(\).
Compiling a
8.3.2
Since the program the compiler
C Program
FirstOut
now and
.
c will actually do something,
let's
introduce
slowly talk more about the compiler as more complicat-
ed examples are introduced.
The
C
compiler
we
will use
is
the
Gnu C compiler and
is
invoked (on
this
command gcc. We will use this compiler because it ANSI C. Many other C compilers exist, and the program-
particular host) with the is
compatible with
mer should find out about the characteristics of their compiler and related utilities. The compiler takes a file as a mandatory argument, as shown below. The general template for compiling a source file is shown below
gcc
[options]
sourcef ile
.
.
where "sourcef ile"
1.
Traditionally, readable
program code
is
is
the file
name
containing the program you have
called source or source code.
276
Chapter 8 Introduction to C Programming
The name of the
written and wish to compile.
template to compile
this
FirstOut
.
file
must end with
"
c." Lets use
.
c.
localhost> gcc FirstOut. localhost>
where no options are used, and the "sourcefile" command argument used file "FirstOut c." A few other compiler options are listed below.
is
the
.
Table 8.2:
-bsd
gcc Compiler options BSD semantics
=>
Enforce
-g
=>
Produce additional symbol table information for the debugger
-I DIR
=>
Specify the directory
-o OUTF
=>
Name
-0
=>
Uses the object code optimizer
-P
=>
Returns a count of
-v
=>
Compile verbosely
-Y ENV
=>
Compiles
strict
DIR
to search for # include files
the output object file
for
If there are
outf
how many
times each routine
environment ENV
no compilation
(e.g..
BSD,
errors, the next
is
called
POSIX,
SYSTEM_FIVE)
response will get a shell prompt
without any other notification. Unless otherwise stated, the compiled version of the code (the object code), which, in this case, called "a. out." After the
This
is
program
is
also the executable
file, is
compiled, you are ready to execute
is
it.
done by entering "a out," along with any arguments required by the .
program, as follows:
localhost> gcc FirstOut. c localhost> gcc ./a. out
Hello there future ANSI C wizard!
It is
possible that you could
fail to
get the
program
to execute
you have successfully compiled the program. This could happen
even though
if
your
UNIX
/a out" in it. In this case, simply type out" to tell UNIX what directory the executable is in. If you want to change the name of the executable file to something other than a ou t.
execution path does not include
.
"
'
'
.
.
instead of "a.
.
you can use the "-o" compiler option as follows:
localhost> gcc -o foe FirstOut. c
This will
name
the object
code
file
for
"FirstOut
.
c" to be "foe." Be
The C Programming Language
8.3
careful to not confuse the small 'o' with the large '0,'
option to optimize the compilation.
Do (man gcc)
which
for
is
277
the compiler
more information on
compiler options and their applicability.
Errors Detected During Compilation
8.3.3 If
your program doesn't compile, then
is
one where you have
it
has syntax errors in
it.
A syntax error
either typed something incorrectly or have added or
gotten to add something which
is
required by the
C
for-
language syntax. Often
these errors will be displayed on the screen. Errors will be identified by line
number. The
line
number
curs, since the error
is
in the error
flagged
when
may
not be the line where the error oc-
the error
is first
identified.
But
it
should
FirstOut.c program is called Fir-
be close. As an example, consider a slight modification to
which generates a syntax
stOut Figure 8.3:
An
.
c but
is
error,
in a file called
shown below. This
"FirstBug
intentional syntax error injected into
.
c":
FirstOut.c.
localhost> cat FirstBug. /A*******************************************************/ /* FILE: /*
FirstOut.c
/* AUTHOR: /* DATE:
J.B.
*/ */
Hodges August, 1993 /* PURPOSE: Displays a string /* LIBRARIES: Uses stdio.h function library /* COMMENTS: Demonstrates the use of the I/O function /* library and the printf function on a /* simple string. /**********************************************
*/ */ */ */ */ */ */
^
— ERROR
#include void main (void) {
printf
("
\nHello there future UNIX wizard! \n\n"
)
return; }
where the terminating portion of the banner comment lation
produces the following
localhost> gcc FirstBug. Id: Undefined symbols:
main
list
of syntax errors.
(*/) is
removed. Compi-
2# O
Chapter 8 Introduction to C Programming
where, in this case,
many
we
are lucky. In
many
cases, a simple 'typo' will result in
downstream of the error. This effect is called error propagation. There are two types of syntax errors: ( 1 ) warnings and (2) fatal errors. The difference between them is that warnings are recoverable but may point to a problem with the program logic. A fatal error will produce non-executable code. Errors have a tendency to propagate. One error early in the program may cause other errors further along. For example, if you have a missing left parenthesis, you might generate two errors - one for the missing left parenthesis and one when the compiler finds the right parenthesis. Because of error propagation, it is a good idea to correct the first error that occurs, compile the program again, and see if other errors disappear. errors
You can want
to
fix
bugs (called debugging) by editing the source
keep different versions of the source code when you
to avoid destroying a partially will
be addressed in Chapter
1
Many programs
start
You may
debugging,
working program. Techniques for debugging .
Using printf to Request Input and Reading
8.3.4
file.
require the user to type data which
It
requested by the pro-
is
gram. Anytime a program interacts directly with a user, regardless of the inter-
mode, the program
action
is
called an interactive
program. Programs which
are called and perform their function without interaction are called
batch pro-
grams. Inputting large amounts of data is tedious and programmers generally avoid interactive input of large amounts of data; however, for small amounts of data type
it is
some
all right.
We
will
now modify Firs tout
data to be read and then displayed.
.
c to signal the user to
The C Programming Language
8.3
Use
Figure 8.4:
of input/output functions in
279
First io.c
*****•> /* FILE: /*
FirstlO.c
*/ */
/* AUTHOR: /* DATE:
J.B. Hodges August, 1993
*/
(T)
*/
Reads user's initials and number of dogs */ /* */ and then prints them on one line. /* LIBRARIES: */ Uses the stdio.h function library /* COMMENTS: */ Demonstrates variable declaration. /* */ Demonstrates use of scanf on char and /* */ int variable types. Demonstrates use of /* */ printf to output variable values. /****************************•***************************/ PURPOSE:
/*
tinclude int
main (void) {
char first, middle, int ndogs;
last;
/* /*
Initials of user's name */ */ Number of dogs
(3
printf (" \nPlease type your initials, separated by no spaces: scanf "%c%c%c" &first, &middle, &last) printf (" \nPlease type how many dogs you own: "); scanf ("%d", &ndogs) printf (" \nHello, %c %c %c, you own %d dogs, right?\n\n", \ first, middle, last, ndogs);
" )
(4
;
(
,
return
(0)
}
I
mention once more
will
function, so
I
that
I
have changed the name of the
file
and
have modified the banner statement accordingly. Henceforth
its
this
kind of change will be assumed to be important but will not be mentioned. This
program
is
essentially identical to
FirstOut
.
c
in that
no new
libraries are
needed and we are only using standard (sometimes called canned functions). However, we are now using variables, since this program will ask the user to type
some data and
til it is
used.
that data has to
We are
use the scanf function. Finally, ables. Item (3)
is
(i.e.,
stored in
memory) un-
we
we
will
will use the printf option to display vari-
a variable declaration. In C, variable declarations define both
the type of variable that
it
will be
the type precedes the identifier, so identifiers
be remembered
also going to read that data from the keyboard, so
and
its
char
identifier.
is
As
previously mentioned,
the type associated with the variable
first, middle, and last, and int
is
the type associated with
2o0
Chapter 8 Introduction to C Programming
ndogs. Variable names should always be chosen to be The variable names in this example are at least pos-
the variable identifier
functionally appropriate.
remember, but they are slightly cryptic. When variables are cryptic it possible to forget, later, what they were for. This makes reading code some
sible to is
time in the future since the tice that
difficult.
This
is
when an
comment may
inline
be of value,
programmer can write what the variable means and what it does. Nothe comments line up left and right. This simply makes the comment
is not required syntactically. In general, this type of comment may save some space, but confuses the distinction between documentation and source code. It would be better, and cleaner, to have the variables de-
easier to read, but
scribed in the function banner. For the purposes of saving space, only, variables in our examples will be It
documented
inline.
should be noted that variable names have thus far been simple character
combinations.
meaningful
legal to use an underscore (_) to separate characters into
It is
and
units,
numbers
to use
name with
gal to begin a variable
names; however,
in variable
a number, nor
is it
Also notice
also
It is
Item (4)
though
printf
tions, printf
tion or
all
is all
is
on the same
line,
sepa-
right as long as they are all the
right to type
illustrates the
use of the keyboard reading function, scant. Al-
and scant are both I/O functions, they are different kinds of func-
always displays
program which
calls
its
result
it.
As
scant always assigns a value and
not le-
same type (i.e., them on separate lines. You can declare diftyped variables on the same line, but not separated by commas.
char). ferent
that there are three variables declared
by commas. This
it is
any other spe-
name.
cial characters (e.g., $, >, gcc -o fsioc FirstStringlO.c localhost> fsioc Please type your name: Jack Please type how many dogs you own: 2 Hello, Jack, you own 2 dogs, right?
When
the
%s format
used
is
input for the program, as
is
encountered. Thus
"Jack"
is
reasonable
shown above, but "Jack Hodges" would not
be.
Input from a Separate Function
8.3.6
One
scant, characters will be placed into the
in
string variable until the first space
final
use of the scant function
the overall function of the
is
program
fined") into smaller (at least,
more
in a separate function. In is
most programs,
broken down ("decomposed," or
specific) subtasks.
fined into yet smaller subtasks. This approach
may
"re-
and these are further
re-
continue until a subtask
nothing more than a single expression. At that point the program
is
is
constructed
by defining the required variable types and identifiers, and setting up interfaces between each of the subtasks and the top-level function, main. This process is called iterative refinement.
When programming
subtasks are implemented
using constructs which control the flow of execution, this process ciated with
what
is
called
of modular programming control behavior.
As
single exit point.
AC
is
function
is
a control stucture. or module:
AC
Although
wise to use only one. so a
is
it
is
it
defines and
function has a single entry point
than one exit point,
is
feature
such, the structure should have a single entry point and a
in the function header.
The use of functions
also asso-
the use control structures that have predictable
orders a sequential block of statements.
it
is
modular programming. The predominant
possible for a
C
C
function to have
function
is
more
also a module.
as one type of control structure in the refinement process
called procedural abstraction.
To
introduce the notion of modular programming, using functions,
we
will
write a small program which does exactly the same thing as the FirstIO c program: however, the display and input segments of the code will be consid.
ered separate functional subtasks and will be performed by their tions.
One
function
(PrintVals)
will
own
func-
be an output function, which will do
284
Chapter 8 Introduction to C Programming
nothing more than display the final results, and one function
be the input function, which obtains data
in the first place.
(GetArgs)
will
The main function
will coordinate the activities of the functions.
Modular programming for simple
Figure 8.6:
I/O in
FirstModio.c
/A*******************************************************/ /* FILE: /*
FirstModlO.c
AUTHOR:
/*
J.B. Hodges August, 1993 Reads user's initials and number of dogs and then prints them on one line. Uses stdio.h function library. Input is separate from MAIN. Output is separate from MAIN. GetArgs: reads the input data PrintVals: displays the output Demonstrates modular design for I/O with functions.
/* DATE: /* PURPOSE: /* /*
APPROACH:
/* /* /*
FUNCTIONS:
/*
COMMENTS:
/*
*/
*/
/*
*/ */
*/ */ */ */ */ */ */ */ */
#include
void PrintVals (char argl, char arg2 char arg3 int arg4) void GetArgs (char *firstp, char *middlep, char *lastp, int *ndogsp) ,
,
©
int
main (void) {
char fi, mi, int nd;
la;
(2)
GetArgs (&fi, &mi &la, &nd) PrintVals fi mi, la, nd)
(3)
,
(
return
(0
,
;
)
}
This program
that
is
proach
from other programs which have been
FirstlO.
c.
The
difference
(i.e.
requesting
and reading This
a large departure
each of the component subtasks of the original program
presented so is
is
far,
but performs the
input,
same
task as
and printing the values) are placed into separate functions.
an example of procedural abstraction. Although the function-based apinitially
involves an increase in program complexity, the overall sav-
ings tar exceeds the pain. Generally, procedural abstraction three ways:
(
1
1
is
advantageous
in
readability. (2) testability, and (3) modularity.
The main function has two new
declaration components, called function
The C Programming Language
8.3
prototypes
(at
item
which declare the function
1).
their order. This allows the
the file and allows the
like
type, parameter types
and
placement of the function definitions anywhere
programmer
A function's prototype
program.
2oO
is
a lot of flexibility
simply a copy of
when implementing
its
in
the
header, but terminated
an expression.
The
variable declarations
shown
at
item (2) correspond in a one-to-one
fashion with the paramter declarations in the function definition and function call (item 3).
Modular programming
Figure 8.6:
for
simple
I/O in
FirstModio.c
(cont.)
*/ PrintVals: Prints output variables */ INPUT: first, middle, last, ndogs /* OUTPUT: */ None /* RETURN: */ None /* USAGE: */ [main] PrintVals fi mi, la, nd) /••••A***************************************************/
/* /*
(
void PrintVals (char argl
,
char arg2
,
;
,
char arg3
,
int arg4)
{
printf
(
"
\nHello, %c %c %c, you own %d dogs, argl, arg2 arg3 arg4 ,
,
right?\n\n"
,
\
)
return; }
/************************************•*******************/ /*
*/ Reads input variables */ None */ first, middle, last, ndogs */ None */ [main] GetArgs(&fi, &mi &la, &nd) /•************************************************•******/
GetArgs
/*
INPUT: /* OUTPUT: /* RETURN: /* USAGE:
,
void GetArgs (char *firstp, char *middlep, char *lastp, int *ndogsp)
(5
{
printf (" \nPlease type your initials, separated by no spaces: scanf "%c%c%c" firstp, middlep, lastp) printf (" \nPlease type how many dogs you own: "); scanf ("%d", ndogsp) (
,
;
");
(6
return; }
Clearly, each function
and the code
The code
is
is
shown above has fewer
lines of executable code,
specifically associated with the task of the particular function.
easier to read, because
you need not search around for the code
2o6
Chapter 8 Introduction to C Programming
segment that performs the subtask you are looking for. When you design programs using functions, you can initially write the statement block in main that calls the functions, the header and a blank function statement block (called a stub) for each function, and the associated function prototypes. This enables you to compile the program, and to develop the internal workings of each function at
own
your
pace.
Debugging and testing is easier, because you can isolate from the rest of the program. Finally, changes to the
the effects of the function
function need not require changes to the entire program, nor do the other func-
need
tions really
to
know how
a subtask
is
solved as long as the values that are
communicated between functions work together In general,
effectively.
you should work toward developing programs which make use
of modular programming methods and procedural abstraction. The
first
thing
you should notice about this sample program is that each function has its own banner comment. The banners associated with a function are different that
than those for a program, usually shorter. Unless a function
is
undergoing a
amount of revision, it will generally just require a banner that tells what the function name is, what it does, what its input and output parameters are, and what it returns. Sometimes, a sample call is useful in the function banner. In our examples, the function from which this function is called is included in the sample call, in brackets. This can be good for small programs; however, if a function is later used in a library, or if the number of functions in which a large
function
used
is
which can
is large, it
might be misleading
really only be resolved
to the reader.
This
is
an issue
by the programmer.
you look closely at the three function definitions you will see that they follow the same general form: ) the function is declared, (2) the parameter If
all
( 1
list is
defined, (3) the local variables are declared, and (4) the statements are
elaborated. Because
main has
thus far had no parameters, the parameter
lists
GetArgs and PrintVals appear much more elaborate. On the other hand, GetArgs and PrintVals are our first examples of functions which is the return no values. Such a function is declared as void to signify that of
it
programmer's intention statement for
is
to return
no values. The associated function return
simply terminated instead of providing
it
with a value. In item
(4).
we PrintVals. A param-
example, instead of having 4 variable defined within the body of main,
now have 4 eter
list
variables declared in the parameter
list
of
simply declares the items which will be shared between the calling
function and the function being called, and sets up an interface for doing so.
The only requirement maintained.
When
to the interface
the function
is
that the
PrintVals
of the variables used as arguments
in the
We
PrintVals
function parameters.
function from
main. PrintVals does
is
parameter types and order be called from
main,
the values
function call are copied into the
say that the values are passed into the not change the values of these vari-
ables in any way. so they are called output
parameters and
PrintVals
is
2u I
The C Programming Language
8.3
called an output function.
GetArgs (item 5) is somewhat GetArgs starts with no variable
different than for
variable values from the keyboard, these are called input
parameters and
The parameter
list
for
PrintVals. Because
GetArgs
is
values and reads
called an input function. In this case, the variable values are
completely different than those
in
main, because they do not have values
until
GetArgs. Moreover, since these variables will actually be the rescanf function, which returns the location of a variable in memory
they leave sult
of a
GetArgs
rather than the value itself, the parameters in the to
locations of those variables in
the
GetArgs must address
is
be used
in
is
the parameters of
GetArgs,
as objects, a simple
not adequate. Thus the parameters declared in the
commonly
GetArgs
header
referred to as pointers.
A
declared by placing an asterisk in front of the paramter name, after
the type declaration.
More on
this in
as a pointer, then the associated
Chapter
argument
variable with an address-of operator, & call to
memory. Since
expressions within
are 'objects that represent addesses,'
pointer
header must refer
GetArgs
(at
9.
in the .
When
a parameter
is
declared
function call must refer to the
This
is
how
the arguments in the
item 3) are given.
Item (6) shows that the variables associated with the values read in
GetArgs must be declared in main, because the values are returned by GetArgs to main before they are sent into PrintVals. This is called variable reference or call
by reference. Anytime
a function references a value,
it
must declare an object for the value. Notice that the variable names are not the same in main that the parameter names in GetArgs. This is quite all right, because the consistency check
is
based on the type declaration and order of
variables in the function call with parameters in the parameter the
arguments sent
to the function
when
matter what they are named. The program
it is
"Firs tModIO c"
run below:
localhost> gcc -o fmioc FirstModlO.c localhost> fmioc Please type your initials, separated by no spaces: JBH Please type how many dogs you own: 2 Hello, JBH, you own 2 dogs, right?
1.
This
is
list.
As long
called are consistent,
true for simple variables, or scalars. but not so for array variables.
.
is
it
as
doesn't
compiled and
2uO
Chapter 8 Introduction to C Programming
8.4
C Types,
Operators, and Expressions
In the
C programming
language, a program
is
comprised of processes and ob-
A process is comprised of the three component blocks mentioned previ-
jects.
ously (header, declaration, and statement blocks). tity, in
that
it
A
performs a task.
A
process
is
dynamic en-
a
process performs a task by executing state-
A program, a function, and examples of processes and each has its own identifier. An object is a data item, and also has a label or identifier. A constant and a variable are examples of programming objects. ments, which involve the manipulation of objects. an operator are
all
The program examples
that
have been presented so
far
make use of
dard library functions. Although the functions that are provided
in
C
stan-
libraries
are very useful, they are provided as support and cannot solve problems. In or-
der to write meaningful programs, programmers must learn their
own
the
FirstIO
program
FirstExpr first is
own
variables, write their
tions. In order to introduce the
.
c
.
c
to
expressions, and develop their
concepts in
this section,
we
become "FirstExpr c" .
illustrates a
how
few things
that
to declare
own
will further
(see Fig. 8.7).
were mentioned before. The
the declaration of a global (program- wide) constant (item 1)
A
func-
modify
named
constant
is
an object whose value does not change during the course
of execution, and
is
declared using the
"PI."
the # stant,
include
syntax. There
PI can be used
in
is
#def ine
no semicolon
syntax, which
is
after the definition.
similar to
As
expressions and as a function argument, but
it
a con-
cannot
be on the lefthand side of an assignment statement.
The second item of
interest is the variable
can use an underscore, as done
at
item
(2), to
naming convention
make an
in C.
You
identifier easier to read
more descriptive, but care should be taken not to confuse cases, not to use numbers as the first character, and not to use other special characters (such as or
$, &, *,
\,
#).
For example:
int state_tax;
is
a legal variable identifier, whereas:
is
not a legal variable identifier.
int state*tax;
8.4
C Types, Operators, and Expressions
Simple variable assignment expressions
Figure 8.7:
in
289
FirstExpr c .
/************•*******************************************/ /* FILE: /*
*/
AUTHOR
/*
*/
FirstExpr. J.B. Hodges August, 1993 Calculates the volume of a cylinder and
/* DATE: /* FUNCTION:
*/ */ */
*/ displays cylinder characteristics. */ /* LIBRARIES Uses the stdio.h function library /* COMMENTS */ Demonstrates variable declaration. */ /* Demonstrates constant declaration. /* */ Demonstrates variable assignment. /* */ Demonstrates field format specifier. /**********•*********************************************/ /*
#include ttdefine PI 3.14159
©
int
main (void) {
char obj_type; double radius, height, cyl_area, obj_volume;
printf ("\n**** THIS PROGRAM CALCULATES THE VOLUME OF A CYLINDER" \ ****\n\n" printf (" \nPlease input the following information for the cylinder:" "
)
\
"\n\n")
printf (" \tradius scanf("%lf", &radius); printf (" \n\theight scanf("%lf", Sheight); printf (" \nYou have entered the following: \n\n" printf ("CYLINDER RADIUS: %6 3 f\nCYLINDER HEIGHT height) :
"
)
:
"
)
)
.
cyl_area obj_volume
= =
:
%6.3f\'n\n", radius,
\
radius * radius; height * cyl_area; PI
*
(5
printf ("The volume of the cylinder is: %6.3f\n", obj_volume) return
(6
(0)
we have placed four floating point identifiers height, cyl_area, and obj_volume) on separate lines Also note
that
(i.e.,
radius,
in the
program
290
Chapter 8 Introduction to C Programming
same declaration. This was done comments could have been placed to the right of these
(item 3), but they share the
for readability. In-
line
declarations. Item
(4) illustrates the
Item (5)
ment
is
use of a formatting directive, first
t for printing tabs.
use of an assignment statement.
used to define the value of a variable, and
equal sign symbol ( = ).
some
our
illustrates
\
expression,
ting the value
it
When you
means
on the
assign-
always associated with the
see a variable name, an equals sign, and then
that the variable
right. It
is
An
on the
left
of the assignment
get-
is
doesn't matter whether the item on the right
is
a
number or a complex expression. Whatever the expression evaluates to will become the new value of the variable on the left. Only variables can be assigned values; however, they can be reassigned new values over and over again. It should be noted that an assignment statement is not the same as an equation. In mathematics,
on the
is
left
left
hand side
what
hand is
is
on the
identified
tional relationship, right to
the assignment
symbol
as
left.
PI
*
radius
*
to
You
you can
The assignment statement
l_area
right side of an equality
is
identified with
what
programming languages; however, what is on the with what is on the right hand side. It is a one-direc-
side. In
at
cannot have an expression on the
in a
left
of
mathematical equivalence.
item (5) defines the value of the variable
cy-
be the result of the expression:
radius;
This expression uses three
C
objects in a mathematical expression. All
three objects are user defined: one constant. PI, for the approximation to pi.
and two uses of the floating point variable, radius. The asterisk
(*) is a
sym-
bol for the multiplication operator, so this expression multiplies the values as-
radius, and radius. The result is made the cyl_area. On the next line (item 6). this variable
sociated with the constant PI.
new is
value of the variable
used
in
an expression (again, a multiplication) to calculate the volume of the
cylinder. Both mathematical expressions could easily have been collapsed into a single line. Also,
could have used the
by
obj_volume
=
had we included the standard math function
pow
library,
function instead of explicitly multiplying
we
radius
itself.
height
*
PI
*
pow(radius, 2);
Table 8.3 presents the mathematical operators available evaluation precedence. pressions
in
You can experiment
FirstExpr
.
c
.
in
C
and
their
with them by modifying the ex-
C Types, Operators, and Expressions
8.4
Table 8.3:
Mathematical operators
in
C
Operator
Precedence least
-
Function =>
addition and subtraction
*
=>
multiplication
/
=>
real division if either
+
division
, %
greatest
291
=>
if
operand
real,
is
integer
both operands are integers
integer remainder
Expressions are evaluated in a particular operator order, or precedence.
With respect
to
mathematical operators, division operators are applied
first,
then multiplication, and finally addition and subtraction. In order to modify
programmer must simply enclose the expressions in parentheThe use of parentheses overrides normal precedence. In addition to the standard (canned) mathematical operators, there is a standard math library
this order, the
ses.
(math.h) with the following functions in
Functions
Table 8.4:
in
the math.h function library
Function ceil
it:
Description Returns smallest integer less than x
(x)
cos
(x)
Returns the cosine of x (x
exp
(x)
Returns e to the power x. where e = 2.718
in radians) ..
fabs (x)
Returns the absolute value of x. where x can be real
floor
Returns largest integer not greater than x
(x)
log(x)
Returns the natural logarithm of x
loglO(x)
Returns the base 10 logarithm of x
pow(x, y)
Returns x raised to the power
sin(x)
Returns the sin of x (x
sqrt (x)
Returns the square root of x
tan(x)
Returns the tangent of x (x
Item (5)
in
FirstExpr
the optional field specifier.
.
c
The
in radians)
in radians)
aformof printf which makes use of specifier defines how a variable's value
illustrates
field
y
292
Chapter 8 Introduction to C Programming
will
be printed. In the example, the variable
a floating point variable with the specifier,
where
X
obj_volume
%f format
"6.3" precedes the type specifier. refers to the total
number of spaces
cluding the decimal point, and
decimal point. In
this
Y
is
being printed as
specifier. In addition, the field
The general format
is
'X.Yf,
allocated to print the value, in-
refers to the precision allocated
example, then, six spaces are allocated for
beyond the the number
and decimal point, and three spaces are allocated for precision. Below program compilation and execution. Note the effect of the printfs on the
is
the
print-
ing of floating point variables.
localhost> gcc -o fexpr FirstExpr.c localhost> fexpr **** THIS PROGRAM CALCULATES THE VOLUME OF A CYLINDER **** Please input the following information for the cylinder: radius 3 height 4 You have entered the following: CYLINDER RADIUS: 3.0 00 CYLINDER HEIGHT: 4.000 The volume of the cylinder is: 113.097 :
:
Included below specifiers
is
on different
a brief table of examples which illustrate different field variables,
and what the
on evaluation of the following expressions:
#define PI 3.14159 #define jack "academic" int
x;
double y; x
=
y
=
210; PI + 3;
print f
prints as a result,
based
Table 8.5: 1
293
C Types, Operators, and Expressions
8.4
pr ntf field specifier for fixed and floating point doubles
printf "The sum is @%f@\n", y
=>
The sum is @6.141590@
2
printf "The sum is @%5.0f@\n"
y)
=>
The sum is
@
6@
3
printf "The sum is @%5.2f@\n"
y)
=>
The sum is
@
6.14@
4
printf "The sum is @%5.4f@\n"
y)
=>
The sum is @6.1416@
5
printf "The sum is @%5.5f@\n"
y)
=>
The sum is ©6.14159(3
6
printf "The sum is @%6.4f@\n"
y)
=>
The sum is @6.1416@
7
printf "The sum is @%5.2e@\n"
y)
=>
The sum is @6.14e+00@
8
printf "The sum is @%6.4e@\n"
y)
=>
The sum is @6.1416e+00@
9
printf "The sum is @%e@\n", y
=>
The sum is @6.141590e+00@
10
printf "The sum is @%g@\n", y
=>
The sum is @6.14159@
The
field specifier controls
3, the field specifier is
printed.
played
The
"5
number
first
in, in this
case
5.
.
how
2f " and
specifies
the variable will be displayed. In
controls
how
how many columns
The second number
example
the value of variable "y" the
number
will
is
be dis-
how many significant this case 2. As mentioned
specifies
figures will be displayed after the decimal point, in
what type of format to display the number in, numbers which precede it will be interpreted. The "f specifies display of the number in floating point notation. When the field variable is too small for a number (as in example 1 ), C ignores the field variable and prints whatever number of spaces is required for the entire number. C will give as many significant figures as requested. In example 2. the number is truncated as a result. When a number is truncated, C will round it off to the next significant place, as shown in examples 2, 3, and 4. If the overall number of spaces is insufficient for the number of significant places requested, C will add (pad) the remaining spaces, as in 4 and 5. If the number of spaces requested is
before, the character specifies
and defines
how
the
large, then the variable value
is
right justified
and may
result in leading
blank
Examples 7 and 8 show the same printf field specification as for examples 3 and 6 except that the exponential format (%e) is being used instead of the floating point format (%f ). As before, C will provide enough space to print the number of significant figures requested. Example 9 is a comparison to example Finally, example 10 uses the %g format, which chooses between the %f and %e formats and drops nonsignificant zeros.
characters, as in
examples 2 and
3.
1
.
The same tables can be generated for integers (i.e.. %d) and %s) using the same example variables (and values) above.
strings
(i.e.,
294
Chapter 8 Introduction to C Programming
Table 8.6:
printf field specifier for integers
printf
'x
is @%d@\n",
2
printf
'x
is is @%ld@\n"
,
3
printf
'x
is is @%4d@\n"
,
4
printf
'Jack is @%s@\n"
5
printf
'Jack is @%5s@\n"
,
6
printf
'Jack is @%9s@\n"
,
1
As with
=>
x is @210@
x)
=>
x is @210@
x)
=>
x is
=>
Jack is @academic@
jack)
=>
Jack is @academic@
jack)
=>
Jack is
x)
jack)
real variables, integer
then
number or string.
C
and
@
210@
@
academic®
string variables are printed right justi-
smaller than the integer, or string.
fied. If the field specifier is
entire
and strings
If the field specifier is larger
will right justify the
number
C
will print the
number or
than the
or string with leading blanks.
Type Conversion
8.5 Variable
In C. the variable
on the lefthand side of an assignment must be compatible
with the resulting type of the expression on the will be flagged
(i.e.,
noted) by the compiler
maintained. However, in patible,
when
and no error
will
many
if
some
right. In
cases, an error
assignment compatibility
This
=
not
is
cases the variable types are different but com-
be flagged. The programmer should be aware that
unlike variable types are used together in an assignment statement.
will convert the value of the expression to the type of the variable
y
string.
is
on the
C
left.
called implicit type conversion, or type coercion, as illustrated below.
PI + 3;
In the formatting
while
examples above,
PI was declared
When y
is
a
declared as a real variable (double),
a real constant. In this example. 3
new
being assigned a
hand side of the expression slightly
y was
value,
C
is
an integer
will convert the result
to a real value before assigning
more obvious example which uses characters and
it
literal.
on the
to y.
right-
Consider a
integers.
3;
The character combination
is
"a*
is
converted to
converted to
its
ASCII value before adding
float before assigning the
value to y.
3,
and the
This
is
an
Program Layout and Organization
8.6
example of where
295
up the unwary program-
implicit type conversion can trip
mer, since the addition operation cannot be applied to characters, but the internal representation of characters, in C.
C
also allows the
called a cast.
programmer
as integers.
is
to force a type
conversion using what
is
A cast is an explicit type conversion since the programmer must to make the conversion happen. A cast is invoked by
change program code
wrapping the type declaration
(double)
8.6
3
=>
3
in parentheses, as
shown below.
.
Program Layout and Organization examples above, the following general
In addition to the style presented in
guidelines can be followed to produce programs with
and
readability
few additional motivations programming style.
reusability. In addition to readability, here are a
for paying particularly close attention to •
maximum
Programs
that are difficult to read get
thrown away because they can't be fixed,
improved, or moved to other hardware. •
The simpler
•
Consistency within the program, and with other programmers, requires less read-
the style, the easier the
know where
ing effort, because you •
If
you
rors
8.7 Style for
start
code will be to read.
to look for what.
good
writing the program with
you might get
if
you
tried to put
it
style,
you might avoid generating
er-
in later.
C Program Layout
The program layout should be
similar to the outline of a paper. There should
be vertical separation (with blank lines) between major functional components.
Here are some guidelines for vertical spacing.
•
One blank
line before even, function.
•
One blank
line before
•
Sometimes before and
•
There should usually be one statement or declaration per
Code
is
and
after
after
easier to read
every block or banner comment.
programming constructs such
when
it
is
as loops.
line.
indented with respect to
example, when you type an "if-else" segment, which
is
its
function. For
used to make branch-
ing decisions, then the code associated with the if-else should be indented equally. •
Here are some guidelines on indentation:
Compound
statements should be indented tw
"containing" statement
was indented two
(e.g..
to
four spaces to the right of the
code associate J with the while example, above,
to four spaces horizontal]}
a ith
respect to the line on which the
29b
Chapter 8 Introduction to C Programming
while occurred.
The
•
identifier in declaration lists should be indented the
rounding statements. Variable names should In
•
segment comments are easiest
many programmers Inline
•
comments
to read
when
indent them the
same
are easiest to read
when
Spacing within statements
is
line
up
same amount
as sur-
vertically.
they use the entire line, although
as surrounding code.
they are justified together.
also important for readability.
C does not rec-
ognize multiple spaces, so you could write the following:
a=b+c*e/f +g;
Unfortunately, this
is
really hard
bols, operators, etc. out, the code
on the eyes! By simply spreading sym-
becomes much more
readable.
a=(b+c)*e/(f+g); Of course,
(b+c)
a=
too
much
f+g
e/(
*
space and the program becomes incoherent.
);
Here are some guidelines for horizontal spacing. •
Put one space between identifiers and operators
•
Put punctuation symbols directly after identifiers ter
to distinguish
(e.g.,
p
(a,
c).
b)
;
note space af-
from
(
{
and
}
),
because they are
difficult
plain parentheses.
Organization of
8.7.1
a = b +
comma).
Put space before and after curly braces
•
(e.g.,
C Programs
The organization of a program is as important as any other aspect of its preparation. If you see functions that run on for many screens, then you will understand. Here are some guidelines which will be useful: •
Functions should usually be less than a page or so long.
•
Pass variables needed by functions as arguments; avoid using global variables.
•
Cluster similar items. Put related variables close to each other within a declaration,
One
1.
and put related functions near each other.
last
comment. Every programmer works hard
Functions that perform similai tasks should be organized
in the
same
to
make
place. Also,
their
programs
most programs
have low-level functions on which higher-level tasks are based. Functions of the same abstraction level
should be located
in the
same
place.
8.8 Exercises
29/
and Sample Quiz Questions
run well. However, no one will appreciate a program unless they can understand
how
plicity
8.8
it
and
works.
clarity,
Make
all
of your programs stand out as examples of sim-
and everyone
will enjoy reading
your code.
Exercises and Sample Quiz Questions Exercises
8.8.1
Exercise
1
Change
the
message printed
be free." Recompile and
in
"FirstOut
.
c" to
"Learn C and
run.
Sample Quiz Questions
8.8.2
Problem
1
What
between syntax and grammar
the difference
is
in
C
(any lan-
guage)? Give an example of each.
Problem 2
What
between a low-level programming language
the difference
is
and a high-level programming language. What
is
C? What
are the
strengths of this type of language?
Problem 3
Explain the notion of language portability, and
why ANSI C
is
a
good
dialect to learn.
Problem 4
What
programming
the fuss about
all
is
readability)
.
.
.
what
style
(documentation and
the reasoning behind the use of consistent style
is
conventions? Provide two examples, each, of (1) itemsthat are com-
monly placed
into a program/function banner,
and
(2) readability
guidelines.
Problem
5
There are different semantic blocks of information which comprise a
C Problem 6
program. What are they, and what task does each perform?
What do
the
used, and
Problem
7
Name
symbols
and
{
when should
}
8
Show "
in a
C program? When can they be
a standard function and the library in which
library functions of this kind
Problem
mean
they (according to convention) be used?
a
C
f oo", is
made
it
is
found.
available for use in a
C
How are
program?
expression in which the value of an integer variable,
assigned a value of 65.
following line
is
Show
a
C
named
expression in which the
displayed:
The annual rainfall in Springfield, MA is 65 inches where you can assume teger variable
Problem 9
Why is
it
named
the case that
"
that the value (65) f oo it
is
is
associated with the in-
.
always best
to correct the first error that
find after compiling a program, rather than to fix as
Problem 10
What
is
a function prototype,
and why
is it
many
as
you
you can?
important to use them?
lapte
Decision Making, Iteration, and Functions
Introduction and
9.1
Scope
Although you have learned how have the basic tools
number of values
into the
say to see
fall in
if
they
to write
to construct real
simple programs in C, you do not
programs. For example, you can read a
program, but you do not
a certain range.
and over again, you would have
If
know how
you wanted
to
compare them,
perform a task over
to repeat the expressions as
wanted the task repeated, instead of using a
to
many
iteration construct.
times as you
This chapter
you the elements of programming in C that will provide you with these necessary capabilities, and it will also show you how to write your own functions so that your code need not always be in a single main function. The will teach
Ch9
examples
illustrated in this chapter provide a starting point for using these pro-
gramming constructs, and are available to the student in the "UNIX /Chap ter9" directory for manipulation and modification. The following topics are be presented in this chapter: •
Decision making
•
Iteration
•
Functions and program structure
At the completion of this chapter, you should be able to design and implement simple functional segments which demonstrate flow control and iteration with the different constructs available in the the C programming language. You should be able to write functions, and you should be able to return values from
299
OUO
Chapter 9 Decision Making,
Iteration,
and Functions
you write. You should understand the difference between paramby value and those called by reference, and you should be able to
the functions eters called
use variable addresses to properly assign and return parameter values through function headers. These abilities should be demonstrated in simple programs,
such as those presented
9.2
in this chapter.
Making Decisions In order to
make
a choice between alternatives,
you must have
choose between, you must have some operator which isons,
is
and you must have some metric for evaluating the
ample of two alternatives
is
to execute
one of two
sets
alternatives to
make comparcomparison. One ex-
used
to
of expressions. The as-
sociated comparison might be based on which of two variables has the greater value.
Making
different kinds of
comparisons thus provides a mechanism for
choice.
Simple Comparisons
9.2.1
you have two numbers and you want to know which one is nu(i.e., greater). In C, you use a relational operator for "greater than" between the variables you want to compare. The combination is an exor 1. If the one variable's pression which can be evaluated, and the result is value is greater that the other, a 1 is returned by the operation, otherwise a is returned. Such expressions are called logical expressions, because they return a truth value of the relational operator applied to the operands. It is called a truth value because it can only have a binary value (i.e., zero or nonzero), as do false and not-false. Logical operators and expressions are thus also called boolean operators and expressions. The value of all logical/boolean expresSuppose
that
merically larger
sions
is
either zero or nonzero. Consider the logical expression in
Comparison, variable that
is
c,"
shown
in Fig. 9.1. This
example
"Simple-
illustrates the use
of a
assigned the truth value of a simple logical expression. Vari-
ables which represent boolean variables are often called logical flags, because
they can be used to later decide whether to perform an action.
9.2
Relational operators
Figure 9.1:
in
AUTHOR
*/ */ */
J.B. Hodges August, 1993 Displays the result of a simple comparison between two integers.
/* DATE: /* FUNCTION: /*
301
simpleComparison.c
SimpleComparison.c
/* FILE: /* /*
and comparison
Making Decisions
*/ */ */
*/ Uses stdio.h function library /* Demonstrates the use of > relational op */ */ /* Demonstrates the use of if-else /•••••a**************************************************/
LIBRARIES COMMENTS
/*
ttinclude
int
main (void) {
int x, y,
z
printf "Enter two integers separated by spaces: scanf "%d%d" &x, &y) (
");
(
,
z
©
= x > y;
printf ("The value of return
(
z
is:
%d\n"
,
z);
0)
}
In this simple
and the value of y
example, the value of Z will be is
or 1. If the value of X
2 then the value of the logical expression ,
is
1
.
:
3
2
:
2
3
3
If the vari-
ables or variable values are switched, then the value of the expression
localhost> gcc SimpleComparison.c localhost> a. out Enter two integers separated by spaces The value of z is: 1 localhost>
is
is 0:
!
out Enter two integers separated by spaces The value of z is: a.
This
is
a pretty trivial example;
you can make. The > operator comparisons based on
how
is
however, there are many comparsions
variables relate to each other. Table 9.
er relational operators available in C.
1
that
it makes shows oth-
called a relational operator, because
302
Chapter 9 Decision Making,
Iteration,
and Functions
Table 9.1: Relational operators
in
C
=>
returns
1
if
x
is
less
than y
X
returns
1
if
x
is
less
than or equal to y
X == y
=>
returns
1
if
x
is
equal to y
X
!=
=>
returns
1
if
x
is
not equal to
X
>
=>
returns
1
if
x
is
greater than or equal to
=>
returns
1
if
x
is
greater than
X
y
= y
y y
y
Relational operators always return a value which can be interpreted as true
or false. In addition, any expression
whose value is nonzero can be interpreted whose value is zero can be inter-
as a true logical value, while an expression
preted as a false logical value.
Using Comparisons to
9.2.2 In C,
Choices
comparisons such as that shown above are usually part of a choice be-
example we determined whether x was greater than y. this comparison by asking a "what if* question. For greater than y. then maybe we want to add PI to x. and if it
tween options. In
We
Make
this
can use the result of
example, isn't
if
x
is
maybe we want
9.2). If
we can
the value of 3
to subtract PI
from
express this in C, then
y. as
we can
and y has the value of 5"?
shown
in
"Simple IF
.
ask the question "what
c" (Fig. if
x
has
Making Decisions
9.2
Simple path decisions
Figure 9.2:
/* It********* /*
simpleiF.c
in
***************************************
* *
* * * *
/
*/
FILE:
SimplelF.
AUTHOR
J.B. Hodges August, 1993 Displays the result of a simple comparison between two integers.
*/
/* /*
/* DATE: /
303
*
FUNCTION:
/*
LIBRARIES COMMENTS
*/ */ */ */
*/ Uses stdio.h function library /* Demonstrates the use of > relational op */ /* */ Demonstrates the use of if-else /********************************************************/ /*
:
#include #define PI 3.14159 int
main (void) {
int x,
z;
y,
printf "Enter two integers separated by spaces: scanf "%d%d" &x, &y)
");
(
(
,
z
x
=
> y;
printf ("The value of if
(
z
z
is:
%d\n"
,
z);
printf "Result of choice is: %6.4f\n", x else printf "Result of choice is: %6.4f\n", y return
@
)
(
(
+
PI);
(
-
PI);
0)
}
It
should
come
as
no surprise
that the
of comparison and decision making else)
is
is
C
construct which performs this type
called if-else.
one of the most powerful control structures
guage. The component inside the parentheses
used as a test
compound
in the
if
statement.
The
test is
The
in
if-else (or if-then-
any programming lan-
called a conditional,
and
is
used to decide between two
sets
of
is
expressions. If the test returns a nonzero (true) value, then the
set
of expressions
ed.
Note the format of the
is
selected. if
Otherwise the second else construct
(at
set
item
of expressions
1).
The
if
is
first
select-
and else are
re-
served words, so they must be typed exactly as they appear. The condition
always wrapped are spaced ed, either
in a set
is
of parentheses, and, in this example, the parentheses
from the expression
from a successful
test
for readability. All statements that are execut-
or an unsuccessful
test,
must be terminated.
If
OU4
Chapter 9 Decision Making,
iteration,
and Functions
the statements associated with either the condtional or the alternative
else) form a
When
compound
curly braces are used, the closing curly brace for the conditional expres-
The same
sions should precede the else. the expression
if
(
x
>
y
)
{.
(i.e.,
statement, then they must be enclosed in curly braces.
z
(
)
effect could be
produced by replacing
item (1) with the following:
at
.
way
without changing the
the
program works. The
SimplelF
ed by compiling and executing
localhost> gcc SimplelF. localhost> a. out Enter two integers separated by spaces: The value of z is: 1 Result of choice is: 6.1416 localhost>
3
2
2
3
.
if
construct can be illustrat-
c:
!
out Enter two integers separated by spaces: The value of z is: Result of choice is: -0.1416 a.
An if
important point about the
the condition
control falls
of the is
if
is
if
is
that the expressions
which
are executed
true cannot be reached otherwise. If the condition fails, then
through
can be used
the
if
in C.
to the next executable expression.
The
first is
an
if
without an else.
not met, then control falls through to the end of the
if.
Two other forms
If the
The format
if
is
condition the
same
as before, namely:
if
condition expressions (
{
)
}
The second form allows
for multiple tests,
by immediately following the
else with another if-else statement as follows:
if }
}
}
conditionl { express ions else if condition2 express ions else expressions3 (
)
(
)
{
(T)
9.2
second if-else construct
In this construct, a (1).
The
effect of
condition!
is
this set
of
is
Making Decisions
embedded
tests is to say that if condition
1
in the is
code
305 at
item
not met, and
if
met. then perform the second set of expressions, and so on. This
construct describes an
OR
relation,
meaning
that exactly
one of these
sets
of
expressions will be performed and that they are mutually exclusive.
Any one
of the three forms of
test is
made based on
scribes the range of values
met just
if
(
if
if
can be used to nest
tests.
the success of the previous test.
which meet both
criterion
Nesting means that
Thus
the set that de-
smaller than that which
is
the first test, or the second:
conditionl condition2 if condition3 expressions3 else { } express ions
(T\
)
(
)
(
{
)
}
else { expressionsl }
else { express ions }
This
is
called an
AND
statement, because each successive test
constraining the possibility of inclusion in the final nation of the
ifs
that the actions
No
matter
can be done as well, and of the
first
how complex
these
embedded
gle control structures, so the "outer"
use curly braces.
I
is
two conditionals ifs
if
Also note
is
further
Of course, any combi-
worth experimenting with. Notice are themselves other
if
statements.
statements are, they represent sin-
need not (and,
have used the plural of expression
pressionsl, etc.), so the use of curly braces tional.
set.
is
in general, (i.e.,
mandated
that all the statements associated with
should not)
expressions3, ex-
after the third condi-
an
if
are indented the
same.
Making Choices from More Than Two
9.2.3
The previous decision-making constructs each
Alternatives
select a set of expressions to
The basic mechanism allows a choice between two alternatives. The case where more than two alternatives else constructs, but it is a bit cumberexist can be handled with embedded evaluate based on the result of a comparison
test.
if
some.
C
has an alternative construct, called switch that selects one set of ex-
pressions
among many
possible cases.
The switch
is
a multiple-branch con-
306
Chapter 9 Decision Making,
struct.
in
"SimpleSwitch.
c" below:
simpleSwitch.c */ */
AUTHOR
J.B. Hodges August, 1993
/* DATE: /* FUNCTION:
*/
*/
Displays information about a dog based on family membership. Uses stdio.h function library Demonstrates the use of switch-case-break-default
/*
LIBRARIES COMMENTS
/*
shown
SimpleSwitch.c
/* FILE: /*
/*
is
and Functions
Multiple choice path decisions in
Figure 9.3:
/*
The format
Iteration,
/*
*/ */
*/ */ */
#include int
main (void) {
char dog; 'Enter the type of dog that you own by character \n" Below is a list of options you can use. \n\n" -- G\n" \ \tGolden Retriever -- L\n" \ Retriever \tLabrador -- F\n" \ XtFlatcoat Retriever XtChesapeake Bay Retriever -- B\n" \ -- C\n\n" \ "\tCurlycoat Retriever " Choice: scanf ("%c", &dog)
printf printf printf
.
)
)
-+ switch dog case 'G' case g' printf (" \nGolden retriever: child friendly \n" -« break; case L' case 1 printf (" \nLabrador retriever: dives for rocks. \n" break; case F' case f printf "\nFlatcoat retriever: black-gold. \n" break; case 'B' case 'b' printf (" \nChesapeake bay retriever: bird hungry. \n"); (
)
{
'
.
)
'
'
'
:
)
'
'
'
(
)
break;
® (2)
9.2
Multiple choice path decisions in
Figure 9.3:
case 'C case c printf break; default: printf
Making Decisions
307
simpleSwitch.c (cont)
'
'
'
(
"
("
XnCurlycoat retriever: never seen one.\n");
\nYou need a retriever \n" !
)
}
return
(
0)
}
The switch
invoked as shown
is
variable (in this case
value (2)
is
and
dog)
is
at
item
(1).
The programmer supplied
provided as the condition
The various options "case" is required,
interpreted at run-time. (3).
where the
string
(in
parentheses) and
its
are provided as per items as is the colon after the
choice. In this program, character variables are used in the condition and case;
however, integers can also be used
in the condition.
Both upper and lower case
options were used in this program, to allow for user discretion. Notice that the
empty statement body
after the first case (at item 1).
stacked in this way, the the cases preceding tion
a
it.
first
Once
nonempty statement block a match is found between
When
the cases are
will be applied to all of
the value of the condi-
and one of the options, the corresponding expressions are evaluated
break
is
found. In the switch-case construct, there
is
no need
until
to define a
compound statement and enclose it in curly braces; it is assumed that there will be a number of expressions to evaluate. Unfortunately, the construct doesn't automatically
fall
through to the end of the
ments, and, without a
ments is
in cases
break
which follow,
ended. At that point the
condition and there
is
a
after executing the state-
statement, will continue to execute the state-
until a
switch
break statement is found or the construct is
"default"
exited. If
none of the options match the
expression (item
4),
then the statements
default are executed. Otherwise the switch is SimpleSwitch c in operation is shown below.
following the lustration of
switch
.
exited.
An
il-
OUO
Chapter 9 Decision Making,
Iteration,
and Functions
localhost> gcc SimpleSwitch.c localhost> a. out Enter the type of dog that you own by character. Below is a list of options you can use. -- G Golden Retriever -- L Labrador Retriever -- F Flatcoat Retriever Chesapeake Bay Retriever -- B -- C Curlycoat Retriever Choice: G Golden retriever: child friendly.
Increasing the Specificity of Comparisons
9.2.4
So far, we have talked only about simple comparisons within a condition, those which utilize a single relational operator and two variables. However, it is often the case that you will want to have a condition which makes a more complex comparison, such as to check if values are within a range. For example, you might want to find out if some person's age is between 21 and 30. If so, then you might want to perform some task on the person, and another task if not. In order to determine whether a variable's value is within a range, you have to perform two comparisons. This can be done with nested ifs of course, but that
is
not a single condition. Multiple comparisons in a single condition
require the use of logical connectives. In
programming
logical tests.
it is
For example,
a frequently desirable to if
you want
combine expressions with
to say:
IF xl AND x2 AND x3 OR yl THEN actionsl where "xl," "x2," "x3," and "yl" are potentially complex expressions, then you need to have some mechanism for performing the AND and OR tests. These are called logical connectives or logical operators. The logical connectives in C are demonstrated in the program "CompComp c." .
Making Decisions
9.2
Compound
Figure 9.4:
logical
expression comparisons
in
Compcomp.c
***** /*
*/
CompComp.c
FILE:
/*
*/
J.B. Hodges August, 1993 Performs a compound comparison regarding the use of specialized cloth for sewing.
AUTHOR: /* DATE: /* FUNCTION: /*
/*
*/ */ */ */
*/ Uses stdio.h function library Demonstrates the use of C logical opers */ /* */ for AND (&&) and OR /***************************•****************•***********/
LIBRARIES: /* COMMENTS: /*
(
|
|
#include int
main (void) {
char matl; int hweave,
lweave;
printf "Enter the type of material you are using. \n"); printf "Below is a list of options you can use.\n\n") printf "\tLight Packcloth -- L\n" \ "\tMedium Packcloth -- M\n" \ -- C\n\n" \ "\tCordura (
(
(
"Matl:
")
scanf ("%c", kmatl);
switch case case
case case
(
ma tl
'L' '1'
:
'M'
:
'm'
:
case •C case C '
'
)
{
:
hweave lweave break
=
hweave lweave break;
= 420;
hweave lweave
=
52 0;
=
1000
=
1000 1000
=
=
220; 420;
42 0;
:
:
break;
defau It:
hweave lweave
=
309
O
Chapter 9 Decision Making,
I
Compound
Figure 9.4: if
(
(matl == (matl == (hweave >= (lweave >= (
'L' 1'
)
and Functions
comparisons
logical expression
in
Compcomp
.
c (cont.)
||
)
'
Iteration,
&& &&
)
200) 400)
(T\
{
)
printf \nLight duty: stuff sacks. \n" else if (matl == 'M' (
"
}
(
)
(
)
|
(matl == 'm' && (hweave >= 400) && (lweave >= 400) { printf (" \nmedium duty: back packs. \n" ); else if (matl == 'C (matl == )) ScSc (hweave >= 50 0) && (lweave >= 1000) { printf (" \nheavy duty: mountaineering packs. \n"),else printf (" \nMake jackets with oxford cloth. \n"); )
)
)
}
(
)
(
'
|
|
C
)
}
return
(
)
}
In this example, both the in
compound
OR
tests. In the first
)
( |
and the
AND
(&&) operators are used
|
such
test
(item
1).
the first test
is
to
check the
material type (a char variable). Both upper and lower cases are acceptable an-
swers from the user and so an
OR
relation
ceptable, then the vertical and horizontal
constraining in
tests,
so an
used. If the material type is
is
ac-
checked. These are deemed
AND is used in the expression. The program is shown
execution below.
localhost> gcc CompComp.c localhost> a. out Enter the type of material you are using. Below is a list of options you can use. -- O Oxford Light Packcloth -- L Medium Packcloth -- M -- C Cordura Matl: C heavy duty: mountaineering packs.
is
weave
9.3 Iteration
311
9.3 Iteration Programs are most useful to
in
allowing tasks which are performed repetitively
be codified into a single package. In a similar vein, some tasks are inherently
repetitive,
and programs which solve them are particularly useful. Consider a
simple program which counts from one number to another number in user-provided increments.
None of the programming
constructs which have been pre-
sented so far can perform this task with variable limits or a variable increment.
You would have to print the starting value, add the increment to it, test to see if the new value was greater than the ending value, print the new value, and so on
until
you had written all the statements. If you changed the ending value, would have to rewrite the program by adding or subtracting groups
then you
of statements. Fortunately, task for you: (1)
while,
a looping construct
and
C
(2)
has three iteration constructs which perform this
for, and
(3)
will generally
do-while. Each
be referred
of these
to as such.
We
is
called
will intro-
duce the while construct with the simple counting program called "Count
ByN.c:" 9.3.1
While Loops
A looping construct needs to know:
( 1 )
when
to
perform
iteration,
and
(2)
what
The while loop construct is the most general iteration mechanism in C, because it gives the programmer the most control over when and how iteration is performed. The while loop has two components: (1) a conditional, and to iterate.
(2) a
body, but must explicitly control four related tasks: (1) initialization, (2)
an entry/exit condition, called a loop control expression, which
is
based on a
loop control variable. (3) an update mechanism, and (4) a loop body of associated executable statements.
Ol 2
Chapter 9 Decision Making,
Simple
Figure 9.5:
Iteration,
and Functions
iteration using a while loop in
/* FILE: /*
CountByN.c
/*
J.B. Hodges August, 1993
countByN.c
*/ */
AUTHOR: /* DATE: /* FUNCTION:
*/ */
*/ Displays a table of integers counted */ by an increment between two values. /* APPROACH: Uses stdio.h function library and while */ /* COMMENTS: */ Demonstrates the use of the while loop /* */ Demonstrates the use of two assignment /* */ operators, var++ and +=. /•••••a**************************************************/ /*
#include int
main (void) {
int first,
last,
counter, current;
incr,
printf (" Input the starting/ending integers, and step: scanf "%d%d%d" &first, &last, &incr)
");
(
,
current counter
=
first;
=
1;
-" while current < last printf ("%5d %5d\n", counter, current); counter++; -* current += incr; (
)
{
}
return
(0)
The used
initialization assigns values to the loop control variables
in the
entry condition and the update mechanism. In
initialization
variable,
is
counter
is
noted
at
item
(1).
which are
CountByN.
where current, which
is
c. the
the loop control
assigned
its initial
value. Item (2) also notes an initialization of the
variable,
which
used
times the loop body
is
is
in the
program
to
keep track of how many
executed. Part of the initialization for the while can be
considered the reserved word while
itself,
which invokes
the construct.
The
while reserved word must be followed by the entry condition and the body of the loop.
The loop
control expression defines the loop entry condition and exit con-
313
9.3 Iteration
dition.
The entry condition
is
a test which must be met before the loop body
can be executed. The entry condition
comparison between the value of while entry condition
in the
current
met, the while body
is
example
(at
item 3)
is
condition
satisfied
is
CountByN.
in
the is
tested again.
is
c should be cycled as long as the entry
as long as the value of
(i.e.,
When
executed exactly once. This
called a loop pass or cycle. After the pass, the entry condition
The body of the while
a simple
is
and the value of last.
current
is
smaller than the
value of last).
The update mechanism modifies sion so that
it is
the variables in the loop control expres-
possible to exit the loop. In order to determine whether the loop
can ever be exited, the exit criterion should be looked the negation
(i.e.,
the exit condition
logical is
satisfied
when
equal to the value of last. Since
the value of
last
The while body entry condition
is
as noted at is
the
item
that is
the associated
mechanism must
statement which
CountByN.
compound
while statement and not with respect
is
this
is
example,
greater than or
never changes throughalter
c, the
is
current
up-
when
the
executed
body consists of
current
the decision-making constructs, the while signals a
why
exit condition
(5).
compound
satisfied. In
it
statement, a counter update (item 4), and an update of
As with
The
current
is static (i.e.,
out the program execution), the update
ward toward last,
at.
complement) of the entry condition. In
new
at
a printf
item
(5).
context, and
statments are indented with respect to the to the outer function
(main).
Increment and Decrement Operators The two update expressions in CountByN. c present the application of two new kinds of operators. The first is an increment operator + + ), and the second is an assignment operator + = ). An increment operator performs the same task (
(
as adding
1
to a variable value, except that the
change
in variable
value takes
++ is placed before or after when the + + precedes the vari-
place at a different time depending on whether the the variable identifier. able, as
A
prefix increment
is
shown below.
++counter
In this case, the value of the
evaluated, and then the value of
counter.
If the
++ follows
counter is incremented, the new is assigned. That is, after
expression
is
incrementing
the variable identifier, the operation
is
called a
postfix increment.
counter++ In postfix increment, the expression
is
evaluated, and the value assigned
314
Chapter 9 Decision Making,
to
Iteration,
new, based on
counter.
In both
the
and Functions
present value of
been incremented and has the same value.
ByN.
counter,
examples, after the expression
When
is
before incrementing
evaluated
counter has in Count
used alone, as
two operators have the same effect, but increment operators can be a source of off-by-one errors and care should be taken when using them. There are also decrement operators which function the same way as the increment operators. c, the
Table 9.2: Increment and decrement operators
Expression
Initial
in
C
New
Value
new
=
current ++
=>
current =
1
current =
new
=
++current
=>
current =
1
current
—
=>
current =
2
current =
--current
=>
current =
2
current =
new new
current =
Values 2,
new
=
= 2,
new
= 2
1,
new
=
2
1,
new
=
1
1
Assignment Operators
An assignment operator modifies the basic assignment of variables in an arithmetic way. The assignment operator shown in CountByN c at item (5) is an .
addition operator and works as follows.
The two following expressions below
are equivalent:
current = current current += incr;
+
incr;
Thus, instead of simply incrementing or decrementing the variable
rent,
this operator lets the
programmer
select
what the increment
cur-
will be.
Likewise, there are assignment operators for the other arithmetic operations (Table 9.3).
9.3 Iteration
Table 9.3: Assignment operators
in
315
C
Operator
Example Expression
+=
current += expr
=>
current = current
+
expr
-=
current
-=
expr
=>
current = current
-
expr
*=
current
*=
expr
=>
current = current
*
expr
/=
current
/=
expr
=>
current = current
/
expr
%=
current %= expr
=>
current
of
Equivalent Expression
The use of arithmetic assignment operators c shown below:
CountByN
is
current % expr
illustrated in the execution
.
localhost> gcc CountByN. localhost> a. out Input the starting/ending integers, and step: 1
=
3
31 4
3
2
7
3
11 15 19 23 27
4 5 6
7
where the increment is 4 and the arithmetic operator adds 4 of current during any particular iteration. 9.3.2
The C
to the current value
For Loops for loop performs the
same function
as the while loop;
however, instead
of the abstract structure in the while, the initialization of the loop control variable, the entry condition,
and the updating mechanism for the loop control
vari-
able are defined in the syntax of the construct, so as long as they are defined
properly the loop will exit normally. The format of the for
loop-control-variable-initialization; entry-condition; loop-control-variable-update-expression loop-body
for
}
(
)
{
is
shown below:
316
Chapter 9 Decision Making,
Iteration,
As an example of its
use,
and Functions
CountByN
.
c has been rewritten with a for loop
below. Execution produces identical output.
Simple
Figure 9.6:
iteration using a for loop in
CountByNFor c .
/it*******************************************************/ /* FILE: /*
CountByNFor
/*
J.B. Hodges August, 1993
*/
AUTHOR: DATE: /* FUNCTION: /*
LIBRARIES: COMMENTS:
/*
*/ */
Displays a table of integers counted by an increment between two values. Uses stdio.h function library Demonstrates the use of the for loop
/* /*
*/
.
*/
*/ */
*/
#include int
main (void) {
int
first,
last,
incr,
counter,
current;
printf (" Input the starting and ending integers and the step: &first, klast, &incr) scanf "%d%d%d"
" )
;
(
,
counter
=
1;
Mj
current = first; -«* current < last; -* current += incr { counter, current); printf ("%5d %5d\n" counter++;
for
(
*~
(2)
(3)
)
,
}
return
(
0)
}
In
"CountByNFor
.
c" the for reserved word invokes the construct. The
components of the for control mechanism are all embedded in a set of paOnly the entry condition is a logical expression, the other two can be any legal expression. The initialization expression (at item 1) is an assignment of the value of first to the variable current, which is the loop control variable in this example. The entry condition is the test (item 2) of whether three
rentheses.
current
is
less than the terminating value,
last. The updating expression
(item 3) applies the incrementing assignment operator to
value of but
is
incr. Note
that the variable
otherwise not used
in the
counter
is
used
control of the construct.
able should not be modified in the loop body.
current
in the
The
with the
body of
the for.
loop control vari-
9.3 Iteration
9.3.3
317
Do- While Loops
The do-while loop is identical to the while except that it is used when the loop body should be continually cycled until a condition is met. One of the most frequent uses of a do-while loop is for user input. For example, if the programmer to
a do-while
an appropriate construct to use. Consider the program
is
PosValues Simple
Figure 9.7:
keep typing values
wants the user
.
until a
non-negative value
is
entered, then
"Get-
c" below.
iteration using a
/* FILE: /*
GetPosValues.c
/* AUTHOR: /* DATE:
J.B. Hodges August, 1993
do while loop
in
GetPosValues.c
*/ */ */ */
/* FUNCTION: /*
Reads and displays integers until a non- */ */ negative value is read. /* APPROACH: */ Uses stdio.h function library /* COMMENTS: */ Demonstrates the use of the do-while /********•********•**************************************/
#include int
main (void) {
int readval;
do
{
printf " \nlnput an integer: " scanf("%d", &readval) printf (" \nlnteger value read is: %d\n" readval); while readval gcc GetPosValues.c localhost> a. out Input an integer: -1 Integer value read is: -1 Input an integer: -10 Integer value read is: -10 Input an integer: Integer value read is: Input an integer: 10 Integer value read is: 10 Final integer value read is: 10
Operations with Loops
9.3.4
Many tasks can be performed with loops that will not be presented here but that you should experiment with on your own. A common use is to nest looping you perform the same operations on different variables. For you wanted to produce an addition table, you could run one loop whose control variable is the first addend, and you could run a second loop and whose control variable is the second addend. The operation would only be performed on the inner loop and would perform the addition of the two addends. constructs so that
example,
if
In a nested loop, the inner loop cycles through all of
values for each value
its
of the outer loop, so the inner loop initialization must occur within the outer loop. Nesting can be done with any
Another task cling.
that
the
can be performed with loops
One simple example
a negative integer
is
number of odd
number of variables.
input)
values.
is
count events while cy-
some criterion (say, number of even values and
is
to read in integer values until
is
met. and to count the
The programmer must
create variables to keep the
current tally of even and odd values, and increment
9.4
to
them within
the loop body.
Functions As
a program
debug, and
becomes more complex, it also becomes more difficult to read, The design of a program is usually accomplished using an ap-
test.
proach called iterative refinement.
In this
approach, the original problem
broken into identifiable subtasks. Then each of those subtasks subtasks. and so on until each subtask
is.
finally refined to the point
fined to
its
where
can be written as source code directly. The problem with
is
that
it
none of the code gets written
written,
even though
all
until the
is
is
in turn, re-
most specific code
is
this
approach
designed and
of the higher-level organization has already been done.
Another approach which uses
iterative refinement
is
called
modular pro-
modular programming, the overall problem solution is associated with the main function; however, each subtask can be associated with a
gramming.
In
9.4 Functions
function called by
main. As
the refinement process deepens, the
319
programmer
can define new functions to solve the related subtasks. The functions are themselves miniprograms which address a particular
component of
the overall
problem. The construction of functions and their interaction with the program
which
calls
them
All functions (e.g.. system library functions, user-defined functions, the
main
have the same general structure: a header, declarations, and a
state-
function
)
ment block,
as
shown
Function structure
in
in the function
"MySquare:"
MySquare
/********************************* /*
*******
*/ */
^
MySquare: Calulates the square of a values
*/
/* /* /* /* /* /*
which follow.
Function Format and Definition
9.4.1
Figure 9.8:
will be addressed in the sections
INPUT: OUTPUT: LOCAL: RETURN: USAGE:
varval None sqval double
*/
[main]
*/ MySquare (4) => 16.0 **************************-* /
*/ */ */
double ~* MySquare double varval
function type declaration function header
(
{
double sqval sqval
return
varval
=
(
sqval
)
*
varval; -*
;
function return
Comments The its
function body
first
in
Functions
thing that you should notice about this sample function
own banner comment. The
is
that
it
has
banners associated with a function are different
from those for a program, usually shorter. Functions generally require a banner that tells what the function name is. what it does, what its input and output parameters are, and what
it
returns.
Sometimes, a sample
function banner, to remind the reader of
1.
main is
a function that
ments of a program.
is
the starting point for
all
how
programs.
It
the function
call is useful in the is
invoked.
organizes the source code frag-
320
Chapter 9 Decision Making,
Iteration,
and Functions
Function Parameters The source code shown
in Fig. 9.8 defines a
function called
"MySquare"
which has one parameter "varval," and one local variable, "sqval." A parameter is a variable which is declared in the function header and serves as an interface element to the calling module. Each function parameter must have a one-to-one correspondence with a function argument called.
the value of the local variable If there is
tion
when
A local variable only has meaning in the scope of the another variable with the same it
out adverse consequences. This a variable's value
is
is is,
restricted to the context of the function body.
is
which called MySquare,
the function
function. That
name
in
another function, or the func-
can have a different value assigned to
is
it
with-
called lexical scoping, because the scope of
restricted to the local statements.
Function Types There are two types of functions:
( 1 )
functions intended to return a value; and
(2) functions intended to communicate, and perhaps modify, variable values
MySquare is one of the former which return values can be used on the right-hand side of expressions, since the value they return is typed and should fit into the context of the expression. The library functions sqrt and pow return values and can be used in expressions directly. The value they return takes the place of the function call when the expression in which they reshared with other functions. The function function types;
side
it
returns a value. Functions
evaluated.
is
Function Type Declaration Whether or not a function will.
returns a value,
it
should be declared as though
A void function need not be declared, but good programming
gests that
it is
returned,
it
name
is
best to declare even
void functions. To specify
it
style sug-
the type of value
necessary to specify the function type along with the function
in the declarations.
This
is
done
just prior to the function header.
The
MySquare is declared a double function, meaning that the funcshould return a double value. If a function is not declared, the compiler
function tion
default
9.4.2
is
type int.
Function Application
A function is invoked (i.e., applied) with a function call from another function. The program shown nization and use of
in Fig. 9.9,
MySquare.
"FirstModRet
.
c" demonstrates the orga-
9.4 Functions
Function structure
Figure 9.9:
FirstModRet c .
*************** ************
-*********-
FILE:
FirstModRet
/
AUTHOR
/
DATE:
J.B. Hodges August, 1993
/*
in
321
.
/*
Reads a value to square, squares it, and and then prints both on a line. APPROACH: Uses stdio.h function library. The / /* calculation is performed in function /* MySquare /* FUNCTIONS: MySquare calculates the square COMMENTS Demonstrates modular design for a function with a return value. Demonstrates the use of prototypes. ************************ -**********: ************* FUNCTION:
/ /
:
ttinclude
®
double MySquare (double varval); int
main (void) {
double val,
sq;
printf \nlnput a value to square: scanf("%f", &val); sq = MySquare (val printf (" \nThe square of %6.4f is %6.4f\n\n", val, (
"
"
)
,
)
return
(0)
,-
sq)
322
Chapter 9 Decision Making,
Figure 9.9:
Function structure
Iteration,
in
and Functions
FirstModRet c .
(cont.)
/•••A****************************************************/
MySquare Calulates the square of a values */ */ double varval the value to be squared /* */ double sqval the squared value /* */ None /* */ double sqval */ /* [main] MySquare(4) => 16.0 /******•************************************•************/ /*
/*
NAME: INPUT: LOCAL: OUTPUT: RETURN: USAGE:
:
:
:
©
double MySquare (double varval) {
double sqval; sqval
=
varval
varval;
*
return (sqval);
Program Organization The first thing that you need to notice in this main function is listed first, and MySquare is by the function prototype noted
sible
item
at
listing is the organization.
The
made
pos-
listed second. (1).
A
copy of the function declaration and header (item
The prototype
function body.
is
This
is
function prototype
is
a
4) without the associated
terminated with a semicolon. The prototype
must come before any function definition and there should be one prototype for every function in the
grammer
except for
file,
main. The prototype allows
anywhere
to position functions
in the file,
because
it
the pro-
already has a
definition of the declaration, number, and type of parameters for the function. If the
prototype
is left
out, the
program
will
compile with warnings, since the
may
functions will automatically be declared as type int. which the actual declarations
when
clash with
found.
Variable Type Consistency The second item of
main
interest is the variable declaration in
(item 2).
It is
very important that the variable and function types be consistent throughout the
program and
double,
its
functions.
The parameter varval multiplication, is
returned to
tin.' \
as
The function MySquare
is
declared as a type
double MySquare and,
so the value returned should be consistent with the
ariable
double,
is
is
declared as a
double
in
double variable sqval. The The value returned by MySquare
value of
assigned to a
main
(item 5).
sq (item
2) in the function call (item 3).
so the type logic
is
which
consistent throughout.
is
is
type. after
sqval
assigned to
declared
in
main
323
9.4 Functions
Function Call The
final note
each of
its
of interest
is
the function call
itself.
When
a function
is
defined,
parameters must be declared. They can have any names the pro-
grammer wishes because of variable scoping; however, the order and types must be consistent with the function call, in which function arguments will be paired by order. Because of this requirement, the variable identifiers used in the calling function need not be the
same
There are advantages and disadvantages able identifers the
same between
as those in the function parameter to
doing
functions, the
so. First,
list.
by keeping the vari-
programmer
is
confused with
many
identifiers will
probably make more sense to the programmer
less likely to get
variable identifiers floating about. Second, the variable
with the main, or calling, function.
On
if
kept consistent
the other hand, an important strength
of modular programming and procedural abstraction
is
the separation of func-
tions so that they are't mutually dependent. If the function parameters are as-
sociated with what the function does, and not with what
it is
used for by any
particular function or program, then the function can be used
grams. This extends
its
by many pro-
usefullness.
The function call for MySquare, in main (item 3), has the single argument val. When the function is called, the value associated with val is copied into the MySquare parameter varval. As long as the types are assignment compatible there
is
no problem.
In this case, both
val and varval
are declared
double. localhost> gcc FirstModRet .c localhost> a. out Input a value to square: 5.874 The square of 5.8740 is 34.5039
9.4.3
Functions that Pass Variable Values
As mentioned
previously, there are functions which return values and there are
functions which manipulate variables. There are three kinds of functions that
manipulate variables: (1) output functions, (2) input functions, and (3) input/ output functions.
Output Functions
An output
function
is
one that
may have
values which are passed to
the calling function, but the function itself does not
way which would
affect the calling function.
modify the values
it
from
in
any
The most common form of output
functions are those which print initial instructions to a user or print variable
1.
Strictly speaking,
one thinks of
input, output,
and input/output parameters, rather than functions;
however, there are examples of functions which perform these tasks.
324
Chapter 9 Decision Making,
values, such as
and Functions
Iteration,
"PrintVals"
Figure 9.10: Function structure
in
in Fig. 9.10.
Print vals
/***************************************************•****-/ /*
PrintVals: Prints output variables
*/
/*
/* /*
INPUT:
/* OUTPUT: /* RETURN:
char int None None
first, middle,
*/ */
last
ndogs
*/ */
*/
*/ [main] PrintVals (chl ch2 ch3 num) /••a-*****************************************************/
/* USAGE:
,
void PrintVals (char argl, char arg2
,
char arg3
,
;
,
Mj
int arg4)
,
{
printf
(
"
\nHello, %c %c %c, you own %d dogs, right?\n\n", argl, arg2 arg3 arg4); ,
\
,
return; }
This function has four parameters: argl. arg2. arg3, and arg4. The
char
first
three parameters are
first
function example with
ue. (1).
types, and the last
is
an
int
more than one parameter, and
it
type. This
important to declare each parameter separately. This
It is
Notice that there
turn statement,
PrintVals
and
is
is
our
has no return valis
noted
at
item
only one expression in the function other than the re-
no assignments
that there are
in the function.
function only produces a side effect to display
otherwise has no effect on the program which calls
it.
some
Thus, the
values, but
Besides functions that
return a value, output functions are the easiest to understand, because they effectively take a
copy of the value associated with a variable and perform some
task on
This
is
called call
with a variable
is
provided to the function, the value
it.
by value. Because
a
copy of the value associated in the original
value
is
not
affected.
Input Functions
An
input function
one
is
that has
parameters whose values will be used
in the
calling function, but are initially assigned values in the local function. If
more
module, then the return
state-
than one variable's value
ment cannot be used. case, because
many
is
needed
in the calling
by value would be inefficient
In general, call
variables use a
would be computationally
lot
costly and
memory
such a
would take up even more memory
Since the programmer already knows that the variable value
change, the original
in
of memory. Copying of variable values
is
address and value can be manipulated
going
to
directly
9.4 Functions
This
is
called call
by reference, because
325
the function call uses the address of
The main program can provide to the function the address of the variable whose value is to change, and the function can return a value into that address which main can use. The only the variable rather than the variable value
thing passed to the function
is
the
itself.
memory
address for the variable (see Fig.
9.11):
Figure 9.11: Function parameter passing,
memory addresses, and
pointers
#include #def ine void GetArgs (...); void PrintVals (...) .
.
.
.
int
main (void)
©
{
char fi, mi, int
la;
nd;
GetArgs (&fi, &mi return
,
&la,
1234567
&nd)
B
(0)
-^
7654321
H
memory void GetArgs (char *fip, char *mip, char *lap,
inst *ndp)
{
scanf "%c%c%c" fip, mip, scanf "%d" ndp) (
,
lap);
(
,
The main function and
the function
it
calls
can share the same address and
values, but have different identifiers. In Fig. 9.1 is
used
In the
GetArgs
char" variables.
A
pointer
address.
1.
item 1) to reference the addresses of the
(at
is
A
the address-of operator (&)
char
variables
mi and
la.
function header, parameters are declared of type 'address-to-
These are called pointers, because they "point"
to the variable.
an object that can be referenced like a variable, but represents an
pointer
is
declared as follows.
326
Chapter 9 Decision Making,
type
*
Iteration,
and Functions
identif ierp
Example
Type
Pointer Identifier
char *fip
=>
char
int *ndp
=>
int
fi P
*
ndp
*
type * declares a variable to be a pointer of type type, and identif ierp is the identifier for the pointer. Using this notation, f ip mip, and lap are pointers to char variables, and ndp is a pointer to an int variable. where
,
The "p" pointer. ers.
in the identifier It is
name
not required, but
The pointer
is
to a variable
helps to remind the
is
programmer
that this is a
for keeping track of point-
and the address of a variable are different ways
of referencing the same variable (item the address (at item 3)
mechanism
a useful
2).
The
(variable) value associated with
then the same even though
it
being referenced
is
through different identifers. Fig. 9.12
the
shows an example of
keyboard
in the
Figure 9.12: Output parameters
in
function
a typical input function that reads values
GetArgs
and returns them
to
from
main.
GetArgs
/••••••••••A*********************************************/ /*
GetArgs:
*/
Reads input variables
*/
/*
*/ None */ char *firstp, *middlep, *lastp */ /* int *ndogsp */ /* LOCAL: None */ /* RETURN: None /* USAGE: */ [main] GetArgs (&fi, &mi, &li, &nd) /•a******************************************************/ /*
INPUT: /* OUTPUT:
;
void GetArgs (char *firstp, char *middlep, char *lastp, int *ndogsp)
©
{
printf "\nPlease type your initials, separated by no spaces: scanf "%c%c%c" firstp, middlep, lastp) printf (" \nPlease type how many dogs you own: "); scanf ("%d", ndogsp) (
(
,
return;
")
9.4 Functions
The function parameters: item 1) are the addresses in
f
irstp, middlep, lastp, and ndogsp
memory
stored. Notice at item (2) that
memory
O.c,"
is
9.13. This
the
two functions
.
are repeated for coherence.
top of the
file,
because
functions in any order. This program
dRet
.
c.
main
is
C
just presented. In this
Note
that the
similar to the previous
main
program,
function
is
allows the programmer to organize
similar in organization to
Item (1) notes the function prototypes for
Vals. Also
PrintVals and "FirstModl-
program does the same thing as the display and input segments of the code
c program; however, the implemented with the functions we have at the
al-
complete program,
are
organized
because the parameters
functions, particularly
illustrated in the following
illustrated in Fig.
FirstIO
is
addresses.
The use of input and output
GetArgs
(at
where the variable values are the variables being read with scant do not re(pointers)
quire ampersands (address-of operators). This
ready represent
327
program
is
GetArgs
FirstMoPrint-
and
the use of local variables in
(item 2), which hold the values that are associated with the functions.
328
Chapter 9 Decision Making,
Iteration,
Figure 9.13: Use of input parameters
in
and Functions
FirstModio.c
/******************************************************-**/ /*
FirstModlO.C
FILE:
*/
/*
*/
/*
AUTHOR: /* DATE: /* FUNCTION:
J.B. Hodges August, 1993
*/
*/
Reads user's initials and number of dogs */ and then prints them on one line. */ /* APPROACH: Uses stdio.h function library. Input is */ /* */ separate from main. Output is separate /* */ from main. /* FUNCTIONS: */ GetArgs reads the input data /* */ PrintVals: displays the output /* COMMENTS: Demonstrates modular design for I/O with */ /* */ functions. /••it*****************************************************/ /*
:
#include
void PrintVals (char argl char arg2 char arg3 int arg4) void GetArgs (char *firstp, char *middlep, char *lastp, int *ndogsp) ,
,
,
Mj
;
int
main (void) {
char fi, mi, int
la;
(2)
nd;
GetArgs (&fi, &mi &la, &nd) PrintVals fi, mi, la, nd)
(3)
,
(
return
(
;
0)
}
Referring back to Fig. trates the use
9. 12, the
function call for
GetArgs
(item 3) illus-
of call by reference, since the address-of operator refers to the ad-
dress of the associated variable.
So any
which
variable
is
manipulated
in a
function will be referenced in the function call with an address-of operator (just as for
scant and
GetArgs
for the
same
reasons). This
is
why On
are passed in with address-of operators.
the four arguments to the other hand, an out-
put function does not need to have consistent values for variables with ing function, so a
copy of
its
call-
the variable value can be sent into the function. Al-
though our examples have used
all call
by reference or
call
by value,
this is not
What is important is that the programmer recognize which arsomehow coming back into the main or calling function and
a requirement.
guments arc which are not.
9.4 Functions
Figure 9.13: Use of input parameters
in
FirstModio.c
329
(cont.)
/*********************•**********************************/ /*
*/
GetArg: Reads input variabless
*/
/* /* /* /*
/* /*
*/
None
INPUT: OUTPUT: LOCAL: RETURN: USAGE:
firstp, middlep,
ndogsp
lastp,
*/ */
None None
*/
GetArgs (&f i
[main]
&mi
,
,
&li,
*/
&nd)
void GetArgs (char *firstp, char *middlep, char *lastp,
Mj
int *ndogsp)
printf \nPlease type your initials, separated by no spaces: scanf "%c%c%c" firstp, middlep, lastp); printf (" \nPlease type how many dogs you own: scanf "%d" ndogsp) (
"
");
(
,
"
)
(
,
return; }
/***********•********************************************/ /*
PrintVals: Prints output variables
*/
/*
*/
/*
INPUT: /* OUTPUT: /* RETURN: /* USAGE:
first, middle,
ndogs
last,
*/
None None
*/ */
PrintVals (chl
[main]
void PrintVals (char argl
char arg2
,
,
,
ch2
,
ch3
char arg3
num)
;
*/
int arg4
,
{
printf
("
\nHello, %c %c %c, you own %d dogs, argl arg2 arg3 arg4 ,
,
,
right?\n\n",
\
)
return; }
The program FirstModIO c .
is
compiled and run below.
localhost> gcc -o fmioc FirstModIO. localhost> fmioc Please type your initials, separated by no spaces: JBH Please type how many dogs you own: 2 Hello, JBH, you own 2 dogs, right?
O\30
Chapter 9 Decision Making,
Iteration,
and Functions
Modular Programming and Procedural Abstraction
9.4.4
The design of programs using functions utilizes the method of iterative refinement discussed on page 319. The idea of looking at a problem statement and identifying the different tasks which need to be performed is the basis for all program development. The modular approach to program development uses programming constructs with predictable behavior to design the program. Procedural abstraction means that the conceptual subtasks which are used to solve a problem, or a subproblem,
make up what
for the problem. If these subtasks can be
solution to a subtask interface
is
called an abstraction level
is
implemented with functions, then the
kept distinct from the problem
itself,
because only the
shared between the original function call and the function imple-
is
mentation. If each conceptual level
grammer is
is
implemented
in this
way, then the pro-
change how a task is implemented, or solved, without components of the problem. Procedural abstraction has some of which are discussed in Chapter
free to (later)
affecting the other
many
benefits,
1
In order to illustrate the application of
dural abstraction, let's construct a
program
1
modular programming and proce-
to solve the simple but possibly real
problem presented below:
Box
Balls in a Suppose
that
you have
store in boxes,
box
and
that
as possible. Write a
a
number of
you want
you want
balls that
to store as
many
program which, given
to
balls per
the diameter
of the balls and the dimensions of the box will return the
number of
balls that will
This
into the box.
fit
that all balls for a particular
box
is fairly
You
will be the
illustrative
can assume same diameter.
of the types of programming problems that you
are likely to see, except that normally the
an example of how the input played.
now,
We
is
to
problem statement
be formatted and
will consider this aspect of the
as long as
it
looks reasonable
when we
how
will also include
the output
problem somewhat
is
to
be dis-
arbitrary for
are done.
Solution Feasibility
The question
that
one must ask oneself before embarking on
even be done? This that
it
is
this task
called a question of feasibility. Clearly,
could be done, as long as
we can
figure out the
it
volume of
is:
can
it
makes sense the
box and
volume for each ball, because we can then divide one by the other to get the number of balls. Almost. Balls will actually take up more volume than their own volume, so we have to have the actual volume that the ball requires. Now. can we do this? The answer is yes! The next step is to identify the mechanism the
whereby
the
problem solution
will be
brought about.
We
have written our po-
9.4 Functions
tential
approach
in English, but
formalized description of identify the
how we can puts. In this
gorithm
volume volume actual number number number number
is
how
we need
to translate this into
to solve the
331
an algorithm, a
problem. In order to do
so,
we must
knowledge domain needed to solve the problem, and then show use it to get from the program inputs to the desired program outexample, the domain knowledge is algebraic, so designing the al-
not really a problem:
= length x width x height of box of ball = 4/3 x pi x radius-cubed volume of ball = (2 x radius) -cubed of actual volumes along length = length / (2 x radius) = width of actual volumes along width / (2 x radius) of actual volumes along height = height / (2 x radius) of actual volumes that will fit into box nicely = 0\ j x (2j x (3)
As you can problem, but
it
see, is
we do
not really need the volumes for the balls to solve the
better to write
down
extra information and
wonder why you cannot solve description already needs some refinement in how we name cide on a few variable names now: clusion than to leave
boxvol ballvol actvol numvlength numvwidth numvheight numvols numballvols numacts
=
it
out and
come
to this
con-
the problem. This things, so lets de-
blength x bwidth x bheight x PI x ballrad x ballrad x ballrad 8 x ballrad x ballrad x ballrad blength / (2 x ballrad) bwidth / (2 x ballrad) bheight / (2 x ballrad) numvlength x numvwidth x numvheight boxvol / ballvol boxvol / actvol
= 4/3 -
= = = =
= =
As you can
see,
we have
kept the ball volume and actual volume for com-
parison purposes, and that there are really only 4 input variables
bwidth, bheight. brad) and one cess. All the other variable values
(blength,
constant (PI) that are used in this pro-
can be calculated from these.
Subtask Refinement
Now
that
we have some
to solve the Balls tasks.
Here
defined).
is
a
faith in the eventual success of
and Boxes problem, we need
first
developing a program
to organize the
program by sub-
cut at an algorithm (the algebraic tasks have already been
Oo2.
Chapter 9 Decision Making,
and Functions
Iteration,
display instructions
obtain values for 4 variables
perform volume calculations for box and
ball
perform comparison calculations display information
modular programming approach, each of these subtasks will either in the main function or they will be handled by functions. Since we have already seen two functions in this chapter that perform similar tasks In the
be statement
as items labeled (1), (2)
and
our needs accordingly. This
assume that we can modify them to suit where modular programming satisfies the need
(3), lets is
for code reusability.
Framework Construction Often the most
difficult task in writing a
main
program
is
to write that first level of
committment to the variable names, their types, and the function types, even if you think they might change later on. Lets give it a try. Lets modify the program FirstModlO. c, because it has a similar structure. Here is what I have done prior to displaying the program below: functions and the
Copy
rename
the file and
Edit the banner
Add known Modify
to protocols, calls,
we
and headers.
already have to do the right thing.
Leave stubs for the functions we haven't written
Granted
in
this is a
The 9.14. The
yet.
number of steps, but we do not want to print file, named "BallsAndBoxes
intermediate
times.
Fig.
requires a
main program and the functions.
for the
names
the functions that
many
It
it.
comments
variable
program.
.
three functions that
were similar
to
this thing out
c,"
shown
is
PrintVals
and
GetArgs
should be very straighforward to understand, as the the same func-
tionality
invoked from them and only the names and number of variables,
and
literal strings,
math add
is
are being changed. Item (1) notes that
library functions if
this
we
are going to use the
include statement, the program will
plain during execution, but
you
will get
still
pow
we have
function. If
compile, and
it
to
add the
you do not
will not
com-
junk for the values associated with the
was never used. Item (2) notes that we have to add a constant for PI for the true ball volume calculation. Item (3), notes the function protocols for the program. This is really a very good way to keep track of all the variables you have chosen, especially if you have the main program located at the function which
top of the
file,
since your function calls will
all
be close to the protocol defini-
333
9.4 Functions
tions ing.
and
it
will be easier for
you
programmer of what these odd I
perform type and argument position check-
to
Item (4) notes that sometimes some inline comments serve to remind the identifiers refer to.
never place variable descriptions
tion banner.
I
have placed them inline
space. Item (5) tells the novice
For
my own
inline, but, rather, in the in this
C programmer
example
programming. program or func-
for conservation of
that functions without argu-
ments must use parentheses or the function will not be invoked. You get an error, but nothing related to the function will happen. Item (6)
bly the one interesting thing in this
first series
of changes.
variables, such as
blen
are being passed in but are not
function, whereas others, such as
boxvolp
proba-
We have not mixed
input and output variables in previous examples; however, in
some
will not
is
CalcVols
changed by the
are being modified in the func-
Hence you see both call by value and call by reference in the function and you will also see it in the protocols (item 3) and headers (item 7).
tion.
call,
334
Chapter 9 Decision Making,
Iteration,
and Functions
Figure 9.14: Main program and function calls for /* ********** /* FILE: /* /* AUTHOR /* DATE: /* FUNCTION: /* /* /* APPROACH /* /* /* FUNCTIONS /* /* /* /* COMMENTS
BallsAndBoxes.c * *
BallsAndBoxes
/
*/ */ */ */ */ */ */ */ */ */ */ */ */ */ */
.
J.B. Hodges August, 1993 Reads information about boxes and balls and returns information about how many balls will fit into the box. Uses stdio.h function library. Uses the algorithms for box volume and sphere volume GetDims reads the object dimensions PrintNums: displays the number of balls CalcVols: calculates object volume CompVols: compares object volumes Demonstrates modular program design 'A-****************************; ************•*•**•***••**• * * :
i
#include #include #define PI 3.14159
void PrintDirs (void) void GetDims (double *blenp, double *bwidp, double *bdepp, double *bradp) int *numvlengthp, int *numvwidthp, int *numvheightp) void CalcVols (double blen, double bwid, double bdep, double brad, double *boxvolp, double *ballvolp, double *actvolp, void CompVols (double boxvol, double ballvol, double actvol, int *numvolsp, int *numballvolsp, int *numactsp, int numvlength, int numvwidth, int numvheight); void PrintNums (int nact, int nballs, int nvols);
© ® ®
int
main (void) double blength, bwidth, bheight, ballrad, boxvol, ballvol, actvol; int numvols, numballvols, numacts, numvlength, numvwidth, numvheight; PrintDirs GetDims (kblength, CalcVols (blength, &actvol, CompVols (boxvol (
)
*/ box dimensions */ ball dimensions /* volume variables */ /* output variables */ /* /*
©
;
&bwidth, &bheight, &ballrad) bwidth, bheight, ballrad, kboxvol, &ballvol, &numvlength, &numvwidth, &numvheight ballvol, actvol, &numvols, &numballvols, fcnumacts, numvlength, numvwidth, numvheight); PrintNums (numvols, numballvols, numacts); return (0) ,
)
©
9.4 Functions
Figure 9.14:
PrintDirs function
for
BallsAndBoxes.c (cont.)
/****************************************-* user
***********************
/* PrintDirs: Prints instructions to /* /* INPUT: None /* OUTPUT: None /* RETURN: None /* USAGE: [main] PrintDirs (
/
)
******************************************************************
void PrintDirs (void) {
printf
\n
************************************ • * * *
* * *
BALLS AND BOXES
* * * * * * * * *
** * * * *
* * *
*** * * *
* * * * * * * * * * * * * * * * * * * * *
* ** ****
return;
:
-q
ir
\
***\n" \ ***\n" \ This program calculates the number ***\n" \ of balls of radius 'R' that will ***\n" \ fit into a box of dimensions 'L', ***\n" \ 'W and 'H' ***\n" \ ***\n" \ The user enters the box dimensions ***\n" \ and the ball radius. ***\n" \ ***\n" \ The program displays the number of ***\n" \ balls that will fit based on three ***\n" \ measures ***\n" \ ***\n" \ ***\n" \ (1) cubic volume displaced ***\n" \ (2) true volume displaced ***\n" \ (3) actual volume displaced ***\n" \ *********************************** ******* \n\n-) ,
* * *
\
.
335
336
Chapter 9 Decision Making,
Figure 9.14:
Iteration,
and Functions
GetDims function and function stub
for
calcvols function
(cont.)
/•••it**************************************************************/ Reads box and ball dimensions variables */
/* GetDims: /* /* INPUT: /* OUTPUT: /* RETURN: /* USAGE: /* /* /*
*/ */ */ */ */ */ */ */
None blenp, bwidp, bdepp, bradp
None [main]
GetDims (fcblength, &bwidth, kbheight, &ballrad)
/••••••••••••••••A*************************************************/ void GetDims (double *blenp, double *bwidp, double *bdepp, double *bradp) {
printf \nPlease type the box dimensions, separated by spaces: scanf "%f %f %f blenp, bwidp, bdepp); printf (" \nPlease type the ball radius: "); scanf ("%f", bradp); (
"
" )
(
"
,
return; }
/* CalcVols: Calculates object volume /* /* INPUT: blength, bwid, bdep, brad /* OUTPUT: boxvolp, ballvolp, actvolp, numvlengthp, /* numvwidthp, numvheightp /* RETURN: None /* USAGE: [main] /* /* CalcVols (blength, bwidth, bheight, ballrad, &boxvol, &ballvol, /* &actvol, &numvlength, knumvwidth, &numvheight /* )
*/ */ */ */ */ */ */ */ */ */ */
/******************************************************************/ void CalcVols (double blen, double bwid, double bdep, double brad, double *boxvolp, double *ballvolp, double *actvolp, int *numvlengthp, int *numvwidthp, int *numvheightp) {
return; }
®
9.4 Functions
Figure 9.14:
/* /* /* /* /* /* /* /* /* /*
CompVols function stub and PrintNums function
(cont.)
*/ */ */ */ */ */ */ */ */
CompVols: Compares object volumes INPUT:
OUTPUT: RETURN: USAGE:
337
boxvol, ballvol, actvol, numvlength, mimvwidth, numvheight numvolsp, numballvolsp, numactsp None [main]
CompVols (boxvol, ballvol, actvol, &numvols, knumballvols, toumacts, numvlength, numvwidth, numvheight);
*/ */
/*
void CompVols (double boxvol, double ballvol, double actvol, int *numvolsp, int *numballvolsp, int *numactsp, int numvlength, int numvwidth, int numvheight) {
return; }
/* PrintNums: Prints output variables /* /* INPUT: numvols, numballvols, numacts /* OUTPUT: None /* RETURN: None /* USAGE: numballvols, [main] PrintNums (numvols
*/ */ */ */ */ */
numacts); /******•*********•***************•****************************•*•**/ ,
void PrintNums int numvols, int numballvols, (
int numacts)
{
printf ("\nCUBIC DISPLACEMENT: %3d balls will fit into the box\n" "TRUE DISPLACEMENT: %3d balls will fit into the box\n" "ACTUAL DISPLACEMENT: %3d balls will fit into the box\n\n" numvols, numballvols, numacts);
\ \ ,
\
return; }
What we have
in this first
attempt
not function properly simply because
is
a
program which
some of the
will
compile but will
guts are missing. Let us
now
complete the phase with one of the functions, and leave the other as an exercise.
We decided earlier that the calculations that we we could
wanted
to perform,
that calculate the object volumes and the number of along any particular box dimension, would be as follows.
ball
given
volumes
338
Chapter 9 Decision Making,
numvols numballvols numacts
= = =
Iteration,
and Functions
numvlength x numvwidth x numvheight boxvol / ballvol boxvol / actvol
so
all
we need
to
do
is
convert this pseudocode
into the function statement block. This has
Figure 9.14: Pseudocode conversion
in
1
into source
code and
dump
it
been done below:
compvols (cont
.
/* CompVols: Compares object volumes /* /* INPUT: boxvol, ballvol, actvol, numvlength, /* numvwidth, numvheight /* OUTPUT: numvolsp, numballvolsp, numactsp /* RETURN: None /* USAGE: [main] /* /* CompVols (boxvol, ballvol, actvol, &numvols,
*/ */ */ */ */ */ */ */ */
/*
*/
&numballvols, &numacts, numvlength, numvwidth, numvheight);
/*
*/
/•••••••••A********************************************************/ void CompVols (double boxvol, double ballvol, double actvol, int *numvolsp, int *numballvolsp, int *numactsp, int numvlength, int numvwidth, int numvheight)
M)
{
*numvolsp *numballvolsp *numactsp
=
= =
numvlength * numvwidth boxvol / ballvol; boxvol / actvol;
*
numvheight;
return; }
Notice that those variables which are being called by reference use what
is
called the indirection operator, and those that are called by value are used directly.
The
indirection operator takes as an argument a pointer and returns the
associated variable. Hence,
if
we want
only have access to through a pointer, tion operator.
As
.
we must
dereference
a final illustration of functions in C,
run from the program
I
to assign a value to a variable
we just
I
am
it
which we
with the indirec-
including a sample
developed.
Pseudocode means an English version of the algorithm that is so close to source code that it is It is often easier to write source code on paper as pseudocode and translate it when
one step away. editing.
9.5 Exercises
and Sample Quiz Questions
339
localhost> gcc BallsAndBoxes.c localhost> a. out ******************************************* ***
***
BALLS AND BOXES
* * *
* **
*** This program calculates the number *** of balls of radius 'R' that will *** fit into a box of dimensions 'L', *** 'W and 'H'
***
The user enters the box dimensions and the ball radius.
* * *
*** ***
,
** *
The program displays the number of *** balls that will fit based on three *** measures: ***
* **
*** *** ***
(1) (2)
(3)
* **
cubic volume displaced true volume displaced actual volume displaced
*** *** ***
***
******************************************* Please type the box dimensions, separated by spaces: 12.5 24.1 10.5 Please type the ball radius: 2.25
CUBIC DISPLACEMENT: TRUE DISPLACEMENT: ACTUAL DISPLACEMENT:
9.5
20 balls will fit into the box 66 balls will fit into the box 34 balls will fit into the box
Exercises and Sample Quiz Questions 9.5.1
Exercises
Exercise
1
Complete the
program Exercise 2
last
make
to
after the
it.
BallsAndBoxes and run the works properly with your function.
function in
sure
Write a program that
5000
Exercise 3
to
it
starts
entire
with the integer 1000 and repeatedly adds
After each addition, print the resulting sum.
What happens
sum exceeds 2.147.483.649°
Write a program that accepts as input an integer "n" and then adds "1
0/n" to itself "n" times. For example, if the input is 5, then the sum is the real number "1.0/5 + 1.0/5 + 1.0/5 + 1.0/5 + 1.0/ 5." After computing this sum. the program should compare it 1.0" and "not to 1.0 and output the word "equal" if the "sum = equal" otherwise. Run your program with different values of "n" and try to determine the smallest integer for which the sum does not equal 1
.
.
.0.
This type of error
is
called a round-off error.
340
Chapter 9 Decision Making,
9.5.2
Problem
Iteration,
and Functions
Sample Quiz Questions 1
Which
relational operator(s) can be used to identify if data
C
read into a
named
a variable
Write a
C
the value
value.
When
that
its
that the data
is
value should be from
which
is
assigned to to 9. inclu-
test.
that reads values into an integer variable "foo."
name of the
within range, print the
the value
variable.
variable and
its
out of range, print an error message and the
is
The range
is
shown below:
logical connective operators (&&
!
,
,
combinations of logical expressions.
|
=) are used to construct
Provide an expression which
performs the same function as the one provided
Problem Problem 4
is
< foo < 9
•
The
Assuming
correct?
and
"foo,"
program
When
name of the
Problem 3
is
provide a set of expressions that perform the
sive,
Problem 2
program
in the latter part
of
1.
Rewrite the program
in
"bar" and compound
Problem 2
to use
logical expressions.
two variables: "foo" and Use the ranges of values
below. 10
Problem 5
< foo < 99
< bar < 9
•
What
are the
components
to a general iteration construct?
ponent does an entry/exit condition
fit
and what
into,
is
What com-
the relation-
ship between an entry condition and an exit condition?
Problem 6
Write a
C
program
to read in 10
numbers from the keyboard and
to
count the number of values which are less than 25.
Problem 7
Which
arithmetic operator can be used to determine even multiples of
number? Which assignment operator can be used decrement a number? a
Problem
8
Wrote
a
C program
to read in 10
numbers from the keyboard and
count the number which are multiples of to a
Problem 9
power equal
What
is
to the
to exponentially
3.
Print the
number
to
3 raised
number found.
the importance of the function call/function header relation-
ship?
Problem 10
Write a
C
program which
their values,
and prints the
with a function.
iteratively reads
result.
10 values, accumulates
Implement each functional segment
10.1
Introduction and In this chapter,
we
Scope
will extend
your
arrays and structures. In Chapter 9.
C programming
include the use of
skills to
you extended your
ability to write
simple
programs, enabling you to direct execution, repeat code segments, and develop
your
own
functions. In order to take full advantage of the
power of these con-
you must be able to use them to locate and compare data as well as to use the data generated. Simple variables can be used as long as they are being used right away, but otherwise a structured variable, one that keeps track of many variables and their values, is needed. Once the need structs, particularly iteration,
for variable organization
nization in data files, so
is
recognized, a logical extension
you
will then learn
how
is
to read data
to use the orga-
from a
file into a
and write it out to a file from a structure. Finally, often the data you compare requires access to different variable types, in which case new structured data types are necessary. We will introduce these concepts and their application in small examples, the source code for which can be found in the "UNIX/ChlO" directories. The following topics wil be addressed in this chapstructure
want ChlO
1^
to
ter:
Comparing
results of
Reading data from a Writing data out
many
iterations
file into
an array variable
to a file
Structured variables for unlike data types
More on memory addresses
When you
have completed
this chapter,
you should understand how array
341
342
Chapter 10 Arrays, Structures, and Files
and structure data types are defined, declared, accessed, and used in simple programs and functions. You should be able to read values into array and structure types, and you should be able to read information from text files into program variables or write information from program variables to text files.
10.2
Comparing One
Iterations Using Arrays
of the most useful functions of iteration
which
are indexed to a loop counter.
feature because that they
we
did not have any
So
is
to
compare values of variables
we have
far,
not been able to use this
way of organizing
variables in such a
could be indexed (related) to one another and.
so,
compared by
way their
order.
10.2.1
The Array
An array
is
as Structured Variable
a structured variable in that
same type. An array scheme for accessing identifier
char double
and a type
is
organizes
it
the values of those variables.
like
many
variables of the
actually a collection of variables, and an indexing
An
array variable has an
any other variable, as shown below:
obj_type; obj_sizes [20] In this
example.
variable because
it
obj_type
j_sizes
is
variables
organizes, and
it
is
a regular variable, often called a scalar
can only have one value
an array variable. its
Its
identifier
double
type
On the other hand, obobj_sizes refers to all of the
at a
is
time.
the type of
all
of the variables
it
An array is declared when the identifier is associated with more than value by square brackets and a value. obj_sizes actually declares double variables, since its type is double and the value in brackets
organizes. a single
up is
to 2
2 0. Although the value
is
2 0. the value
is
only used to define the largest
number of variables which can be associated with 10.2.2 Using
Array Variables
A programmer can do with array lar variables.
the array variable.
variables anything that can be done with sca-
They can be accessed, assigned
values, reassigned values, and
they can be used as arguments in functions. Moreover, the array index (the
number inside the brackets, e.g., 20 in [20]) can also be manipulated in the same ways. The array index always starts at and goes to n - 1, and the index must always be an integer or a character. Let's look gram,
"SimpleArr
.
c" (Fig. 10.1
)
at
an example pro-
and see how arrays work
in
C.
10.2
Figure 10.1: Array Usage
Comparing
Iterations
343
simpleArr.c
in
*/
;:~;leA:r.:
/* FILE:
Using Arrays
/*
*/
/*
AUTHOR
/* DATE: /* FUNCTION /*
/* /* APPROACH -it**********/
J.B. Hodges August, 1993
*/
Calculates an addition table of integers and then determines how many of them are multiples of a user input value. Uses stdio.h function library. Uses array
*/ */
*/
*/ */
#include #define MXSZ1 100 #define MXSZ2 200 int
main (void) {
f starting addend values
startl, start2, maxl max2 add[MXSZl] [MXSZ1] counterl, counter2
int
,
f /'
incr,
/
vals[MXSZ2], numincr;
/'
*/ */
limits of each addend storage array loop counters the search increment values matching search the number of incr found
*/ */ */ */ */
printf "Input starting and ending integers for addendl scanf "%d%d" &startl, &maxl) printf "Input starting and ending integers for addend2 scanf "%d%d" &start2, &max2 printf "Input the search increment: " scanf ("%d", &incr) (
")
(
,
(
")
(
,
)
(
)
numincr
=
0;
for counter2 = start2 counter2 gcc -o CmdArgSum CmdArgSum. localhost> CmdArgSum 3 4
The
3
in the
and 4 here are called
command
main block of CmdArgSum.c
arguments. To do
line
this
we
include
the following:
void main(int argc, char *argv[])
The operating system knows that are included
on the
(as strings) to the array etc.
Note
that
argv
is
to assign to
command
argv
line,
and
as elements
argc
the
to assign
argv
[
]
number of arguments
each of these arguments ,
argv
[
1
]
,
argv
2
[
an array of unspecified length, where each element
is
]
a
pointer to a string. In the example:
localhost> CmdArgSum
3
4
5
argc gets the value 3, meaning that there argv is always the command name, [
is
]
the string
"CmdArgSum."
Table 10.1:
are 4 arguments.
All of the values are
argv values
in
command Value
Element
argv[0]
=>
"CmdArgSum"
argv[l]
=>
"3"
argv [2]
=>
argv [3]
=>
"
4
"
"5"
The
first
so the value assigned to
line
shown
in
argument
argv
Table 10.1.
arguments
[
]
Array Variables
10.3
in File
Operations
34 1
10.3 Array Variables in File Operations Our
last
program had a special nested loop
how
used to show
and
to generate
have
arrays are used.
we can
which we
written a single nested loop
but that would have been less fun and wouldn't
test the values,
set us up for this example.
would be nice
It
from a
table into an array variable
to generate the values
We could have
read large data
UNIX
file
if
we could
read the addition
and then analyze
it.
If
we can do
arrays and perform bulk anal-
this,
then
ysis.
We can let someone else worry about how to generate the data file.
ing data from a
file into a local
subtasks: (1) declaration of a
when
Read-
data structure requires the completion of three variable, (2)
file
finished. (3) close the local
There are two types of nary
files into local
file.
files that
These
open the
file for
reading, and.
will be the topics of this section.
can be read
in C:
(
1
)
text files,
and
(2) bi-
Most files which are read into programs are text files, and most of you have seen in this book are text files. A text file is an ASCII, or
files.
the files editable,
file.
In a text file,
each viewable
The end of
nevvline character.
line
ends with a special character
The end-of-line character
called an end-of-line character.
is
which cannot be seen under normal All of the information in the
file:
characters at the end of each
line,
as a
editing. After the last end-of-line character
the characters for ever)' line, the end-of-line
and the end-of-file character, are organized
as a single set. or string, of characters.
When
the file
is
viewed, the end-of-line
characters are interpreted, and you "see" lines on a screen.
The
which makes up the
is
file is
associated with a name, which
set
of characters
the filename.
referenced in the operating system just as a variable
is
known
another special character, called an end-of-file (EOF) character.
in the file is
filename
also
line character is usually a control character
is in
The
a program.
10.3.1 File Declaration
To
read from a
be declared
file
in the
(instead of the keyboard or standard input), a variable
program which
tem. This will be called a
we
file
must
will represent the file in the operating sys-
variable. Since the local variable and the real
file
them by making use of the file's address in memory. Thus the file available in the program must be declared as a pointer associated with the file which can be referenced in the program. Until now. we have referred to pointers as the memory addresses of variables, and that is what they are. but it is easier to call them pointers, so we will do so henceforth. The file are different,
variable
FILE *infilep,
is
refer to
declared as follows:
*outfilep;
where inf ile might represent the name of a file that data will be read from. and out file might represent the name of a file that data will be written to. There need not be any correlation between the file name and the pointer vari-
348
Chapter 10 Arrays, Structures, and Files
able,
have done
I
to identify pointers
pointers and
10.3.2
A
is
an example only. The notation of using a "p" after a ptr, or some other notational mechanism) makes it easier in source code. The "FILE *" type is associated with file
this as
pointer variable (or
required.
Opening a Local
file is
opened
File for
for use with the
Read or Write
fopen
function,
ments: (1) a string that represents the local
and
file
(2) a character that designates whether the
fopen requires two argu-
name
in the operating system,
file is to
be used for reading
The return value of a call to fopen is a pointer to the file, and it is the pointer which is used in subsequent operations (i.e., read and write). Consider the program "SimpleFilelO. c" shown in Fig. 10.2, which illustrates the use of files for reading values into arrays and writing them back to files when done. The first portion of the figure is documentation the ("r") or writing ("w").
way
I
like to prepare
Figure 10.2: Array Usage
in
it,
as
opposed
to other
examples
in this text.
simpleFileio.c
/************* ****** ******************************************** /* FILE: /* /*
AUTHOR
/*
DATE: FUNCTION:
/* /* /*
/*
APPROACH LIBRARIES:
Hodges August, 1993 Reads values from the UNIX file system a local array select values within a user defined range, and writes those to another file in the UNIX file system. Uses arrays to store the file contents. Uses the string function library.
*/
*/
J.B.
*/ */ */ */ */ */ */
/* /*
*/
CONSTANTS
/*
STRSZ 20 100 MAX
-
Maximum string size Number of allowable successes
*/ */
*/
/* /* /* /* /*
/* /* /*
/* /*
/* /* /*
/
*/
,
/* /*
* *
Simpl eFilelO.C
VARIABLES
FILE FILE char char int int int int int int int int
*infilep *outfilep infilen [STRSZ] outfilen [STRSZ] val lower upper vals[MAX] succ[MAX] counter, counterl nsucc status
-
*/
input file pointer output file pointer input filename output filename value from infilen limits of age range limits of age range storage array ages in range loop counters number of successes number args fscanf read
*/ */ */ */ */ */
*/ */ */ */ */ */
/* /* ****** ****** ******
********************************************
k
*
/
10.3
Figure 10.2: Array Usage
Array Variables
simpleFileio.c
in
in File
Operations
349
(cont.)
#include #include #define STRSZ 20 #define MAX 100 int
main (int argc, char *argv
[
]
{
*infilep, *outfilep; infilen[STRSZ]
FILE char
,
outfilen [STRSZ] int
val, lower,
upper, vals [MAX] succ[MAX] counter, counterl, nsucc, status;
strcpy (inf ilen, argv[l]); strcpy (outfilen, argv[2]); infilep = f open (inf ilen, "r"); outfilep = fopen (outfilen, "w"
printf "Input the youngest/oldest ages in search range: scanf "%d%d" &lower, &upper) (
(
,
counter nsucc
= =
;
;
status = f scanf (inf ilep, "%d", &val); status == 1 && counter < MAX; status = f scanf (inf ilep, "%d", &val) vals [counter++] = val;
for
for if
(
)
counterl = 0; counterl = lower && vals [counterl]
Use CFILE
as the core
-e PFILE
=>
Use PFILE
as the executable file
-s SFILE
=>
Read
the
symbol
table
from
SFILE
-se PFILE
=>
Read
the
symbol
table
from
PFILE
-q
=>
Don't print introductory banners (quiet option)
-x CMDFILE
=>
Execute
dump
gdb commands
to
from
examine
and use
it
as the executable file
CMDFILE
Once started, gdb takes and evaluates one command at a time. The commands can be of any length, and their names can be abbreviated up to the point where they cease
to
are three types of
commands
serve.
be unambiguous. Other than exiting the debugger, there
The step, next,
in a
debugger: (1) execute, (2) stop, and (3) ob-
run, and continue
commands
are execute
commands.
Exits
blank line
=>
Repeat the
help [name]
=>
Show
gdb last
command
gdb command name, gdb
information about
information about using
break FUN
=>
Set a breakpoint at the entry to function
break LIN
=>
Set a breakpoint at line
break FIL:FUN
=>
Set a breakpoint
break *address
=>
Set a breakpoint at
enable BNUM
=>
Enable the breakpoint(s) specified by
disable BNUM
=>
Disable the breakpoint! s) specified by
clear FUN
=>
Remove any
condition BNUM EXPR
=>
at
FUN
number LIN
FUN in
function
address
FIL
file
(asterisk
breakpoints for function
Break for break number ates
or general
BNUM when
is
required)
BNUM BNUM FUN
expression
EXPR evalu-
nonzero
backtrace FRM
=>
Show
frame FRM
=>
Examine
up N
=>
Select and print the frame
down N
=>
Select and print the frame
stack frames for frames the stack for frame
FRM
FRM
N frames up from N
frames
the current frame
down from
the current
frame
info frame
=>
Displays information about the current
list LIN
=>
Displays ten lines centered around line number
list
=>
Displays ten more lines
list-
=>
Displays ten lines before the lines
print expr
=>
Display the value of an expression
step
=>
Execute each statement one
run [arglist]
=>
Start
c
=>
Continue running your program
your program, with
at
last
frame
LIN
printed
a time
arglist.
if
specified
after a breakpoint
11.4
11.4.2
gdb - A Sample
Using
Debuggers
383
Session
A sample run using a debugger can quickly show the merit of using gdb during program development and testing. Consider the bus error that was discussed in section 1 1 .2.2. The error was in the scant function, where the programmer had forgotten to use an ampersand (&) to reference the input variable. To use gdb with this program, the following sequence could be followed:
localhost> gcc -g CountByN.c localhost> gdb -q (gdb) add-file a. out (1j) Reading symbols from /Users/hodges/a. out .. .done done Reading symbols from /usr/shlib/libsys_s B. shlib. (gdb) run (g) Starting program: /Users/hodges/a out Input the starting/ending integers, and the step: 1 100 10 (3) Program generated(l) Memory access exception on address 0x50242fc (protection failure) 0x501564e in _sys_nerr () (gdb) bt (4) .done. Reading in symbols for countbyn. c #0 0x501564e in _sys_nerr () #1 0x64 in ?? () x501514e in _sys_nerr () #2 #3 x504d5b2 in _sys_nerr #4 K3e3 6 in main () at countbynbug c 22 (gdb) list 22 17 main (void) .
.
.
.
:
.
.
(
.
18
19 20 21 22 23 24 25 26
:
{
int
first,
last,
incr,
current;
counter,
printf (" Input the starting/ending integers, scanf "%d%d%d" &first, &last, incr); (
,
v current counter
=
first;
=
l,
ns
Error Here continued..
In this
ByN
.
sample session, an error was introduced into the program "Count
c" in the scant
call,
by removing the required ampersand address-of op-
incr
The program was comfile for debugging. The '-q.' debugger was then started with the quiet option, The first command issued by the user from within gdb, (item 1), was to load the executable file "a out" into the debugger. The response from the debugger is to display the path to the executable and some other information. The next command (item 2) was to run the program. Although this sample session will only show the ac-
erator
from the variable
piled with the '-g' option,
.
(see 'Error Here' line).
which
sets
up the executable
384
Chapter 11 Programming Tools
commands and responses from a debugging session, the user can issue commands for assistance at any (gdb) prompt. Simply type "help" to get a list of top-level topics, or "help topic" to get a list of lower-level topics for a specific top-level topic. You can then see what alternatives and optual
tions there are.
The
program (a out)
which where the values 1,100, and 10 were input to correspond to the variables "first," "last," and "incr." The debugger then notes a memory address exception and provides the address where the error occured. At this point, it is often useful to find out result of running the
.
displayed by the program. This
is
where at
4).
noted
at
item
is
This provides a
full listing
the request for input
(3),
execution trace the error occured, so
in the
item
is
I
type "bt" (for backtrace,
of the trace by stack frames, which are
basically function call environments, along with associated address entry is the address in main and the line on which the error was program had had many functions, instead of just one, then the backtrace would have shown the function calling sequence all the way back to main. The frames lower than scant in the backtrace are calls from scant, so
points.
Most
useful
flagged. If the
the actual error
was detected
inside of scant.
The
line
number next
to
main
helps to locate which scant produced the error.
Item (5) notes a
now know
ther side of line
22
to
(gdb) list *0x3e36 0x3e36 is in main (countbyn. c 22 17 main (void) :
18 19
command
to
list
to look there for a problem.
show
the source code at line 22, because
The debugger
will then
list
lines
on
context.
(6) )
{
first,
int
20 21 22 23 24
last,
incr,
counter,
current;
printf "Input the starting integer/ending scanf "%d%d%d" &first, &last, incr); (
...
(
,
current counter
25
=
first;
=
1;
26
gdb c ont i nue (jj Continuing. Program generated (1) Memory access exception on address 0x50242 fc (protection failure) 0x501564e in _sys_nerr () (gdb) step (8) Current function has no line number information. Single stepping until function exit. (
)
:
1
1
Program exited normally. (gdb) quit
^^^
____
(9)
we ei-
Another
command, by address, and the address, not just
listing
type the asterisk
385
Developing Program Libraries
11.5
shown
is
at
item
the address.
(6).
Note
The user must
that the listing
is
identical.
From
this point
and
tify the error,
it
becomes necessary
this is not
programmer be able
that the
to iden-
always easily done, and requires more familiarity
with the debugger. This session
is
only intended to provide the novice with
enough clout
to use the
the available
commands. The command
debugger and
to
experiment further on your at
item (7)
tells
own
debugger
the
with
to re-
sume execution of
the program, after which the execution is stepped (item 8) which happens to be a single step. Normally, stepping allows the user to 'walk' through program execution and view the environment, but in this case the error occurred right away so stepping was of little value. Item until termination,
(9)
shows
the exit
Program Libraries
11.5 Developing
Header
11.5.1
Large
C
from the debugger.
Files
programs are often divided
into several
bined with the use of #include statements.
We
components which
are
com-
have used #include statments
to library functions. The #include statement inheader file, which is a listing of the function prototypes for the function names, and corresponds to a C source file by the same root " (i.e., it has a c" suffix) name. Programmers can define their own libraries by creating the appropriate header and C files, and then including them in whatever programs require the library.
to provide access in a
cludes what
program
called a
is
.
When created:
creating a library of functions from a source
( 1 )
two new
file
with the
files are
name of the library followed by h" and (2) name of the library followed by " c." The header "
a header file with the
a source code file
file,
.
.
consists of the constant definitions and prototypes for each function in the
library,
and should have a general banner describing the
library: its use,
its
con-
tents,
and any suitable comments. The declaration for each function prototype
in the
header
file
should be preceded by "extern," which declares the proto-
type to be exportable to other
files.
The primary requirement of using user-defined clude statement, double quotes
( "
" )
er file instead of the normal angle braces (
).
"My_Functions
brary into a program would appear as
libraries is that, in the in-
are used to delimit the
.
name of the head-
Consider the inclusion of a
c."
shown below.
The
inclusion of this
li-
oOD
Chapter 11 Programming Tools
Figure 11 .5:
A header file
template
/*******•**********************•************************/ /*
My_Functions
.h -
header file
*/
/* /*
*/
Library of functions intended to support some task
*/
Included functions:
*/ */
/* /* /*
funcl func2
/*
...
*/
/* /*
funcn
*/
*/
*/
#define XYZ
(J)
/***************************************•***************/ /*
funcl banner from source code file
*/
extern typel funcl (parameters)
/******************************************•*•**********/ /*
func2 banner from source code file
extern type2 func2
*/
(parameters);
The creation of the source code library "My_Functions c" may require some changes to the source code for the functions. The functions should be rewritten to make them as general as possible. This may include changing constant and function names, and possibly moving constants into the function parameter list. In addition, the header file must be included, as shown above, for .
both the program which uses the library and the library implementation (i.e.,
types must be
Figure
1 1 .6:
Use
My_Functions c Of course, removed from My_Functions c first.
source code
file),
of a user-defined
.
.
file
the function proto-
.
header
file in
the implementation
file.
#include "My_Functions h" .
/********************************************************/ */ Complete definitions of the functions listed in */ My_Functions .h /••••a**************************************************/
/* /*
When file
the header file
and source
file
are created for the library, the source
needs to be compiled so that you have an object code
file
for
it.
This
is
done
Organizing and Maintaining Complex Programs
11.6
by using the -c' compiler option on the library source code *
file,
as
OOi
shown
be-
low:
localhost> gcc -c My_Functions
.c
After this step
is
complete, you can use the function library by loading the
object file for the library (and including the header
source
file
of course)
when you compile
file in
the
new program
a program which uses the
library:
localhost> gcc -o My_Prog My_Functions.o My_Prog.c
11.6 Organizing
and Maintaining Complex
Programs The student has been introduced to basic programming and programming skills with the C programming language. These skills will only be useful as a stepping stone to improving your skills, and will only provide you with enough knowledge to read and experiment further on your own. The programs you have seen and written in this part of the text are single file programs, meaning that all of the source
code resides
libraries are included in that file.
system and user-defined
in
one
file in
the operating system,
and
all
Most application programs make use of many
libraries,
source
files,
and object
files.
When develop-
ing large programs, you will frequently find yourself needing to edit one seg-
ment of
the overall program. There
and link
all
is no need for you to manually recompile which depend of the programs on the modified module(s). A utility
make
allows you to define, beforehand, how all of the components of program fit together, and will then determine, when make is invoked, which files have to be recompiled and compile them. This way the programmer
called the
is
freed from having to think carefully about what program segments must be
makes certain that the recompilation is done correctly. This you to make and show you how to create a simple makefile. After that you should look in the reference section for more information on make, what it can do for you (which goes beyond compilation and recompiled, and
it
section will introduce
simple tasks), and 11.6.1
how
to use
The Makefile
make organizes the tasks gram.
it.
Some
that
need
to
be performed to maintain a complex pro-
tasks involve changes to source code and recompilation. Others
involve installing
new
executables in the proper directories. Others involve
building archives or installing the program from archives. construction and installation of online
man pages
Still
others require
for the application,
make
388
Chapter
11
Programming Tools
takes as an argument a
what actions need
to
called a makefile,
file,
"Makefile" or "makefile."
A
which
is
typically
named
makefile identifies each organizational task by
be performed, and by the dependencies between actions
and the components of the program.
A dependency describes executable program has
When you modify
the source
recompile the source
a relationship
at least
file
file, in
that object file is
file
file,
files.
and
order to get the
and produce a new object
cutable depends on the object file. If
between
one source
and the object
one object
file.
new
executable you must
file.
We
file
needed by another program
For example, every
at least
say that the exe-
depends on the source
in order to
compile, then
you can begin to see that the executable file may depend on many source files and object files whose dependencies are sequentially and hierarchically defined. When you create a makefile, you define the dependencies for the different components of your program and its associated organizational tasks. When you want to perform any of these tasks, you simply invoke the makefile with an argument which is associated with the task you want performed. This procedure is illustrated below for six different organizational tasks. % % % % % %
make make make make make make
program clean man install archive
In this example, the
project and,
if
make
utility will,
by
default, use the makefile for the
given an argument, such as 'program,' 'clean,' 'man,' 'in-
stall,' or 'archive,' the associated actions.
will search for that task in the makefile
When
no argument
is
provided,
make
and perform
generally de-
faults to the first task in the makefile.
11.6.2 Constructing a Makefile
Consider the construction of a simple makefile tasks
to
perform the organizational
mentioned above for a simple program. The makefile has three
section types, as illustrated below: •
Macros
•
Dependencies
•
Actions
distinct
11.6
Figure 11.7:
A template
Organizing and Maintaining Complex Programs
makefile
=
compiler_version program_executable_name primary_source_code list_of_objectf ile_dependencies list_of_libraries_used list_of_compiler_options location_to_put_executable location_to_put_manpage
=
${ PROG}. tar
CC
=
PROG SRC OBJS LIBS CFLAGS BINDIR MANLIB ARCHIVE
=
= = =
= =
Oo9
# PROGRAM program: ${OBJS} ${LIBS} ${CC} -o ${PROG} ${CFLAGS} ${OBJS} ${LIBS} ${SRC}
#OBJECTS objects: ${LIBS} ${CC} -c ${LIBS} #CLEAN clean:
/bin/rm -f core ${OBJS} #MAN man: cp ${PROG}
1
$
{MANLIB}
# INSTALL
${OBJS} ${LIBS} /bin/uncompress $ {ARCHIVE} tar xvf $ {ARCHIVE} ${CC} -o ${PROG} ${CFLAGS} ${OBJS} ${LIBS} ${SRC} mv ${PROG} $ {BINDIR} cp -r ${LIB} ${MANLIB} cp ${PR0G}.1 $ {MANLIB}
install:
|
# ARCHIVE archive /bin/tar cvf
$
{ARCHIVE}
The
/bin/compress
$
lines labeled item (1) are called
for the makefile.
©
{ARCHIVE}
macros, and represent
Macros can be referenced anywhere
in the
local variables
makefile by pre-
ceding them with a dollar sign ($), as with shell variables, and embedding the macro name in either a set of curly braces ( { } )or a set of parentheses ( ). (
The use of macros makes
the makefile easier to read
example, macros have been defined as noted as necessary
at
(item
and reduces typing. In 1),
by the appropriate items when referenced
expression or an action expression. Item (2)
is
a
)
this
and would be replaced in either a
comment, which
dependency
is
ignored by
o90
Chapter 11 Programming Tools
make, and follows (#)
is
used
it
then nothing after
Item (3) identifier,
is
on the
delimits the target from is
on the
make
has three components: (1) a target
dependencies, and (3) a
The
on the dependencies. The target to
It
of the colon, (2) a colon, which separates/
left side
its
sign line,
by make.
relation.
right side of the colon.
argument
If a
will be interpreted
it
dependency
a
is
which
comments. Anytime a pound pound sign is seen anywhere on a
the convention of shell
denotes a comment.
means
relation
identifier is
list
of dependencies, which
dependent what the programmer uses as an that the target is
during invocation.
Items (4 to 10) define the actions taken for various tasks associated with various targets.
Any
The
action
must begin with
use of tabs after column
but the tab in action.
As
column
a
column 1. by make,
character in
are ignored (except for readability)
make
used by
is
1
a result, any
1
to
determine that what follows
number of lines can follow
the
dependency
an
is
relation
and
be counted as associated actions as long as they begin with a tab character. Item (4) is the action to
cies
perform when code
between the various objects and
is
modified, and defines the dependen-
recompile
libraries that are necessary to
the program. In this example, only the template has
been shown; however,
in
real example, each object file would have its own dependency relation. Some tasks do not have source/binary code dependencies, such as the "clean" task, c 1 ean removes any core files which are leftover from code de-
any
velopment, and
is
simply a
UNIX
script.
Item (5) defines the dependencies and action necessary to compile the braries to produce the object files
need for program compilation. Item
action associated with placing the
quires that the
macro
manpage
MANLIB
for
which the
library
(item 6), the source code
is
is
and
re-
be defined.
Items (7 to 10) are the actions to perform stallation, in
the
(6)
in the appropriate location,
li-
is
all
the tasks associated with in-
unarchived using
uncompress
compiled as before, the executable
is
tar
and
placed
in the
appropriate directory (item 7), the appropriate libraries are placed in the correct directory (item 8), and the
man page
is
moved
(item
9).
Item (11) defines actions to perform to archive the
program. The archival requires a name of the directory
gram
files
can be found, and then the directory
is
files
in
associated with the
which
all
of the pro-
archived using
tar and
compress. Let us
now
for the Balls
compiling that ments. •
consider a more realistic example, by converting the program
and Boxes problem so library,
that
it
has a source code library, separately
and writing a makefile
The following items describe Create the header
file for
to organize the
the procedure
the Balls
we
will use.
and Boxes functions
program seg-
•
Create the function library for the Balls and Boxes program
•
Modify
•
Create the makefile
•
Test the makefile
the original
BallsAndBoxes.c source code file
for the
functions except main into
anew
associated header
be called
file will
A sample header file
BandBF
for
'library.
.
file
program
Recalling the Balls and Boxes problem,
Figure 11.8:
391
Organizing and Maintaining Complex Programs
11.6
1 '
we
will begin
We will call
"BandBF
.
it
by moving
"BandBF
.
all
c".
h":
c
/+********•****************************•***************************/ ^*************** Balls and Boxes Function Library *****************/ *****************/ /*************** /it************************* header file ***************************/ /*****************************************************•***•********/
*/ PrintDirs Prints instructions to user /**************+***********************************************•***/
/*
:
extern void PrintDirs (void) /*
GetDims
:
Mj
;
Reads box and ball dimensions variables
*/
extern void GetDims (double *blenp, double *bwidp, double *bdepp, double *bradp)
/************•************•****•*******************************••**/ */ Calculates object volume /********************•*************************•****•**************/
/* CalcVols:
extern void CalcVols (double blen, double bwid, double bdep, double brad, double *boxvolp, double *ballvolp, double *actvolp, int *numvlengthp, int *numvwidthp, int *numvheightp)
/*******************•******•***************************************/ */ CompVols: Compares object volumes /••it***************************************************************/
/*
extern void CompVols (double boxvol double ballvol, double actvol, int *numvolsp, int *numballvolsp int *numactsp, int numvlength, int numvwidth, int numvheight) ,
/
/*
PrintNums: Prints output variables
*/
extern void PrintNums (int numvols, int numballvols,
1.
This
is
not actually a library, since
we
will
move
all
the functions for the
Normally, a library would consist of those functions which have a tions
which operate on a
common
data type.
int numacts)
common
program
into this
file.
function, or the func-
the
The
392
Chapter 11 Programming Tools
Each of the function prototypes as noted at item (1), so that
it
is
declared as
will be available
The associated BandBF c implementation .
Figure
1 1 .9:
Implementation
file
BandBF c .
for
'extern'
in the
header
file,
by functions defined elsewhere.
file is partially
shown
new BallsAndBoxes
.
in Fig.
1 1
.9.
c.
/A*****************************************************************/ /*************** Balls and Boxes Function Library *****************/ /*************** *****************/ /*********************** source code file *************************/ /****************•****************************•********************/ #include #include #include "BandBF. h"
Mj
#define PI 3.14159 /*
*/
Normal Function Definitions Here
Note ies
that the
header
used by the functions
any constants (item 2) is
file is
included
at
in this library.
that are
item
It is
(1),
along with the other
librar-
also important to locally define
used by the functions
in the library.
often unnecessary to include/define these items in the
As
such,
it
main program seg-
ment. In this example, PI has been defined for the use of the functions for the Balls and Boxes problem, but these functions comprise the entire procedural component of the program (outside of main), so this may not be indicative of all
programs.
Next we ticular case,
In general,
will
modify the original Balls and Boxes source file. In this paronly included in the program the main function segment.
we have
however; we might have many segments with many functions
in
each. In this particular example, once the function prototypes are replaced by the inclusion of the header
file, little is left in
main
(as
shown
in Fig. 11.10).
Organizing and Maintaining Complex Programs
11.6
393
Figure 11.10: Main program modifications for Ball sAndBoxes.c :*****•******:
/*- ***********
BallsAndBoxes
/* FILE: /* /* AUTHOR /* DATE: /* FUNCTION: /* /* /* APPROACH /* /* /* FUNCTIONS /* /* /* /* COMMENTS /
*/ */ */ */ */ */ */ */ */ */ */ */ */ */ */
.
J.B. Hodges August, 1993 Reads information about boxes and balls and returns information about how many balls will fit into the box. Uses stdio.h function library. Uses the algorithms for box volume and sphere volume. GetDims reads the object dimensions PrintNums displays the number of balls CalcVols: calculates object volume CompVols: compares object volumes :
:
Demonstrates modular program design
it**************- ******: ********************:
************* ******/
#include #include # include "BandBF.h"
®
#define PI 3.14159 int
main (void) {
double blength, bwidth, bheight, ballrad, boxvol, ballvol, actvol; int numvols, numballvols, numacts, numvlength, numvwidth, numvheight;
PrintDirs GetDims (&blength, &bwidth, &bheight, iballrad) CalcVols (blength, bwidth, bheight, ballrad, &boxvol, &ballvol, &actvol, &numvlength, &numvwidth, &numvheight CompVols (boxvol ballvol, actvol, inumvols, knumballvols, toumacts, numvlength, numvwidth, numvheight); PrintNums (numvols, numballvols, numacts); (
)
)
,
return
(0)
The
final task is to create the
makefile. In this case,
use the names of the
gram
is
complete,
we
files
we
will
macros which
name
we have
will call
it
the makefile
will be
used
in the
sample
"Makefile," and we
generated for this example.
When
will
the pro-
"BAB," for Balls and Boxes (item
1
in
The only personal library used in this example is the "BandBF. c" source code file, so it will become the single component of the $ {LIBS } mac-
Fig.
1
1.10).
394
Chapter 11 Programming Tools
ro (item 2). Likewise, the result of compiling
"BandBF.o," which
is
We will also include the main The Figure 11.11:
result is
Makefile for
= BAB PROG = BandBF.o OBJS = BallsAndBoxes SRC = BandBF. LIBS = cc CC CFLAGS = BINDIR = -/bin MANLIB = -/lib ARCHIVE = ${PROG}.tar
"BandBF
.
c"
is
the object file
component in the ${objs} macro. function file in this component (also at item 2).
the only object
shown below:
new BallsAndBoxes
.
c
program (T\
^
(2)
.c
# PROGRAM program: ${0BJS} ${LIBS} ${CC} -c ${LIBS} ${CC} -o ${PROG} $ {CFLAGS} ${OBJS} ${SRC}
#
rest of makefile is the same as the template
Note modified
that the original template, for this particular at all!
this particular
First
we
The
definition of the
program.
example, has not been
macros takes care of the idiosyncrasies of
Now let's test the file to see if
will recompile an executable as
shown
at
it
lives
up
to
its
promise.
item (1) below. In this case,
Mj
localhost> make cc -o BAB BandBF.o BallsAndBoxes localhost> Is *.o
.
BandBF localhost> make clean /bin/rm -f core *.o localhost> !ls .
Is
*
.
[2j
o
No match.
localhost> make cc -c BandBF cc -c BandBF cc -o BAB BandBF.o BallsAndBoxes .
.
.
localhost> make archive /usr/ucb/compress BAB. tar /bin/tar cvf BAB. tar humpback [127] Is BAB*
(Aj
|
BAB*
BAB. tar.
we
already had a copy of
"BandBF
.
o" from a previous
tost,
so only the over-
Recommended Reading
11.7
all
compilation was performed. As proof that
sary components, the (at
we
'clean' option
then remove the object
that
we implemented
will recompile the neces-
file for the
item
(at
2).
function library using
Then we re-make
the file
item 3) and note that both passes (the library compilation and the final com-
pilation) are performed. Finally (item 4),
which tars and compresses
11.7
make
395
we
the source
execute the
code
'archive'
option,
tree for the project.
Recommended Reading Learning
to
Program
in
C
Kernighan, B. and Ritchie, K. [1991]. The tice Hall,
Hanly,
J.
Englewood
and Koffman,
E.
Program Design Oram, A. and
Cliffs,
C Programming
Language, Pren-
NJ.
and Friedman, F. [1993]. Problem Solving and
in C,
Addison Wesley, Reading,
MA.
Talbott, S. [1991]. Practical C, O'Reilly Associates, Seba-
stapol,
CA.
C Programming and UNIX Advanced C Programming Kernighan, B. and Pike, R. [1984]. The Prentice Hall,
Rochkind, Marc
J.
glewood
Englewood
[1985]. Cliffs,
UNIX Programming
Environment,
Cliffs, NJ.
Advanced UNIX Programming,
Prentice Hall, En-
NJ.
Make Oram, A. and
Managing CA.
Talbott, S. [1991].
sociates, Sebastapol,
Projects with Make, O'Reilly As-
This final part of the text combines the concepts and
utilities in
UNIX with those
in the
C
program-
ming language. A programming language can be extremely useful for manipulating UNIX files, commands, arguments, and responses. Each command shell has its own programming language. and a program written for a particular shell is generally called a shell script. Unlike an executable C program, an executable shell script is not a binary file. The shell command interpreter reads each line of the program and evaluates it. This part of the course introduces you to the tools you will need for shell programming in the C Shell and the Bourne Shell. You are not expected to become fluent in writing shell scripts, nor are you expected to become a good programmer by writing a few simple scripts. However, you are expected to learn how to write a shell program in each environment, which includes the use of general programming functionality applied to UNIX system commands and responses. No aspect of either command shell's programming language is presented in much detail. Generally, a topic is introduced that covers a broad range of programming situations. An example is discussed that illustrates how that topic fits into the UNIX programming environment and how it is used. By the end of each chapter, you should have a few template examples that enable you to construct simple working scripts that exhibit behavior commensurate with the topics leading up
to
and including those introduced
in the chapter.
Regular Expressions
and Scripts
12.1
Introduction and A
shell script
evaluated in a
is
Scope
an executable
UNIX command
comprised of expressions which can be
file
shell
by the
language used to control the evaluation of
programming language. Much
shell
matching
a desired pattern with a
UNIX
UNIX
.
or
utility,
The matching process
command on is
Regular expressions
•
egrep
•
avvk
•
sed
language
is
the
have completed
regular,
are helpful in
the patterns
which succeed the match.
means
for comparing, or
matching patterns.
topics will be presented in this chapter:
•
When you
of shell programming consists of
of potential patterns, and executing a
requires an understanding of regular expressions, be-
cause a regular expression
The following
list
UNIX command interpreter. The command expressions is called a
how
UNIX
to
this chapter,
make
you should be able
a language regular,
why
to tell
in
a
regular expressions
and when they are used. You should be able
examples of different uses of regular expressions
when
to provide
UNIX commands,
and you
egrep. awk. and sed scripts. egrep. awk. and sed are. when they are
should be able to use regular expressions in
You
should be able to describe what
useful,
what
their differences are.
and you should be able
to write simple to
399
400
Chapter 12 Regular Expressions and Scripts
Chl JL
awk and sed scripts. The
moderate found
"UNIX /Chi 2"
in the
examples
illustrated in this chapter
can be
directory and can be executed and modified to
help you learn the material.
Before discussing regular expressions and their uses it is
know
informative to
can create a
that a shell script
UNIX commands
of
need not be complicated
and execute
all
at all.
You
of them by executing the
the shell environment. For example, create a file called
file in
per"
file
programming.
in shell
"LatexAPa-
that contains the following six lines:
latex paperfile; bibtex paperfile; make index paperfile; latex paperfile; dvi2ps paperfile > paperout.ps
You
can
now
execute
all
of these processes by entering the single com-
mand: localhost> /bin/csh LatexAPaper &
where
per
is
"paperfile tex" .
a simple shell script.
mands and
utilities,
All the script does
and
is
is
that the user
debugging output
ample
is
is
that the file,
command
paperfile tex would .
else.
have to
number of commands so
The commands
can perform other tasks. In
exist.
that
you
are executed in the back-
this
example, however, the
displayed to the standard output anyway. This simple ex-
the essence of shell
that has a
name of a latex source file. LatexAPacommands are known UNIX com-
that the
sequentially execute a
can continue doing something
ground so
the
Note
programming.
A shell script can be
sequence that you have a
difficult
a
one
line file
time remembering, or
can be an extremely complex program. Start small and work your
way
it
up.
12.2 Regular Expressions An
expression defines a relationship between operations and the objects they
operate on.
which are
A
regular expression
legal
ular language.
is
one
in
which
the operations are those
on regular languages, and the objects are the words in the regresult of a regular expression is a set of words which would
The
match the original expression, and forms the basis for pattern matching in UNIX. Regular languages form the basis lor all computer programming languages, so their scope extends beyond that of
UNIX. For that
reason, a shallow
discussion of the mathematical basis of regular languages and expressions pre-
cede our discussion of their use
sed. and
egrep
in shell
programming. The
UNIX
are introduced at the end of the chapter.
utilities
You have
awk.
alread)
Regular Expressions
12.2
been using regular expressions
Chl2
in
401
many commands,
so try to view the followyou have seen or can look up. Regular exvi. egrep. and find for pattern matching
ing discussion in terms of examples
pressions are used in utilities like
while searching for strings. regular
Mathematical Background
12.2.1
A
Let
be a
of characters called an alphabet.
finite set
A word
is
a string of
characters from A. For example, "aab." "abc." "ccc." and "babbc" are in the
standard alphabet, because they are
from the standard alphabet. There
is
which has no characters. This word
A i: = L2 = L3 =
{a,
bb, aa,
{e,
ab}
{a,
language
acab} aaa.
is
all
also a special
word called
will be designated with
the
empty word
an "e."
a set or collection of words. For example:
are all languages. Notice that
some languages
are finite (e.g.,
LI and L3. which
have exactly 3 and 2 elements, respectively) and some are infinite
Given
words
and
}
12.2.2
all
comprised of strings of characters
(e.g.,
L2).
Language Construction Operators a collection of languages, there are
from them using the following
ways
to construct other
languages
set operations:
Union (u) Intersection (n)
Difference (-)
Concatenation Star closure
1
.
We
will use
A
=
{
a
,
b
,
c
}
(*)
for set
examples
in this section. In
—upper and lowercase alphabetic punctuation, and math symbols. —except newline includes
all
characters
for the
UNIX,
the character set
characters, digits, the blank character, character.
J
402
Chapter 12 Regular Expressions and Scripts
Union The union of languages with. That
is,
the union
is
is
the
same
as the union of sets that
the combination of
languages. For example, suppose that
all
you
are familiar
non-repeated elements of both
Ll and L2
are defined as follows:
Ll = {urchin, starfish, nudibranch} L2 = {dolphin, otter, starfish}
then the union of
Ll
u
L2 =
Ll and L2
is:
starfish, nudibranch, dolphin,
{urchin,
where the
u
symbol
is
otter}
normally used to denote the union operator.
Intersection Similarly, the intersection of languages in
is
the collection of elements repeated
both languages. For example, suppose that
Ll and L2
are defined as fol-
lows:
Ll = L2 =
{pumpkin, zucchini} {tomato, potato, pumpkin}
then the intersection of
Ll
n
L2 =
Ll and L2
is:
{pumpkin}
Again, the
n is the
standard symbol for the intersection operator.
Difference Given two sets, the difference between the sets is arrived at by taking the members of one set which are not members of the other set. For example, suppose that Ll and L2 are defined as follows: Ll L2
:=
==
{school, pool, dog, {pool, snail, tree,
snail} stone}
then the difference between
Ll
--
L2 =
{school,
dog}
Ll and L2
is:
12.2
and the difference between L2 and LI L2
-
Ll =
{tree,
403
is:
stone}
Notice that -
Regular Expressions
set difference is directional, since
Ll
-
L2
is
different than
L2
Ll.
Concatenation Given two words, we concatenate them by placing them together to make a longer word. For example, the concatenation of "school" and "house" is "schoolhouse." The concatenation of "aab" and "cbbba" is "aabcbbba." Note that the concatenation of any word with "e" is the word itself (e.g., "aab" concatenated with "e" is "aab"). We can concatenate two languages by taking
all
possible combinations of concatenations of words in the
guage with words
in the second.
For example,
if
Ll and L2
first lan-
are defined as fol-
lows:
Ll = L2 =
{school, pool, {house, hall}
dog}
then the concatenation of
Ll and L2
is:
L1L2 = {schoolhouse, poolhouse, doghouse, schoolhall, poolhall, doghall}
The concatenation of two languages L1L2).
is
written like an arithmetic product
(i.e.,
Star Closure
New
languages can be constructed by repeating the concatenation operation
any number of times. This operation
is
called *-closure (star closure). For ex-
ample, a language can be concatenated with
itself:
L2L2 = L22 = {househouse, househall, hallhouse, hallhall}
where the notation L2 2 means
that the
language L2
is
concatenated with
itself
J
404
Chapter 12 Regular Expressions and Scripts
(i.e.,
L2L2L2
=
twice) This can be done three times:
L23 = {househousehouse
househousehall, househallhouse, househallhall, hallhousehouse, hallhousehall, hallhallhouse, hallhallhall}
And
,
so on. For any language L,
guage L, denoted L*, a
member
of)
L*
if
it
let
LO =
e}
{
the union of LO, Ll, L2,
is
then, the *-closure of lan-
,
the concatenation of zero or
is
word is in (i.e., more words from L (The
Basically, a
...
zero condition, LO, pertains to {e}).
We
are
regular
if
now
it
ready for the definition of regular languages.
contains only a finite
number of words or
it
A
language
is
can be constructed
from other regular languages using unions, concatenations, or ^-closures. For example, Ll = L2 =
(aa,
if:
bb}
{c}
are languages then: •
Ll and L2
•
L3
are regular because they are finite
Ll*
=
{e, aa, bb, aaaa, aabb,
=
aaaaaa, aaaabb, regular since
is
it
.
.
bbaa, bbbb,
.
the *-closure of regular language
is
Ll
L4= L2L3 = {c caa, ebb, caaaa, caabb, cbbaa ebbbb, caaaaaa,
•
,
.
is
regular since
L5 = L3
•
u
it is
the concatenation of
L4 =
regular since
L6 =
•
it is
(L2*L1 )*
.
L2 and L3
{e, c, aa, caa, bb, ebb, aaaa, caaaa, aabb, caabb, .
is
.
.
the union of regular languages. is
also a regular language.
nation of the *-closure of
L2 with
It is
the ^-closure of the concate-
Ll. Note in this example, that there
is
an "em-
bedding' of one *-closure within another.
12.2.3 In
Regular Expressions
UNIX,
in
Unix
regular expressions are used by
many
utilities to define/identify
ap-
The basic idea is that a regular expression is used as a compared to a large number of character/word strings. When
propriate sets of words.
pattern which
is
is found to be equivalent to the pattern, then it has •'matched.'" or "patmatched" the expression. The set of strings that matches the pattern is a regular language. For example, in the vi editor. there is a substitute com-
a string tern
1
12.2
mand :
for
making a change
Regular Expressions
The syntax
in a line.
for the substitution
405
is:
s/old_text/new_text
Given the
'
:
s'
command,
the editor searches the current line for a string
matching the "old_text" and replaces
with the "new_text." The it compare against sample strings. Notice that the two strings are separated by a slash ( / ). The slash is called a pattern delimiter. The pattern delimiter need not be a slash, but can often be any special symbol as long as the symbol is not a part of either the original pattern string or the
"old_text"
replacement
is
the pattern to
string.
For example,
if
the line:
car is rad
John'
exists in a file
and the vi
command
:s/ra/:re
is
issued while editing the
file,
then the result will be
car is red
John'
In this
example, the search
command could be
modified as follows without
any affect on the result :s#ra#] re
More
generally, the specification of the
"old_text" can be
a regular ex-
match zero or more occurrences of the single character regular expression that precedes it. Note that this is a different usage than the asterisk as a wildcard. It only matches a single character. The pattern "we*k" matches "wk," "wek," "week," "weeek," etc. Suppose the line of text is: pression. For example, the kleene star (*) can be used to
This wk is the last wek in which to purchase a weeek 's supply.
1.
We
ever,
will use
it
vi and
pattern search to illustrate
some
delimiters and
is
not the search
command
itself.
howdemarked by
applications of regular expressions,
should be remembered that the regular expression
is
the statement portion
406
Chapter 12 Regular Expressions and Scripts
Then
:
the
command:
s/we*k/week/g
will produce:
This week is the last week in which to purchase a week's supply.
The "e" must precede the "*" or it will match the "w" instead. The "g" on the end of the command means make the replacements globally, i.e., everywhere in the current line. Without the "g," the substitution will only be made on the
first string that
special
symbols used
matches the
pattern. Besides the *, there are several other
in regular expressions,
some of which
are tabulated be-
low.
Table 12.1: Symbols used in matching regular expressions []
Any all
characters within the brackets will be matched, e.g. [a-z, alphabetic characters in both lower and upper cases
a-z] matches
Matches any single character
One important cial
symbol
Replace the notion of
*
as
note:
one of
its
Sometimes
the pattern to be
matched contains
a spe-
characters. For example, suppose the line of text
is:
with the star closure in this line.
and you wish
to substitute the
"*" with
"*-closure. The command: '
:s/*/*-closure
will not
work, because the
status, called
command i
* is a special
symbol.
escaping the symbol, precede
it
To remove
its
special
symbol
with a backslash (\). The correct
is:
s/ \ * / \ * -closure
The backslash preceding the next character.
the asterisk
means "ignore
the normal
meaning" of
Regular Expressions
12.2
Regular Expressions
12.2.4 Bracketing In the
UNIX commands ed and grep, there is also the
lection of
symbols as a
and can be used
407
unit.
in place
The reference
of the collection.
ability to refer to a col-
is
then called a tag of the collection
To
tag part of an expression, enclose
The parentheses will have to be escaped, so precede each Once a part of an expression is tagged it may be referred to by number based on its ordinal position: 1 refers to the first tagged expression, 2 refers to the second, and so on. The position should be preceded with a it
within parentheses.
with a backslash,
(
\
).
backslash (\). For example, consider the text in the
taggedtest" I
file
"regular/
below:
feel good.
John loves Mary.
Positioning on the
first line in
vi, the following
command
can be issued:
:s/\(good\)/\l \1 \1/
which produces I
feel good good good.
As another exc imple, by positionin g vi on the
third line
and issuing the
fol-
lowing search and replace command: :s/\ (John\) loves \(Mary\)/\2 loves \i/
replaces
John loves Mary.
with
Mary loves John.
12.2.5 Full
Note
Regular Expressions
that not all regular expressions are possible with the special
above.
It is
not possible to
However, with some
embed one
UNIX utilities,
pressions" are possible.
The
* within another
such as
symbols
such as (ab*c)
awk and egrep,
*.
"full regular ex-
additional special symbols used in full expres-
4Uo
Chapter 12 Regular Expressions and Scripts
sions are.
Table 12.2: Symbols used in (
)
full
regular expressions
Parentheses can be used to group part of an expression; for example, a (dog)
matches
a,
adog, adogdog,
Matches one or more occurrences of the regular expression
Matches any character
A
Matches the
$
Matches
the last character
Matches
either of
+
red ?
or
first
that precedes
it
character on a line
on a
line
two patterns separated by
; |
for example,
red rad matches |
rad
Matches zero or one occurrence of the preceding regular expression
Parentheses have precedence over the other symbols, then enation, and, finally,
12.3
*
etc
*
+
?,
concat-
|
egrep The command egrep is
a pattern matcher like
the use of full regular expressions, allows
permits pattern input from a is
to use the
file.
you
The way
gr ep and f gr ep,
but
it
allows
to specify several patterns,
and
from a
file
to specify pattern input
'-f option. For example: '
egrep -f patfile searchfile will search the
"searchfile"
using patterns in the
"patfile." As another
example:
localhost> egrep -c 'A[0-9]+Q' *.c
will provide a
count (-c option) of the number of lines
contain the pattern "A" followed by any
by a "Q." Each for the
grep
file will
in
each
number of digits
be reported independently. See the
family in Chapter
3,
page 86.
(
[
" .
0-9
c" ]
file
which
+) followed
command
reference
12.4
409
awk
12.4
awk The awk
utility is
an extremely important and useful method for locating and
manipulating information
UNIX commands.
from
highly stylized/ordered data, such as the output
in
Data which
terms of fields and records, so
awk
to scan the lines of a file (or
many
perform an action on those
file.
program on
highly structured
really a field
files)
awk
is
command
line,
often referred to in
awk can
be used
match
match)
(or don't contain a
sent to standard output but can be redi-
There are two formats for awk: the
the
is
matcher,
looking for a particular pattern and
lines that contain a
of the pattern. The output from rected to a
is
is
while the second takes
awk
takes an
first
it
from a
script/
We
file.
ad-
dress the command-line version, because understanding one will be enough to
understand and use the other.
awk script [file-list] The "script" above
refers to an
awk
An awk
script.
script is a
sequence of
pattern/action pairs:
pattern {action} The
pattern
the line.
used
is
and the action
Both patterns and actions are optional.
generic form, then
then
to select lines to analyze,
awk
If a pattern is
selects all lines of the input
file. If
is
"pattern"
is
analyze
to
missing from
an action
awk performs the default action of print, which copies the
output (most often the display). Each
used
is
this
missing,
line to standard
a regular expression en-
closed in slashes (/). Actions can be quite complex, and entire books are de-
voted to
awk
script writing (see Section 14.8).
A large subset of C
can be used as actions. Actions are always enclosed Note: it is
It is
a
good idea
to place the entire script in single
not confused with shell
long listing as input. First redirected to a
file
in curly
commands. Here
let's
look
at the
called "f oo" using
are
quotes
(
'
'
( { )
} ).
so that
some examples which use
long directory
tee.
instructions
braces
listing,
which
I
a
have
410
Chapter 12 Regular Expressions and Scripts
localhost> Is total 361
-
-rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rw-r- — r--rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rw-r-rwxr--xr-x -rwxr- -xr-x -rw-r- --r--rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rw-r- --r— -rwxr- -xr-x -rwxr- -xr-x
jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges
—r—
1
1 1 1
1 1 1
1 1 1
1
1 1 1
1 1 1
1 1 1
1 1 1
1
1 1
|
tee foo
Note
5731 15826 21794 2303 40798 689 12723 14287 15017 23280 7693 15947 5369 2233 1685 5521
Mar Mar Jan Jan Mar Mar Aug Aug May May May May May
Oct Oct Sep Jan 5437 Apr 6326 Jun 12726 Aug 14287 Aug 7224 Jun 63235 Jun 12722 Aug 14287 Feb 30639 Jun
that
tee
23 21 6 6
23 23 12 12 16
16 16 16 16 19 19 11 29 30 21 12 12
21 4
12 1
21
1989 1989 1990 1990 1989 1989 12:15 12:15 1990 1990 1990 1990 1990 1990 1990 1990 18:15 1989 1991 12:13 12:15 1991 1990 12:10 1990 1991
reserved a
rectory before completing the listing is
always expect
awk
.
.
.
.
.
.
.
foo jack. sty
©
mitthesis sty sf suletter sty sf suseal .ps sf suthesis sty texinfo. tex uclaletter sty ucseal .ps ucthesis sty .
.
.
.
.
name and location (at item 1) in the current dicommand. Also recall that the long directory
extremely structured. Every line follows the same format, so you can to find similar information in a similar location. This is the kind
of information which ple
aaai sty alpha4 .bst apalike.bst apalike. sty cite .bst cite. sty csuletter sty csuseal .ps diss .bst diss doc diss sty dissl2 .doc dissl2 sty disstitle.doc disstitle sty doublespace sty
script
on
is
ideal for
this file.
awk,
as
you
will see.
Now we can issue a sim-
12.4
localhost> awk total 361 -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rw-r- __ r __ -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rw-r- --r--rwxr- -xr-x -rwxr- -xr-x -rw-r- r~ -rwxr- -xr-x -rwxr- -xr-x -rwxr- -xr-x -rw-r- --r--rwxr- -xr-x -rwxr- -xr-x
—
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
'
{print}
'
411
foo
Mar Mar Jan Jan Mar 689 Mar 12723 Aug 14287 Aug 15017 May23280 May 7693 May 15947 May 5369 May 2233 Oct
jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges jhodges
awk
5731 15826 21794 2303 40798
23
21 6 6
23 23 12 12
16 16 16 16 16
19 1685 Oct 19 5521 Sep 11
5437 6326 12726 14287 7224 63235 12722 14287 30639
Jan Apr Jun Aug Aug Jun Jun Aug Feb Jun
29 30 21 12 12 21 4
12 1
21
aaai sty alpha4 bst apalike .bst apalike sty cite .bst cite sty csuletter sty csuseal ps diss .bst diss doc diss sty dissl2 .doc dissl2 sty disstitle doc disstitle sty doublespace sty
1989 1989 1990 1990 1989 1989 12:15 12:15 1990 1990 1990 1990 1990 1990 1990 1990 18:15 1989 1991 12:13 12:15 1991 1990 12:10 1990 1991
.
.
.
.
.
.
.
.
.
.
.
.
foo jack. sty
mitthesis sty sfsuletter.sty sf suseal .ps sf suthesis sty texinf o tex uclaletter.sty ucseal .ps ucthesis sty .
.
.
.
This ultra simple script has no pattern and only the simplest of actions, reads and prints the entire script
file,
one
it
The next most simple awk simple pattern match test and an
line at a time.
does almost the same thing, but has a
action to print the result
localhost> awk foo /cite. bst/ {print} -rwxr-xr-x 1 jhodges 40798 Mar 23 1989 cite. bst '
'
The pattern matches all lines in the file "foo" which contain the string "cite. bst" (note the use of slash delimiters, which are used to bound a string), and sends these lines to standard output. Because the contents of foo is
a directory listing, and since there
name, the 12.4.1
The
script output is
awk and
real strength
is
only one item
comprised of a single
in this directory
with that
line.
Field Variables
of awk
is
how it works
with structured data.
A data file called
J
412
Chapter 12 Regular Expressions and Script*
shown below. Each of the columns is separated by empty space, so easy to look at and compare each column, awk has a facility for looking at these columns, or fields, by identifying the column separator and the column number the item is associated with. As a default, awk denotes fields as items separated by spaces. Each column, or field, is associated with a number,
"data"
it is
and
is
referenced with a leading dollar sign ($). For example, wherever the no-
tation
Below data: 237 422 371 345 323
$3
is
seen in the
awk
script
below, the third field
-"
is
being referenced.
rows of four numbers, called
are the contents of a file that has five
7
— —
37 embedded in field #1 of line #1 22 embedded in field #1 of line #2 37 embedded in field #1 of line #3
8
-
37
19 28 12
85 48 16 8 8
67
89 37
19
is
fairly
Below
is
an
awk
script
is
field
which matches '37' and
#3 of
/37/ {print $1} 1221 {print $3}
'
'
#5
'22.' If a '37'
where in a row, then the script will print the first field in found in a row, then the script will print the third field in
localhost> awk
line
is
found any-
that row. If a '22'
data
237
-"
field #1 of line
16
-
field
#3 of
line
371
field #1 of line
323
field
In the if the
example above, the
field
where the match occurs
UNIX
example using
then both actions will be executed. fields in the action part of
localhost> awk /.ps/ {print $4} /.doc/ {print $4, $8} 23280 diss. doc 14287 15947 dissl2.doc 223 3disstitle.doc 14287 14287 '
The all
I.
by
Note
awk
that the
'.'
is
as a period.
# 1 of
is
line
lines
input
file
for this
which contain
"
example
.ps."
not a special character unless
1
it
is
Now
both a
foo
again "foo." The
escaped. Because
if
let's return to
first
pattern matches
and the second pattern matches
is
#2 #3 #5
-1" example.
our "Is
'
#1
not specified, so
pattern occurs in any field, the action will be executed. In addition,
patterns are matched,
is
that row:
it
is
not.
it
is
all lines
which
interpreted
12.4
contain
" .
doc."
If the line
awk
matches either pattern, then the action
says to print the fourth field in the
line. If the test is true for
" .
413
print $4 awk
doc," then
As a result, awk is extremely useful for manipulatof UNIX commands, because they are always very structured.
also prints the eighth field.
ing the results
The use of fields
in
awk scripts can also be applied to files with different field awk has an optional argument, '-Fc, that allows the
delimiters (separators),
user to define which character "c" will be treated as the field separator.
example, you are trying
to
analyze a
"test," whose
file,
If,
for
fields are separated
by colons, as follows:
localhost> cat test bob: ted:mary :alice :baz :bar :dog: fish: snail cloud :
then your
localhost> awk -F: baz bar
'
awk
script
could identify each field as follows:
/mary/ {print $5} /ted/{print $6}
test
'
field separator In this example,
which happens,
when awk
in this case, to
"mary,"
finds a
prints out the sixth field, which, in this case,
they are on the same line in the
12.4.2
values of fields.
"bar."
is
'
awk make
In this
by adding a logical
finds a "ted,"
it
decisions about what to do based on the
to
work.
easily have tied the
either"
foo
'
example, the two vertical bars
need not be capitalized
we could
test to the conditional:
/.doc/ {print $4, $8}
ator, so the conditional is true if
'F*
it
doesn't matter that
A logical test may take the form of conjunction, a disjunction,
tests together
localhost> awk /.ps/ 14287 csuseal.ps 23280 diss. doc 15947 dissl2.doc 2233 disstitle.doc 14287 sfsuseal.ps 14287 ucseal.ps
The
It
file.
or a negation. Returning to the "f oo" example,
1.
prints out the fifth field,
awk and Logical Tests
Often one will want to have
two
it
be "baz." Likewise, when
.
(
) |
represent the logical
|
ps"
or
"
.doc"
is
OR oper-
on aline of "foo."
414
Chapter 12 Regular Expressions and Scripts
have changed the action
was to show how The other logical operator you might use is the AND, or conjunction, operator, which is the double-ampersand (&&). Notice that
I
slightly, but the intent
the logical operator works.
Formatted Output
12.4.3
awk
allows the programmer to
extension
is
make use of commands
printf (see Section 8.3.4,
page 278).
We
other than print.
C programming
to control the format of output using the
One
function
can apply the same example using
printf, as follows:
localhost> awk The The The The The The
file file file file file file
size size size size size size
is is is is is is
/.doc/ {printf "The file size is: %d, the file name is %15s", $4, the file name is csuseal.ps the file name isdiss.doc the file name isdissl2.doc the file name is disstitle.doc the file name is sfsuseal.ps the file name is ucseal.ps
/.ps/
'
:
:
:
|
14287 23280 15947 2233, 14287 14287
The
printf
and the
size of the space allocated for printing the field value.
means
and by not specifying
size the entire
its
to print a string of 15 characters,
In this case, is,
more
fo
"footmp"}
localhost> more footmp The file size is: 14287, the file name is csuseal.ps The file size is: 23280, the file name isdiss.doc The file size is: 15947, the file name isdissl2.doc The file size is: 2233, the file name is disstitle.doc The file size is: 14287, the file name is sfsuseal.ps The file size is: 14287, the file name is ucseal.ps
'
foo
awk
and
awk
12.4
The component of the awk
script associated with the redirection has
bolded. Notice that the redirection
not create the long directory listing
is
quoted. Finally, what
first,
directly into the
it
been
performed within the action part of the
is
awk statement and that the filename ed, instead, to pipe
415
we would rather
-1 command, but want-
with the Is
awk
if
This can also be done easily,
script.
as follows:
localhost> Is -1
awk '/.ps/
|
/.doc/ {printf "The file size is: %d, the file name is %15s", $4, $8 >"footmpl"} '
localhost> more footmpl The file size is: 14287, The file size is: 23280, The file size is: 15947, The file size is: 2233, The file size is: 14287, The file size is: 14287,
the file name is csuseal.ps the file name isdiss.doc the file name isdissl2.doc the file name is disstitle.doc the file name is sfsuseal.ps the file name is ucseal.ps
Again, the component of the
awk
doesn't
files,
so you can perform the
if
you
know
awk script associated
the difference
same
are sending the output to a
script to
between a single tasks
temp
on many file,
bolded.
you have
to
list
of
Of course, modify the
awk Test
Field variables can be used in the test part of an in the action part
of the
long directory
this instance,
We
files sequentially.
as above,
is
input and a
append, rather than write, using >>, or some other creative method.
12.4.5 Field Variables in the
4. In a
with the pipe
file as
use the
script. In the
fourth field represents the size of the
listing, the
we want to
test called is
flag
a
any
awk script as well as they are we will look at field number
next example,
file
member
file.
In
with a size between 20 and 100 kilobytes.
of.
which
is
the tilde
symbol
(~).
awk '$4 - [2-9] [0-9] [0-9] [0-9] [0-9] {printf "The file size is: %d, the file name is %15s", $4, $8>"tmplrg"} localhost> more tmplrg The file size is: 21794, the file name is apalike.bst The file size is: 40798, the file name cite.bst The file size is: 23280, the file name diss. doc The file size is: 63235, the file name is texinfo.tex The file size is: 30639, the file name is ucthesis.sty localhost> Is -1
I
'
In this (size) field
matches the pattern
between
and
selected.
The
9,
or a
action
is
file
Is
-1
in which the fourth between 2 and 9 followed by four digits wholse size is between 20 and 100 Kilobytes) are
example, those lines from the
listing
(a digit
to print the
same
size
and name as before. The same ex-
4 ID
Chapter 12 Regular Expressions and Scripts
ample can also be performed with
localhost> Is -1
inequalities, as
shown below.
awk '$4 >= 20000 && $4 < 100000 {printf "The file size is: %d, the file name %15s", $4, $8>"tmplrg"} localhost> more tmplrg The file size is: 21794, the file name is apalike.bst The file size is: 40798, the file name cite.bst The file size is: 23280, the file name diss. doc The file size is: 63235, the file name is texinfo.tex The file size is: 30639, the file name is ucthesis.sty |
is
'
In this example,
and
we used
12.4.6 Shell Variables
As a final
own
AND operator (&&).
and awk
illustration of the strength of
ables from outside an its
both inequalities for greater than or equal to (>=)
less than ( echo awktestsv set filenamel = $1 set filename2 = $2 awk / "$filename2" '
n
'
4
/
{print $4,
$f ilenamel
$1
ii
evaluate in shell parse
unparse
unparse
parse
$f ilenamel has the value "foo," and $f ilename2 has "diss bst," then this print produces the following: If
the value
.
localhost> awktestsv foo diss. bst 15017 diss. bst
where the
first
column of
the output
is
the file si/e
and the second
is
the file
417
sed
12.5
name, as expected. This example can be confusing, because, as a beginner, you
do not
know how
really
double quote
is for.
the single quote
The
single quote
and the second means script,
The balanced quotes
argument and return
first
quote
you can
start
its
command
will be
indicate pars-
tells to start
to stop parsing (zmparse)
and go back
to
and stop quoting any-
depending on what you are trying
tells the shell's
awk what
used to identify to will not.
interpreting in the shell environment. So,
where within the double quote
being used by awk, nor what the
parsed (interpreted) by awk. The
the script to be
ing the script,
is
what
interpreted as part of the script and
is
to accomplish.
The
interpreter to evaluate the associated
value. Thus, the effect of the single and then double
quotes surrounding the shell variable
$f ilename2
and then
to interpret the variable as a field,
is
to tell
awk
to evaluate the variable
to not try
and return
the value to this location in the script.
Sometimes you find yourself in a position of wondering if you could concatenate a field from the awk test with the value of a shell variable. In such a case, you can use a similar tactic as above. Consider a case where you want to concatenate the
name of
a file with the value of the
HOME
shell variable.
This
can be done as follows:
localhost> awk '/.tex/ {print /usr/f 1/ jhodges/texinfo tex
"
'/"$8}
"$HOME"
'
'
foo
.
Now awk is
is
being told to print a string, but just after the string argument
started, the script is unparsed.
by the
shell,
Then $HOME
is
ignored by
awk and evaluated
before starting the script parsing again. Finally, the string argu-
ment is ended with a /' which is followed by the name of the file in field 8. The interesting thing going on is the string conversion in awk. Note that the '
last slash is
located inside the string double quotes, just prior to concatenating
with the field variable. Also note that spacing has been provided, around "
12.5
$HOME."
for clarity, but should not be used to
produce the desired output.
sed sed
is
called a
stream editor and
patterns and manipulating them. erate terns.
on
is
It is
an important and useful tool different than
fields but. rather, allows editing
What sed does
is
to take a list
awk because
commands
to
it
in selecting
doesn't op-
be used on general pat-
of patterns line by
line,
much
as does
awk, and executes a set of actions on them. The editing actions which sed uses are the same as the global editing commands in vi. They work with regular expressions and allow offline
(i.e..
not using an editor) editing of
will edit lines of a file according to a specified script.
the results to the standard output,
sed
By
default,
has two formats, like awk.
files,
sed The
sed
sends first is
4lO
Chapter 12 Regular Expressions and Scripts
to take the script
on the command
line as follows.
sed script [file-list] where the editing commands in the script are applied to all lines in the each of "file- list." The second format uses an option '-f to lo-
the files in the
cate the script in a
file:
sed -f scriptfile [file-list] in
which the editing commands are stored in the file "scriptfile." A or "scriptfile" contains one or more lines in the following
"script" form:
[address
[,
address]] instruction {argument -list} The "address" may be line numbers in the file. In general, any global command you would use in vi to find a line can be used to specify an address in sed. Addresses in sed are optional. If only one is specified, then that line is edited. If two addresses are specified (separated by a comma), then that range of lines is edited. The address may also be regular expressions, in which case those lines which match the pattern are selected. If addresses are not specified, then sed edits all lines. As a simple example using sed, consider the following
file:
localhost> more test When I was a boy we had a dog named Greta. I have no idea how we were supposed to spell that dog's name, I was really quite young. Dogonit, you'd think I would have learned something about dogs by now. I think I've owned a dog almost my entire adult life.
__^_
t_
Suppose you want
to replace all occurrences of a particular string, like
"dog," regardless of case, with "Llama," for a specified range of file.
You could do
lines in a
so as follows:
localhost> sed '1,$ s/ [dD] [oO] [gG] /Llama/g' test When I was a boy we had a Llama named Greta I have no idea how we were supposed to spell that Llama's name, I was really quite young. Llamaonit, you'd think I would have learned something about Llamas by now. I think I've owned a Llama almost my entire adult life. .
Note
that the script, as in
ponent of the script
is
awk.
is
enclosed
in single quotes.
The
used to identify the pertinent range of lines
first
com-
in the file. In
sed
12.5
this case, the lines
the entire file
is
from the
analyzed.
first
(1) to the last ($) inclusive are included, so
The second component of the
perform. In this case the global search and replace also the 'g' at the is
end which
tells
just as easy to input the script
419
sed
from a
script is the action to
command
is
used. Notice
to replace all occurrences
on a
line. It
file.
localhost> sed -f sedfile *.cl
where the sample "sedfile" contains the
line
shown below:
localhost> cat sedfile S/"\-/%;/
Note
first that
the file-list '
A
'
is all files
with a '.cl' extension. The
sed
each semicolon at the beginning of a line with a percent-sign (%). As an illustration, consider the sample lisp file bescript uses the instruction
to replace
low:
localhost> more shortlispf ile2 .cl 1st is nil, return 1st 1st is single atom, return 1st car 1st is atom, return car 1st cons ed to flatten of cdr 1st car 1st is list, return flatten car 1st appended to flatten cdr 1st '
(defun fltn (1st) (cond start conditional ((null 1st) 1st) end conditional (atom (car 1st) (cons (car 1st) (fltn (cdr 1st)))) (t (append (fltn (car 1st)) (fltn (cdr 1st)))))) ;
;
(
(defun bob
(x)
(+
x
3)
We can use the template le2 cl" .
as follows.
and the
file,
or
list
of
files,
on
"shortlispf i-
420
Chapter 12 Regular Expressions and Scripts
localhost> sed -f sedfile *.cl 1st is nil, return 1st 1st is single atom, return 1st car 1st is atom, return car 1st cons'ed to flatten of cdr 1st car 1st is list, return flatten car 1st appended to flatten cdr 1st (defun fltn (1st) (cond start conditional ((null 1st) 1st) end conditional (atom (car 1st) (cons (car 1st) (fltn (cdr 1st)))) (t (append (fltn (car 1st)) (fltn (cdr 1st)))))) ;
;
(
(defun bob
(x)
(+
x
3)
Note
that
because the
semicolons
test
only mentioned the
12.5.1 Selection of Line
first
Ranges
sed example we
In the first
would not be affected by column of every line.
in other locations
in
sed
selected the entire file for output. Often
working with the output of UNIX commands you of the total
mand
number of lines
will display a
you want
to
forms
localhost> ps uxa
|
command
heading before
awk you
that function for
need
response. For example, a
listing the
when
be working with a subset
ps com-
processes and their attributes. If to the
to eliminate that first line.
ps command,
The
script
then
below per-
you using sed.
sed -n '2,$ p'
In the
in the
will
perform some analysis on the response
in order to use
this script
|
example above,
awk '{print $1, $10}'
a
number of new tools are introduced. Most imporwhich means "do not print anything which does
tantly, the '-n' option is used,
not meet the criterion of the match". Then, in the script, anything
turned must be explicitly printed. This
of the script process
is
name
why
the 'p'
is
we want
re-
used. Finally, the result
piped into awk, which prints the process owner userid and the (fields 1
and 10). Below
localhost> ps uxa sed -n '2,$ p' jhodges ps jorge rtin geisman /usr/ local /lib/mocka/sys/Mc jhodges azhao pine-exe vcsc3081 vi jhodges sed |
is
|
is
a sample run of this script.
awk '{print $1, $10}'
12.5
12.5.2
Regular Expressions
sed will The
script
'
421
sed Tests
allow the use of regular expressions in the selection of string patterns.
below looks for a number use of bracket
illustrates the
.ocalhoso sed -n
in
sed
[0-9]
(
[
]
in
)
in the first
sed
column of
the input file
and
scripts.
p'
12.5.3 Shell Variables in
As with awk.
it
will often
sed
Scripts
be important to use a shell variable to select a pattern
sed. The script below shows that, to perform this kind of task, same mechanism that was used in awk also works for sed. Particularly,
for analysis in
the
one doesn't quote that portion of the
and then the quote
is
script
used to identify the shell variable,
again begun, or restarted. This example selects patterns
which include the value of the
shell variable
"third".
localhost> sed -n $third' p'
Unlike awk.
sed does
not
work with
field variables, so there is
no confu-
sion in referencing the shell variable in the script, but the parsing of the script
must
still
12.5.4
be turned off while the shell variable
interpreted.
sed Option Reference
Table 12.3
how
is
to use
is
an abridged table of
sed
sed options which may be useful
in learning
effectively.
i
422
Chapter 12 Regular Expressions and Scripts
Table 12.3:
sed Command
options reference
N
Append
the next line of input to the selected
patterns with an
P
embedded new
line
Print the selected patterns to the standard out-
put
q
Go
r RFILE
Read
s/test_pat/new_string/options
Search and replace. Any character may be used instead of 7' options are zero or more of:
to the
end of the
script
the contents of file
rfile
;
g
p
Global change on a
line
Print the selected patterns if a
replacement was made
w wfile
w fname
Append the selected patterns to wfile if a replacement was made Append the FNAME
selected patterns
to
y/stringl/string2
Replace the characters
in
corresponding characters lengths of
stringl
stringl with in
string2;
the
the
and string2 must be
equal !
-function
Apply the function only
to lines not selected
by the addresses
Place the current line
output as a line
number on
the standard
Exercises and Sample Quiz Questions
12.6
12.6 Exercises
423
and Sample Quiz Questions
12.6.1 Exercises Exercise
1
Let
Ll =
{aa, bbb} and L2
=
{cc}.
What
is
the union of
•
What
is
the intersection of
•
What
is
the concatenation of
Ll and L2 (Ll
u L2)?
Ll and L2 (Ll n L2)? Ll and L2 (L1L2 )?
WhatisL2*° Exercise 2
Practice with each of the special symbols. '..'"[ '$*
in different
amples are the Exercise 3
Create a
file
UNIX commands
grep
family,
Some
and regular expressions.
ex-
find. Is, cp and rm.
with three columns of numbers. Write an
prints the third number on each
which
].*'*.' '\,' and
line in
awk command
which the
first
number
is
larger than the second.
Exercise 4
Exercise 5
Write a combination sed/awk script that takes the output from the UNIX w command and prints out only the first 10 users (alphabetically) and what processes they are running. You can use the head com-
mand
for this exercise.
Copy
the lines below into a
ing the
word "know"
file.
Use sed
into a line that reads
to
change
all
lines contain-
"Why not?" You
need
will
a scriptfile.
I I I I I
I
12.6.2 Problem
stayed up late. know I shouldn't have. ate too much. know this is bad for me. will be better in the future. don't know if this is possible.
Sample Quiz Questions 1
Why
are regular expressions a useful tool for
examples which
Problem 2
illustrate
UNIX
users? Give two
your answer.
Use a regular expression and the find command to locate all files below your current directory which end with some extension, like c,' '
.
and print them.
Problem 3
You have
to transfer a number of files from a Macintosh to the local The Macintosh file system separates directories with colons, as host. shown on the first line below. You want to convert the Macintosh file
system into your
UNIX
file
system, so that your
same way on both machines. What utility and why? Write the line of code which
is
files are
most useful
organized the for this task,
will translate a file called
424
Chapter 12 Regular Expressions and Scripts
"f ilein" with
on
file
paths of exactly four directories to the form
below. Write the output to a
line 2
file
shown
called "f ileout."
Documents: Classes: UNIX: Manual :Man2 tex Documents/Classes/UNIX/Manual/Man2 .tex .
Problem 4
Write a
sed program
that looks through every file in the current di-
rectory and replaces every occurrence of "got a fair trial" with "got an unfair
Problem 5
Explain what "escaping" a character means, and essary.
Problem 6
trial."
when
its
use
is
nec-
Give an example.
Write an
awk
script that takes the output of the command Is - 1 and named "foo" which contains the names and sizes of whose size exceeds 100 kilobytes. Sample input is shown
returns a file
those files
below: %
Is -1
-rw-r--r--
1
-rw-r--r—
1
-rw-r--r--
1
-rw-r~r~
1
Problem 7
613633 May 14 13:44 All.ps 463 May 14 13:43 ModulellABCD. aux 13374 Apr 30 1992 ModulellABCD.tex 463 May 14 13:43 Modulel2ABCD.aux
hodges hodges hodges hodges
Consider the two regular languages. LI and L2. below. Present an ex-
ample of concatenation.
Problem
8
•
LI:
{
ant,spider.beetle
•
L2:
{
hill,web,juice
Write an
awk
mand and and #8).
script
}
}
which takes as input the output from the w comlist of users and what they are doing (fields #1
returns the
Chapter 13
C
13.1
Introduction and When
a user issues
Shell Scripts
Scope
commands
or
command
sequences
executing programs, and the arguments they give
ments
to the
this sort
at the
program. In the case of interactive dialog
of program execution
is
in the
C
shell,
they are
keyboard are the arguin the
similar to interpreter-based
command
shell,
programming
lan-
guages such as Lisp. In interpreter-based programming languages, expressions are typed at the keyboard, evaluated by an expression interpreter, and re-
C shell and Bourne shell can be used programming interpreters. As with most programming languages, the expressions which can be evaluated by the shell environment are potentially more complex than a user might wish to type at a keyboard; certainly more so than they would want to type repetitively. For this reason, commands sponses are returned on the display. The
as high-level
can be written into a file is
file
and executed
in the shell
environment. The resulting
computer program, a shell script a user doesn't wish to type over and
called a shell script. Also similar to a
can be as simple as a single over, and
it
command
that
can be as complex as any program
in
any programming language.
A shell script can take many forms. The most simple form is typing a command sequence directly at the shell prompt. A shell script can also be a file with a sequence of commands that is executed like a program. You have already been introduced to many shell scripts in the form of initialization (dot) files. In Chapter 5. the special login scripts, ".login" and ".cshrc," were introduced and discussed. Both files are C shell scripts. In addition, many other dot files (e.g., ".logout," ".vacation," ".mailrc," ".newsrc," " .
exrc." ".profile."
etc.) are also shell scripts
which are executed along
425
426
Chapter 13 C Shell Scripts
own
with their
initiate a utility,
and
which newsgroups
Each command the
it is
and each
is
file is a script
will be read.
command
shell that a user creates has a
the default login shell
times
dot
is
utility's functionality.
fines
A
which is called to where a user defines variable values and customizes a For example, the file ".newsrc" file is where the user de-
special environments.
Bourne
C
the
is
shell.
shell, as
interpreter. Often,
has been assumed in this
There are many different kinds of shell
optimized for different kinds of tasks. In
C
this chapter,
Some-
text.
interpreters,
you
will be
(/bin/csh). Scripts executed in the Bourne shell are introduced in Chapter 14. One advantage the C shell has over the Bourne shell is that it can work with digit strings as if they were numbers. Another is that the command syntax is more similar to the C programming language. The notions of shell variables and scripts are common introduced to writing scripts for execution in the
to
any
has
shell
own
its
shell
environment; however, each environment syntax, so the
C
shell
and Bourne
shell
is
slightly different
and
envinronments will be pre-
sented independently. The following topics will be covered in this chapter: •
Script execution
•
Script variables
•
Script arguments
•
Script creation
•
Script control structures
On
completion of
of scripts, executed,
how how
this chapter,
they are created,
you should be comfortable with
how
they are
made
executable,
the notion
how
they are
how to perform different You should be able to write
variables are defined and accessed,
kinds of operations, and
how
to
perform
iteration.
which manipulate and execute simple UNIX commands, to incorporate the use of the sed and awk utilities in your scripts. If you need to see the source code for examples shown in this chapter, they can be found in the "UNIX /Chi 3" directory. simple
Chl3
C
shell scripts
and you should be able
13.2
C
Shell Script Execution When you
write a shell script,
it
must be executed
in the shell
environment for
was written. You can invoke a C shell script at the command line. Of course, if you are currently executing commands in a C shell this is obvious. But if you are currently executing commands in a Bourne shell, you could tell which
it
the shell to execute following
commands
in the
C
shell.
To know what
to do,
you must be able to recognize the prompt that signifies one shell over another. The prompt that is used by your computer system indicates which shell is operating. Almost always the default prompt for the Bourne shell is a dollar sign. $. and the default prompt for the C shell involves either a percent sign. %. or a
C
13.3
427
Shell Script Variables
you modify your login shell prompt, then you should you can still recognize the difference between different shell environments. Suppose that you are normally working in the Bourne shell and want to execute a set of commands in the C shell, you would invoke the shell greater than sign >. If
make
sure that
csh command
with the
_.
.
_
_
/bin/csh command
as follows:
initiates the C , , .„ shell on „"command"
-«
.
command
This cuted.
will create a subshell in
The process of creating
shell is a child
is
shell.
completed, control returns to the parent
is
new
which
will execute all
exit the shell. This
is
will be exe-
called forking a shell, and the new-
process of the current, or parent,
execution shell
a subshell
which the commands
commands
in the
When
shell.
the
command
You can
also fork a
new environment
until
you
done as follows:
/bin/csh
When is
13.3
C
this
command
executed.
To
is
issued, the dot-file associated with the
return to the parent or calling shell, type
C
shell (.cshrc)
.
Shell Script Variables One
of the most important notions of a shell script
is
the shell variable.
As
in
any program, the arguments which are provided by the user, and the values
must be associated with variables which can be addressed and manipulated by the user. There are two variable types used in shell scripts: (1) string variables, and (2) numeric variables. In addition, variables used within the
script,
can be defined three ways: (1) within the shell. (2) automatically as shell command line arguments, or (3) as environment variables. 13.3.1 Assigning In the
C
and Referencing String Variables
shell, string variables are
set command.
assigned a value with the
For example, the shell variable named "f ilen." below,
is
a string variable:
sec filen = Unix-Man. tex
Note
that the syntax allows spaces
assignment
character. =.
en't required. shell
between the variable name, filen. the
and the variable value,
The name "set"
is
"Unix-Man tex." They arC and Bourne .
required. Variables in both the
environments are referenced by preceding the variable name with a dollar
sign. $.
For example, the variable
echoed
or displayed.
filen
has been defined and can
now be
428
Chapter 13 C Shell Scripts
localhost> echo $filen Unix-Man. tex localhost> tail $filen % \appendix { answtoexs % \ include { answtoexs % \ appendix { \ latex Examples} % \ include { LatexExamples %\printindex %\bibliographystyle{apalike} %\bibliography {unixmanual % \ end { document
and Referencing Numeric Variables
13.3.2 Assigning
Numeric variables are assigned values with the @ command. For example, you might be looping through a command sequence, and the looping may continue until a shell variable called "total" reaches some value. The total '
'
variable can be incremented as follows:
@
total
=
(
$total
+
1
)
or
@
total++
The
latter
example (++
after the variable
increment operator (see Table In either case,
it is
variable name.
It is
integers, so the
@
9.3,
name total)
is
"Assignment operators
important to keep a space between the equally important to
remember
that
called the in
C
postfix
C," on page 315).
at sign
(@) and
the
numeric variables are
cannot be used to perform operations on double type vari-
ables.
13.3.3
Many When
Command
Line Arguments as Shell Variables
shell variables will
a script
is
called,
be defined implicitly through the script invocation.
any
fined as local variables, with
command
line
names $1, $2
arguments are automatically de$9. They can be redefined
in-
side the shell to be called something different, and they can be referenced with-
any other shell variable. For example, suppose you want to which takes two arguments at the command line. Each argument will automatically be assigned a numeric variable name associated with its order in the argument list. The following script echos the values assigned to two in the shell like
write a script
C
13.4
command
line
Shell Script
Arguments
429
arguments, regardless of what they are
— #!/bin/csh # A script that takes two command line arguments and echos them
(T)
echo $1 echo $2
The line labeled item (1) has significance worth mentioning now. Normally, a pound sign denotes a comment line. However, when the pound sign is followed by an exclamation point and a shell path, on line 1, it forces the script to be evaluated script in the
in that shell.
C
an example of
shell.
how
Thus, item (1)
Assuming
this
that
is
we have
simple script
is
telling the shell to evaluate this
called this script
"echoargs,"
used follows:
localhost> echoargs pink elephants pink elephants
13.3.4
Environment Shell Variables
There are many
shell variables that take
on special values from the environ-
ment, or can be set by the user. For example, the variables HOME. USER,
MAIL, HOST and TERM
are
all
environment variables, which can be
global
referred to in any shell script.
13.4
C
Shell Script
Arguments
Many shell scripts get their input from the user at the shell prompt, while others may get it from the user at execution time. When a script is invoked with arguments
it
looks as follows:
script args where "args" are the required arguments of the
script. Internally, the user-
provided arguments are ordered by their appearance
at the
command
line, as
mentioned above, and referenced with a dollar sign ($) and their ordinate position. For example, suppose we write a script called "phones csh," which .
searches a
file
named "addresses"
number. Simple enough. The
"name" $1
script
for a
"name" and
returns the telephone
keeps track of the variable by calling
430
Chapter 13 C Shell Scripts
localhost> cat phones. csh #! /bin/csh #look up a name in a phone directory fgrep $1 $HOME/addresses
Consider a sample piece of an
"addresses"
file
below.
localhost> more addresses
USHGA: P.O. Box 8300 Colorado Springs, CO 80933 (719) 632-8300 APA: 25 Goller Place Staten Island, NY 10314 (718) 698-5738
When
the script
is
executed
at the
command
"name" argument, such as "APA," which localhost> phones. csh APA 25 Goller Place Staten Island, NY 10314
APA:
is
(718)
line, the
user provides the
defined within the script as $1:
698-5738
Prompting the User for Arguments
13.4.1
A
script might also obtain information from the user by prompting for it. The most prevalent instance where one uses interrogated input is when you do not want to remember the number of arguments or their order, or when you are writing the script but others will be the predominant users. In such cases, the
user assigns a variable
from the standard
name with the value $ simpleif .csh 5 Texmacros 371 Texmacros localhost> simpleif. csh Texmacros footmpl aaai.sty diss. doc alpha4.bst diss. sty jack. sty mitthesis sty apalike.bst dissl2.doc sf suletter sty apalike.sty dissl2.sty sfsuseal.ps cite.bst disstitle.doc disstitle. sty sf suthesis sty cite. sty csuletter sty doublespace. sty texinfo.tex foo tmplrg csuseal.ps tmplrgl footmp diss.bst .
.
it
is
argu-
executed on the directory
argument's value
gument. The
made
is
used to
not 5
in the
(i.e..
any-
second
test the pattern.
ar-
Af-
can be tested as follows:
tmplrg2endif tmplrg3 uclaletter sty ucseal.ps ucthesis.sty .
.
.
The types of
tests
which can be performed
not related to simple variable and value
tests.
in the
One can
C
shell conditional are
refer to status of files in
13.6
even
the operating system, or the script. Tables 13.1
C
to the
Shell Script Control Structures
number and values
437
input arguments, within
and 13.2 show some of the available options.
Table 13.1: File system logical tests
in
the
C
shell
-r FILE
=>
returns
1
if
the file
named file
is
readable by the user
-w FILE
=>
returns
1
if
the file
named file
is
writable by the user
-x FILE
=>
returns
1
if
the file
named file
is
executable by the user
-O FILE
=>
returns
1
if
the user
-e FILE
=>
returns
1
if
the file
named file
exists
-f FILE
=>
returns
1
if
the file
named file
exists
and
is
a regular file
-d FILE
=>
returns
1
if
the file
named file
exists
and
is
a directory
owns
the file
Table 13.2: Standard variables
named file
in
the
C
shell
$0
=»
calling function
$N
=>
Nth
$*
=»
all
the
command
line
arguments
$@
=>
all
the
command
line
arguments
$#
=>
the
$$
=>
process number (PID) of the current process
$!
=>
process
$?
=>
exit status of the last task
command
name
line
argument value
number of command
line
number (PID) of the
arguments
last
background process
13.6.2 foreach-end in programming is the loop. Loops perform the task of iteration, by executing a series of commands repeatedly. Each time the command sequence is executed is called a cycle or pass.
Probably the most powerful control structure
438
Chapter 13 C Shell Scripts
In order to
make
use of the loop, the programmer must be able to enter
it and begun (entered) when some condition is met, and the loop is ended (exited) when that criterion is no longer met. for each is a looping mechanism which performs an iteration a specific number of times.
exit
it
at will.
f oreach
is
The loop
is
programming because
useful in shell
once for each of a
tions
set
performs a
it
of arguments. For example,
if
set
of opera-
you want
to write a
which performs the same task on every element in a directory, or for evps list, then f oreach is the loop mechanism to use for the C
script
ery process in a shell, f
foreach item commands end
oreach
has a simple syntax, as outlined below.
listof items
(
where "item"
)
will be associated with each
element
each loop pass (or cycle). Within the loop, "item" able.
For example, consider the loop for the
"onsince csh" .
looks
at all the
users in a
currently working on the local host. If so, current status.
The
script is
is
"listof items"
in
"onsince csh,"
script
.
and checks
list
for
referred to as a shell vari-
below.
to see if they are
"onsince csh" .
prints out their
shown below:
localhost> cat onsince. csh #! /bin/csh # a simple foreach script echo -n "Enter a list of userids please (without quotes) foreach user $< echo " finger $user fgrep "On since" end echo "
"
(lj
:
)
(
|
The the
first
item of interest in this script
echo command
feed
(i.e.,
item
1).
nately, this
command and
mechanism
after the initial
since csh, .
is
the optional '-n'
This option instructs
a carriage return) after the
ing the displayed
where
(at
echo. This
is
the user response
echo
particularly useful for hav-
on the same
requires the user to insert another
echo
argument with
to not print a line
Unfortu-
line.
echo
"
"
some-
to print a carriage return. In the script called
on-
the user inputs the userids of users he or she wants to check up
onsince csh executes finger on user. The userids are entered at the command line and read with the $< format inside the foreach item list. Notice (at item 2) that user is actually a shell variable on. For each user
which
is
name
input,
iteratively assigned a
loop. In C,
user
.
new
value from the
list
on each pass through the
would be consider a loop control variable.
C
13.6
When
a user
is
and piping
it
into
rently logged on.
currently logged on, there
is
finger output running finger
a line in the
since" in it, so if the user is logged on, fgrep "On since" will return that line
with the line "On
There are other ways
notion of f oreach. The script
is
439
Shell Script Control Structures
to
perform
if
this task, but
the user
it
is
cur-
illustrates the
executed as follows:
localhost> onsince.csh jhodges susan Enter a list of userids please (without quotes) On since Feb 1 08:01:21 on ttypl from huckleberry sfsu On since Feb 1 09:26:06 on ttyp4 from modemll.sfsu.edu :
.
f oreach
is
particularly useful for performing the
ber of items returned from a
UNIX command.
same action on
a
num-
you rename all of the files in a directory with a new extension. First you would want to list all of the files in the directory, and for each item, you want to use the mv command to rename it. You can use f oreach to perform this want
For example, suppose
that
to
task using
"ch-ext csh" .
as follows:
localhost> cat ch-ext. csh #! /bin/csh # this script changes extensions in a directory # exto - original extension # extn - new extension set exto set extn f oreach
=
$1
=
$2
item 'Is *.$exto' mv $item $item: r $extn (
(D
)
©
.
end
is defined by executing a UNIX command must be enclosed in single back quotes in order to execute, and the user should pay close attention to select backquotes instead of regular single quotes, as they look very similar on most keyboards. The balanced backquotes tell the command interpreter to evaluate the expres-
In this example, the
(at
item
1).
The
list
of items
UNIX command
sion.
The
1
two variables: ( ) "exto," which is the original extension on files, and (2) "extn." which is the new extension for the files. The Is command lists all files in the current directory which have the original extension, so these are the items which will be flagged in the f oreach. The mv com-
1.
The
:
r terminology
is
script has
part of the edit global search
modification reference." on page 168.
1
command
syntax. See Table 5.9,
"Command
440
Chapter 13 C Shell Scripts
mand operates on each new extension.
"
of these items, replacing the
.
xxx"
extension with the
13.6.3 while-end
while
is
a looping
mechanism which performs
met. Often the criterion includes what
criterion
is
which
incremented within the loop. The
dition,
mands
is
iteration as long as the entry is
while
called a counter variable
takes an expression or con-
which serves as an entry condition, and loops on the embedded comas long as the entry condition is true. The while loop is terminated
end
with an
When
statement.
the entry condition
is
control falls
false,
through to the end statement. Both the while and end statement must be on their own lines, and at the beginning of those lines. The syntax for the while loop is shown below:
while test-expression commands end (
while
loops can be used for count-control loops as in the following
"looptest csh." Count
script called
an
initial
)
.
value, and then the
means that a counter is given looping mechanism will increment and evaluate control
the counter value during each loop pass.
localhost> cat looptest. csh #! /bin/csh # a simple while script set count = set limit =
initialize
counter variable
set exit variable value
7
while $count != echo hello JBH @ count++ end (
$
limit
)
In this example, the is
used, as well as the
" !
="
ments the "count" numeric ates the
same
set
of
C
relational operator,
"count + + "
shell variable
"commands"
which means "not equal," which incre-
postfix increment operator,
by one. The
while
seven times. The script output
loop then is
straightfor-
ward.
1
.
This
is
a
and control
programming terminology which is
directed to the last
refers to the fact that
statement in the loop.
iter-
no other statements are executed
C
13.6
Shell Script Control Structures
441
localhost> looptest.csh hello JBH hello JBH hello JBH hello JBH hello JBH hello JBH hello JBH
The while loop construct hides
a potentially serious problem for the un-
suspecting programmer. If the loop
means
to exit
is
successfully entered, but there
form of intervention interrupt the job).
is
An
a
infinite
Another
is
(i.e.,
is
is
manifested. Often this
stop the job) or a
loop will not correct
ror in generating an infinite loop
not updating a
initial pass.
More important
if
is
never
than identifying
of the mechanisms for creating infinite loops, because this
be avoided, there are a few ways which,
(i.e.,
The most common ercounter which is needed to
itself.
not having a valid test criterion, so the test
successfully evaluated after the all
no
then the programmer has created an infinite loop, which will
it,
continue to execute until some form of intervention
exit the loop.
is
is
a situation to
invoked religiously, will virtually
guarantee against the possibility of an infinite loop. •
Always
looping variables before entering a loop.
initialize
initialized, then there is a possibility that
have deleterious
it
will take
If a variable is
effects.
•
Always increment counter variables within the loop body. The body series of expressions which are executed repeatedly.
•
Test the loop per, to
In addition, test the
make it is
not
on a value which can
test
is
that
condition with the expected entry and exit values, on pa-
sure that they will
work before executing
always a good idea when learning
the loop.
how
to write loops to
loop with an expression which will allow the programmer to exit the
loop without killing the associated process. This expression could be a branch statement such as described in the next section.
13.6.4
Goto
The goto construct is used to switch the flow of execution to a different statement. The goto is generally used with a conditional operator to exit from a loop without explicit reference to the loop's own exit condition. The syntax for the
1
.
goto
statement follows.
Usually called an exit criterion, which
expression.
is
the logical
complement of
the entry condition or test
442
Chapter 13 C Shell Scripts
goto label
label commands where "label" can be any string. When the goto is encountered, control jumps to "label" and the commands below "label" are executed. Consider an example script called "simplegoto csh," which modifies .
loop test csh .
to exit after 3 iterations:
localhost> cat simplegoto. csh #! /bin/csh # a simple while-goto script set count set limit
= =
counter initialization 10
while ($count != $limit) echo hello JBH @ count++ if (Scount == 3) goto foo end foo
:
foo
is the label control is shifted to here
-«
Is
Executing the script does the obvious.
localhost> simplegoto. csh hello JBH hello JBH hello JBH MH-Stuff AKCL AKCL- MANUAL - PS MX-TESTS Archive-TEX Mail Code Misc News Documents F90 OracleProject TeX3 LATEX-TESTS .
Texinputs Texmacros UnixManual akcl bin dvi2ps f ilterf ile
In the last section,
it
was mentioned
loops might want to insert a graceful exit from a loop.
latex lib mail -elsewhere manpages temp tmac
that a
goto/ label
The above
programmer learning
to use
statement which would allow a
script is modified,
below, to show one
13.6
way it.
this
C
Shell Script Control Structures
could be done. The other difference
so that an infinite loop
is
is
that this script has
generated which the
goto
443
an error in
will catch.
localhost> cat badgoto.csh #! /bin/csh # a simple while-goto script with a costly error # embedded in it set count set limit
=
11
=
10
bad counter value
while ($count != $limit) echo hello JBH @ count++ echo "counter is: $count" echo echo -n "Is it alright to continue? "
set test $< if ($test != "y" endif
(y/n)
" :
=
end stop
test exit )
block
goto stop
—
control
is
shifted to here
Is
This script illustrates a previously unmentioned means of creating an nite loop: a
poor choice of counter
which exceeds the an
exit.
The
test
limit variable will allow the loop to begin, but doesn't allow
sequence inserted allows the programmer
the counter variable
while executing
infi-
initialization values. In this case, a value
and
to exit
"badwhile csh." .
localhost> badwhile. csh hello JBH count is: 12 is it alright to continue (y/n) hello JBH count is: 13 Is it alright to continue (y/n) hello JBH count is: 14 Is it alright to continue (y/n) y count is: 15 Is it alright to continue (y/n) n AKCL MH-Stuff Texinputs AKCL -MANUAL -PS MX-TESTS Texmacros Archive-TEX Mail UnixManual Code Misc akcl Documents News bin F90 OracleProject dvi2ps LATEX-TESTS TeX3.0 filterfile
to
view the value of
without outside assistance, as shown below
:
:
latex lib mail -elsewhere manpages temp tmac
444
C
Chapter 13
Shell Scripts
As you can
was allowed to cycle several times, to illustrate was incrementing properly, but that the exit test was not succeeding and wasn't going to. At that point, the script was instructed see, the script
that the counter variable value
to exit the loop.
13.6.5 switch
and case
Often a programmer will encounter a situation
made
more than two possibilities can employ nested if -then- else and case construct. but
The switch construct bilities
is
used to
in
which
constructs, or one can use the
test a pattern against a
and to redirect execution based on the value of the
for the switch statement
switch test-string case patternl: commands breaksw case pat tern2 commands breaksw (
is
shown on
must be one
a decision
exist for branching. In such cases,
switch
number of possiThe syntax
pattern.
the next page.
)
default: commands
breaksw endsw where "default"
is
used as an "otherwise" clause. The
evaluated and compared to
of the values, then control then evaluated.
When
switched to the
first
match, then the
switch ered, since
the
is
it is
"patternl," "pattern2," is
"test-string" etc. If
shifted to the associated set of
execution reaches the
"breaksw"
statement following "endsw."
"default" commands
If
it
is
matches one
commands
that are
statement, control
is
none of the case patterns
are executed.
used when a number of different possibilities must be consideasier to
program than many if -else sequences. Consider script below, which decides what to do based on
"simpleswitch csh"
what type of input
.
it
gets.
13.6
C
445
Shell Script Control Structures
localhost> cat simpleswitch.csh # /bin/csh # A simple switch and case script !
switch ($1) case dog: echo The lazy brown dog crawls under the rickety fence breaksw case cat echo The spunky cat plays with catnip breaksw case fish: echo The slippery fish swims all day breaksw default echo "The lazy typist cannot think of anything" breaksw endsw
Several points should be script doesn't
do much. Second, the
diately following all in
made with
it.
test variable
must have a colon
(
:
)
imme-
Third, there must be a default case to catch errors (a catch-
the event that cases are left out of the options). Like the other control
structures,
switch
and
ning of their respective following output
when
endsw
lines.
must be on separate
lines,
The simpleswitch. csh
executed:
localhost> simpleswitch.csh dog The lazy brown dog crawls under the rickety fence localhost> simpleswitch.csh fish The slippery fish swims all day
.
respect to this example. First, the
and
script
at the
begin-
produces the
446
Chapter 13 C Shell Scripts
and Sample Quiz Questions
13.7 Exercises
13.7.1 Exercises Exercise
1
C
Write a
cat Exercise 2
does the following.
shell script that
command
ments on the
line,
each of which
copy the contents of the
to
C
Write a
first
It
a
is
two onto
accepts three argu-
name, and
file
documents programs using
shell script that
uses
it
the third.
man
format.
It
prompts the user for the information associated with standard manual headings, and
it
uses an editor to have the user write the text associated
with the heading.
When
but to
Exercise 3
and including
C
Write a ercise 2.
13.7.2 Problem
may seem
very complicated,
you have understood
the material up
this chapter.
program document written
in
Ex-
prompts the user for the name of the program and the direcit
is
located, and
more. Both of
using
if
shell script that reads the
which
tory in
in
It
This
file.
within your capabilities
is
it
of the headings have been completed, then
all
document
the script writes the
Chapter
displays the appropriate manual page
it
these questions are associated with the material
6.
Sample Quiz Questions 1
How
are variables identified in
valid
C
shell variable.
C
Give an example of a
shell scripts?
Given an example of a
C shell
variable
you have
seen used in this manual.
Problem 2 Problem 3
What
steps
Suppose
must be taken
that
you
at
the script.
Suppose
that
ond argument with
How
list
are about to write a
What
guments
and
"
C
shell script that will take
one variable, "pathname,
set
is
.tex.
would you
"
two "
in
the concatenation of the sec-
How
would you assign the command-line
refer to the first
script?
C
shell script that will take a user
of machine names which
eleven.
a shell script executable?
"pathname"
argument from within the
You
line
the suffix
variable in the script?
Problem 4
make
are about to write a
command
arguments
the
to
are the considerations for deciding
into the script,
name and
will vary but will never be smaller than
and how
how
to input these ar-
are they assigned
and referenced
within the script?
Problem
5
What
are the differences
between
scripts for the following constructs.
C
shell scripts
You can answer
ing the next chapter. •
Variable assignment
•
Numeric variable increments
•
Reading arguments interactively
and Bourne this after
shell
review-
13.7 Exercises
Problem 6
Write a
C
shell
segment
execute an input
and Sample Quiz Questions
that uses the 'whi le' syntax
command
n times, where the
ments, and the number of times to execute the
Problem 7
Write a
mand
C shell
line
than
Problem 8
1
command,
script that takes a directory path
directory path given, finds and prints
Megabyte
to a file
all files
and semantics
command and
and a
arguments and recursively descends the
file
44
file
its
#
to
argu-
are input
type as
com-
system from the
of file type or size greater
named "large-files
.
how to write a C shell segment that asks the user to among two choices on a fast food menu, each of which has a different number that will be printed and some way to identify the item. Write or describe
select
The
script will read the response,
the response
by
sume 8%), and
itself
print the result
cents" where RESULT numbers
to
increment the value associated with
multiplied by California sales tax (you can as-
is
the
"your total comes to RESULT amount in pennies. You cannot use real
increment variable values with the @ operator.
Bourne Shell Scripts
14.1
Introduction and The
Scope
C shell and Bourne shell are both command Each environment has its own programming language that can be write scripts which are executable in its environment. This chapter pre-
last
chapter mentioned that the
interpreters.
used to
sents the
programming language associated with
the
Bourne
shell.
We address
similar issues as to those that were presented in the last chapter but apply to the syntax
and semantics of the Bourne
shell.
The following
them
topics are cov-
ered in this chapter: Invoking a Bourne shell script Shell variables Shell arguments Shell creation Shell control structures Script functions and subscripts
When you the notion of
have completed this chapter, you should be comfortable with Bourne shell scripts: how they differ from C shell scripts, how
they are executed,
how
they are created,
variables are defined and accessed, tions,
and how
write simple
Chl4
to
perform
Bourne
how
how to
they are
made
executable,
how
perform different kinds of opera-
Like the C shell, you should be able to which execute simple UNIX command se-
iteration.
shell scripts
sed and awk you need to see the source code for the script examchapter, you will find them in the "UNIX /Chi 4" directory.
quences, and you should be able to incorporate the use of the utilities in
ples in this
your
scripts. If
449
450
Chapter 14 Bourne Shell Scripts
To
C shell scripts and Bourne shell scripts, many of the examples provided in this chapter will be the same ones implemented in Chapter 13 on C shell scripts. Pay close attention to help clarify the similarities and differences between
the differences, as they can slip
When you it
forget.
Bourne Shell Script
14.2 Invoking a which
you up when you
write a shell script,
was
written. In
cial characters
on the
it
must be executed
in the shell
environment for
Chapter 13 we introduced the notion of the (#
first line
of a script.
When
the
command
!
)
spe-
interpreter sees
it expects the next characters to identify what shell environment to execute the following script in. In the C shell, the path following # would be "/bin/csh." In the Bourne shell, that path is "/bin/sh," and the first lines of the script would appear as follows:
these characters,
!
1
#! #
/bin/sh This is a bourrne shell script
You can also invoke a Bourne shell script at the command line. Of course, if you are currently executing commands in a Bourne shell this is obvious. But if you are currently executing commands in a C shell, you could tell the shell to execute the following commands in the Bourne shell. To know what to do, you must be able to recognize the prompt that signifies one shell from another. The prompt that is used by your computer system often indicate which shell is operating. Almost always the default prompt for the Bourne shell is a dollar sign, $. If you modify your login shell prompt, as you were shown how to do in Chapter 5, then you should make sure that you can still recognize the difference between different shells. Suppose that you are normally working in the C shell and want to execute a set of commands in the Bourne shell. You would invoke the shell with the
.....
/bxn/sh command,
-
This
command
The
process of creating a subshell
is
will create a subshell in is
is
shell
which
will execute all
This
is
a Bourne
"command"
which the commands parent
shell.
completed, control returns to the parent
exit the shell.
initiates
she n on
commands
in the
new
shell
When
the command exYou can also fork a
shell.
new environment
until
done as follows.
systems, your default path will prohahly include the "bin" directory.
path to be explicit.
will be executed.
called forking a shell, and the
a child process of the current shell or
new
On most
as follows:
—
ecution
I
sh command
I
am
including the
you
14.3
Bourne Shell Variables
451
/bin/sh When
this
command
return to the
is
issued, the dot-file
parent or calling
shell,
type a
may have used the mechanism of forking which sends everything typed to a file.
When done
. The
14.3
default
.profile"
shells with the
will
script
name
session,
"script" command, on standard output
one must exit the
for the session
is
To You
be executed.
to exit the shell.
to standard input or displayed
with the file
"
shell with
"typescript."
Bourne Shell Variables Besides being a
command
interpreter, the shell is also a high level
program-
ming language. The language includes variables and control structures much like any programming language. We will first look at variables, how they are declared, how they are assigned values, and how they are used. One of the most important notions of a shell script
is
As
the shell variable.
in
any program, the
arguments which are provided by the user, and the values used within the
script
must be associated with variables which can be addressed and manipulated by the user.
The Bourne
shell
variable types, as does the
may
they
may be
does not distinguish between string and numeric
C
shell.
Variables can be defined within the shell,
be defined automatically as shell
command
line
arguments, or they
defined globally as environment variables. Bourne shell variables can
also be exported to the calling environment. In the
Bourne
shell, variables are
assigned by following the variable
name
with an equals sign and then the the value, without spaces:
f
ilen=Unix-Man. tex
Note
that the syntax
does not allow spaces between the variable name,
"f ilen," the assignment character, =, and the variable value,
tex." Variables the variable
in the
name with
Bourne
shell
"Unix-man.
-
environments are referenced by preceding
a dollar sign, $, as in the
able f ilen has been defined and can
C
shell.
now be echoed
For example, the or displayed.
vari-
452
Chapter 14 Bourne Shell Scripts
localhost> echo $filen Unix-Man. tex localhost> tail $filen % \ appendix { answt oexs % \ inc lude { answtoexs % \ appendix { \ latex Examples} %\ inc lude {LatexExamples} %\printindex %\bibliographystyle{apalike} %\bibliography{unixmanual} % \ end { document
Numeric Variables
14.3.1
The Bourne
numeric and non-numeric variable similarly. So any which must be manipulated must be considered an expression which should be evaluated. The expr command can be used in either shell environment, but because the Bourne shell doesn't have a specific mechshell treats
numeric variable
anism for numeric variable evaluation syntax for using
expr
is
often used in the Bourne shell.
it is
The
shown below, along with an example.
expr argument* where "argument*" means pending on the to
work on
member to erator,
Below
task,
string
expr
that zero or
more arguments can be used, de-
evaluates each argument separately, and
is
defined
comparisons as well as numeric and logical operators. Re-
leave no space between the shell variable name, the assignment op-
and the expression. See the man page for more information on its use. is an example of incrementing a numeric variable for a Bourne shell
script:
myvar=3 myvar= expr $myvar '
+
1
where the
entire expression
is
backquoted
to
inform the Bourne shell to evalu-
ate the expression.
14.4
Bourne Shell Arguments Like the
C
the shell
prompt or interactively within the
guments
is
shell.
Bourne
shell shell scripts
identical to the
C
shell, but will
can get their input from the user shell.
The use of command-line
at
ar-
be repeated here for completeness.
14.4
14.4.1
Bourne Shell Arguments
453
Command-Line Arguments
When a script is invoked with
arguments on the
command line it appears as
fol-
lows:
script_name [args] where "args" are the required arguments of the script. Internally, the userprovided arguments are ordered by their appearance at the command line, and referenced with a dollar sign ($) and their ordinal position. For example, sup-
pose
we
rewrite the script called
searches a
file
phones csh .
named "addresses"
number. Simple enough. The
script
for a
for the
shell,
which
returns the
phone
Bourne
"name" and
keeps track of the variable by calling
name
$1: -._-. „ ONLY difference with C shell
localhost> cat phones. sh **#look up a name in a phone directory ,
.
,
fgrep $1 $HOME/addresses
The "addresses"
file,
below,
is
unchanged from
the previous example:
localhost> more addresses
USHGA: P.O. Box 8300 Colorado Springs, CO 80933 (719) 632-8300 APA: 25 Goller Place Staten Island, NY 10314 (718) 698-5738
When the script is executed at the command line, the user provides name argument, such as "APA," which is defined within the script as $1: localhost> phones. sh APA APA: 25 Goller Place Staten Island, NY 10314
(718)
the
698-5738
where the script has been executed with the argument APA, and the result of fgrep on this value, in the file addresses, is shown. In this script, there is only one occurance of APA.
them
all in
order.
Had
there been others,
fgrep would have
listed
454
Chapter 14 Bourne Shell Scripts
14.4.2
Prompting the User for Arguments
A script might
also obtain information
from the user by prompting
The most prevalent instance where one uses interrogated input is when you do not want to remember the number of arguments or their order, or when you are for
it.
writing the script but others will be the predominant users. In such cases, the
user assigns a variable
name with
the
command read, and the
variable value from the standard input. is
A simple example
script takes the
of this type of input
shown below.
echo "Enter the person's name:" read answer
Now consider a modified version of phonesp
.
csh, called "phonesp sh," .
which prompts the user for the director}' and name of the the addresses file, and for the name to look up. The response typed by the user is assigned to the shell variable
"answer"
as
shown below
for the variables "dir,"
"ad-
dress." and "name:" localhost> cat phonesp. sh #! /bin/sh # look up a name in a phone directoryecho read echo read echo read
what is the directory path for the addresses file? dir what is the name of the address file? address what is the name to look up? name
fgrep $name $dir/$address
and a sample usage of the
script:
localhost> phonesp. sh what is the directory path for the addresses file: /usr/f 1/ jhodges what is the name of the address file: addresses echo what is the name to look up: USHGA USHGA: P.O. Box 8300 Colorado Springs, CO 80933 (719) 632-8300
where
the bolded items are those entered at the keyboard.
able can be Its
made
use follows.
A
Bourne
available to other shells and scripts with the use of
shell vari-
export.
14.5
Bourne Shell Creation
455
export filename
f
ilename=$l
14.5
Bourne Shell Creation There are two steps tested.
in writing
any
script. First, the script
has to be written and
Second, the script has to be made executable. Depending on
be using the
script, this latter step
may
require
you
who
will
to place the script into a di-
which you make public, as for linking. A script is created with an editor like any other program. In the following example, the vi editor will be used, but it doesn't matter which editor is used. What does matter is that you write scripts which are clear and readable, rectory in your execution path or into a directory
you should when you write other programs. For example, invoke an edlines below into a new file. Save the file as "hel loyou sh." This example will demonstrate how the read command is used to assign variables from the standard input.
just as itor
and type the
.
localhost> vi helloyou.sh #!/bin/sh # This simple script tests user input echo Please enter your name: read name echo Hello $name
"helloyou.sh"
;
[New file]
6
lines,
106 characters
456
Chapter 14 Bourne Shell Scripts
Two
items of note in this
path to the shell environment
comment
the
As
character.
tion of the script in the script to
be executed
when you have that
it
is
are the first
file
that the only initial difference
two
(i.e.,
created the
before, #
!
sh (Bourne file
shell
/bin/sh). Also, is
a special
environment following
in the
lines,
between the Bourne
shell)
it,
the
because they indicate
and the
pound
comman so the
shell is the is still
that forces execu-
first line
forces the
environment. As with the
you will want the same way:
for the shell script,
executable, and you will do so in
C
sign (#)
to
C shell,
make
sure
localhost> Is -1 hell* -rwxr-xr-x 1 jhodges 64 Aug 25 23:54 helloyou.csh -rw-r— r— 1 jhodges 59 Feb 1 11:26 helloyou.sh
Note the use of the asterisk wildcard to list out any file starting with "hell." This way I get both the C shell version and the Bourne shell version. Note that "helloyou csh" is executable but that "helloyou. sh" is not. .
So
the next thing
executable.
Use
"helloyou
.
you must do, before you can
the
chmod command
test the script, is to
to give yourself execute
make
the file
permission for
sh:"
localhost> chmod 755 helloyou.sh localhost> !ls -1 hell* Is -1 hell* -rwxr-xr-x 1 jhodges 64 Aug 2 5 23:54 helloyou.csh -rwxr-xr-x 1 jhodges 59 Feb 1 11:26 helloyou.sh
which
retains the earlier permissions, but also gives you,
and others, execute
permissions. Note the difference in permissions between the two scripts.
execute the script
in the
Bourne
Now
shell:
localhost> helloyou.sh Please enter your name: jack Hello jack! localhost>
This example peforms exactly the same function as did
and does so so
that the difference
demonstrated.
As
before, this
between the
example
is
helloyou csh,
two environments can be
.
easily
presented to illustrate the use of
prompted variable assignment and variable reference. Once the script is running properly, you will want to name it something memorable and move it into your default execution path. Normally, users put their executables. whether hi-
14.6
457
Bourne Shell Control Structures
nary or not. in the directory "-/bin:"
localhost> mv helloyou.sh -/bin; Is -/bin/hell* -rwxr-xr-x 1 jhodges 64 Aug 2 5 23:54 helloyou.csh -mxr-xr-x 1 jhodges 59 Feb 1 11:26 helloyou.sh
mode change
In future examples, the
you move a
file
from one directory
path, the shell will not
know
that the file is
the pointers to the default path
will
now
by invoking
When
be assumed for brevity.
to a directory in
your default execution
You can reset command line.
in that directory.
rehash
at the
localhost> rehash
You can now type the rehash command works
14.6
script file
the
same
name
directly at the
for both the
C
shell
command
line.
and the Bourne
The
shell.
Bourne Shell Control Structures We now turn to the trol
means
important topic of flow control
to control
which
in shell scripts.
UNIX expressions are executed,
Flow con-
and when.
A con-
programming mechanism or construct for conditionally executing a set of expressions. The essence is to provide a test criterion, as in sed and awk. and one or more actions to be taken as a result of matching or not matching. The use of the common Bourne shell control structures will be illustrol structure
is
a
trated here. •
if-then-else-fi
•
for-in-done
•
while-do-done
•
until-do-done
•
case-esac
14.6.1 if-then-else-fi
if- then -else construct
in the
A test-expression
given, and.
is
Bourne
shell is similar to that in the
C
shell.
"then -commands" fol"else-commands" following
if true,
the
then are executed. If not. then the else are executed. The structure is terminated with an f i statement instead of an endif as for the C shell. The syntax for the if -then-else construct lowing
follows.
458
if
Chapter 14 Bourne Shell Scripts
[
test-expression
]
then then- commands else else -commands fi
Two C
test-commands)
C
made regarding Bourne shell syntax as compared to if -then-else construct. First, the conditional (i.e.. bounded by square brackets ). Remember that, in the
notes should be
shell syntax for the is
( [
]
was bounded by parentheses. Second, the construct is closed by a mandator)' f i that matches the if The else clause is not necesshell, the conditional
.
sary unless the alternate path statements ed,
if -then-else-f i
any
constructs can be
of Bourne shell commands. Consider a simple example,
list
pleif
else- commands) are includnested. "Commands" here means
(i.e.,
.
"sim-
sh," which considers two shell arguments and makes a simple deci-
sion based on their values.
localhost> cat simpleif.sh #!/bin/sh # a simple if-then-else script if
[
"$1"
=
"5"
]
then du $2 else Is $2 fi
at the command line. If the command (du) is executed on
This script takes two arguments
ment's value argument.
is 5,
then a disk usage
If the first
argument's value
is
not 5. then a listing
is
second argument. The logical equivalence relational operator shell
==
operator)
tween the equality ed below of
all
is
test
used to
test the pattern.
operator for
C
shell
Since there
and Bourne
is
first
argu-
the second
executed on the (=. unlike the
C
a difference be-
shell, a table is present-
the logical operators for the Bourne shell.
Bourne Shell Control Structures
14.6
Table 14.1: Relational operators
in
the Bourne shell
x -It y
=>
returns
1
if
x
is
less
than y
x -le y
=>
returns
1
if
x
is
less
than or equal to y
x -eq y
=>
returns
1
if
x
is
equal
x
=>
returns
1
if
x
is
equal to y and both (x, y) are strings
x -ne y
=>
returns
1
if
x
is
not equal to y and both (x, y) are numbers
x
=>
returns
1
if
x
is
not equal to y and both (x, y) are strings
x -gt y
=>
returns
1
if
x
is
greater than or equal to y
x -ge y
=>
returns
1
if
x
is
greater than
A -a B
=>
returns
1
if
expression
a and expression B
A -o B
=>
returns
1
if
expression
A or expression B
=
!=
y
y
Table 14.2: File system logical tests
in
to y
and both (x, y) are numbers
y
is
are true
true
(i.e.,
(i.e.,
return 1)
return 1)
the Bourne shell
-r FILE
=>
returns
1
if
the file
named file
is
readable by the user
-w FILE
=>
returns
1
if
the file
named file
is
writable by the user
-x FILE
=>
returns
1
if
the file
named file
is
executable by the user
-e FILE
=>
returns
1
if
the file
named file
exists
-f FILE
=>
returns
1
if
the file
named file
exists
and
is
a regular
-d FILE
=>
returns
1
if
the file
named file
exists
and
is
a directory
Note
that the shell variable $
above. This the
C
shell,
is
how
the
459
Bourne
1
is
quoted
file
in the conditional
shell evaluates expressions.
of the example
Remember
that, in
expressions evaluated by the shell had to be placed in single back-
quotes. After the script has been
made
executable,
it
can be tested as follows.
460
Chapter 14 Bourne Shell Scripts
localhost> simpleif.sh 5 Texmacros 371 Texmacros localhost> simpleif.sh Texmacros aaai.sty diss. doc footmpl alpha4.bst diss. sty jack. sty apalike.bst dissl2.doc mitthesis sty apalike.sty dissl2.sty sf suletter sty cite.bst disstitle.doc sfsuseal.ps cite. sty disstitle. sty sf suthesis sty csuletter sty doublespace. sty texinfo.tex csuseal.ps foo tmplrg diss.bst footmp tmplrgl
tmplrg2endif tmplrg3 uclaletter sty ucseal.ps ucthesis.sty
.
.
.
.
.
The student should modify ical logical operators are
this
example, as an exercise, so that the numer-
used for comparison instead of the string operators.
The types of tests which can be performed
in the
are not related to simple variable and value tests. in the operating
Bourne
shell conditional
One can refer to
status of files
number and values input arguments, show some of the available options.
system, or even to the
within the script. Tables 14.2 and 14.3
Table 14.3: Standard variables $0
=>
calling function
$N
=>
Nth command
$*
=>
all
the
in
the Bourne shell
name
line
command
argument value
line
arguments
(if
quoted, quotes
all
the arguments in one string)
$@
=>
all
the
command
line
arguments
(if
quoted, quotes
each argument individually)
number of command
arguments
$#
=>
the
$$
=>
process number (PID) of the current process
$!
=>
process number (PID) of the
$?
=>
exit status of the last task
line
last
background process
14.6
Bourne Shell Control Structures
461
14.6.2 for-in-done
As with loop
is
some
the
C
exit condition
ten in
Bourne
shell, the
Bourne
shell
is
matched,
many
The C
it
is
used of-
set of operations once
performs a
shell equivalent is f oreach. Since, in
cases, the set of arguments can be the result of a directory listing, or the
items in a process status
for
for is a looping mechanism which
programming, because
for each of a set of arguments.
As a reminder, a commands repeatedly until
shell has looping constructs.
an expression which performs a number of
listing, etc.,
for
can be a very powerful mechanism.
has a simple syntax, as outlined below.
for index in listofitems commands done The "index" here defines
index (i.e.,
ed.
the
is
assigned the
command
Then index
loop
is
is
first
a local variable called a loop control variable.
element of the
sequence which
is
list
as a value
repeated, here
"commands")
assigned the second element of the
executed again, and so on. The "in"
loop. Also note that the closing statement
is
is
and the loop body
list
is
execut-
and the body of the
component of the
a required
"done," whereas
in f
for
oreach
it
was "end." localhost> cat onsince2.sh #! /bin/sh # a simple for script users= 'hodges hodges hodges for user in $users do finger $user fgrep "On since" |
done
It
should be noted that the single quote on the users
as easily be a double quote, as long as the
pression.
The
script is
programmer
executed below.
localhost> onsince2.sh On since Aug 6 07:51:05 on console 10 days Idle Time On since Aug 6 07:51:05 on console 10 days Idle Time On since Aug 6 07:51:05 on console 10 days Idle Time
list,
is
above, could just
consistent in the ex-
462
Chapter 14 Bourne Shell Scripts
One can
also write a
for
loop without a
list
such
as:
for index do commands done In this case, the
example of
the
list is
for
assumed
to
be the
command
presented in Chapter 13, rewritten for the Bourne at all the
users in a
list
and checks
onsince. sh
local host. If so,
line
shell,
As an
arguments.
"onsince csh" onsince sh looks
loop, consider the loop for the script
to see if they are currently
.
.
working on the
prints out their current status.
The
script is
shown below. localhost> cat onsince. sh #!/bin/sh # a simple for script for user do finger $user
|
fgrep "On since"
done
In this script, the user inputs the userids of users he or she
wants to check
the script is invoked. For each user name input, onsince sh fingers the user. When a user is currently logged on, there is a line in the finger output with the line "On since" in it, so if the user is logged on, running finger and piping it into fgrep will tell. The script is executed as fol-
up on when
.
lows:
localhost> onsince. sh jhodges csalmon On since Feb 1 08:01:21 on ttypl from huckleberry sfsu On since Feb 1 12:26:08 on ttyp4 from oad-8csld.sfsu.edu .
14.6.3 while-do-done
while
is
Bourne
a second
shell
looping mechanism which takes an expres-
embedThe while loop is semantithe while loop in the C shell. The only differences are the )around the entry condition, as for the if -then- el se-
sion or condition which serves as an entry condition, and loops on the
ded commands
while the entry condition
cally identical to
square brackets
1
.
There
taster tor
is
no particular need for
(
[
true.
]
fgrep here. Any of the grep or egrep.
simple string searches than
is
grep family would work, but
fgrep
is
14.6
f i construct, and the
The Bourne is
"done" which
terminated
for
loop.
while execution loop is described with a do clause, and it with a done statement. The while, do, and done statements
while
own
loop
is
lines,
and
shown on
entry-test-expression while do commands done [
whi le
closes the construct, like the
463
shell
should be on their for the
Bourne Shell Control Structures
at the
beginning of those
lines.
The syntax
the next page:
]
loops can be used for counting events, often called count-control
loops, as in the following script called
"looptest
.
sh."
localhost> cat looptest. sh #! /bin/sh # a simple while script count=o limit =7
while
[
-«
initialize
-•
set exit variable value
"$count"
"$limit"
!=
counter variable
]
do
echo hello JBH count= expr $count ,
+
l
v
done ="
C
which means "not equal," is used because variables are being referenced. As in previous Bourne shell scripts, the use of shell variables in the test condition must be double quoted, and the variIn this example, the
"
!
operator,
able assignment spacing must be paid attention to throughout. als,
ent. In the
by the
Of course,
liter-
such as numbers, need not be quoted, but the logical operators are differ-
Bourne
shell, the
incrementing of numeric arguments
explicitly evaluating an expression with
expr
.
In the
do
is
performed
part of the loop
expression must be backquoted. Other than these differences, the
script is very similar to
localhost> looptest sh hello JBH hello JBH hello JBH hello JBH hello JBH hello JBH hello JBH
expr.
looptest csh. The .
script output is obvious.
464
Chapter 14 Bourne Shell Scripts
14.6.4 until-do-done shell looping construct is the until loop. There is no synbetween the while and until loops, and the only semantic difference between while and until is the test condition. In the former, the
The
final
Bourne
tactic difference
loop is
is
executed as long as the condition
executed until the condition
is satisfied,
is satisfied.
while in the
loop
latter the
The syntax of the until loop
is
as
follows.
until exit-test-expression do commands done [
]
To demonstrate the use of until in a script, we looptest sh to work with until ("untiltest sh"): .
will
modify
.
localhost> cat untiltest. sh #! /bin/sh # a simple until script count=0 limit=7 exit condition is the
until "$ count" = "$limit" do echo hello JBH count= expr $count + l done [
logical
]
complement
of while entry condition
v
'
which executes predictably:
localhost> untiltest. sh hello JBH hello JBH hello JBH hello JBH hello JBH hello JBH hello JBH
14.6.5 case-esac
case construct is the analog to the C A case takes a "test-string" and matches
The Bourne struct.
shell
"patterns."
it
If
one of the patterns matches the
shell
switch
against a
con-
number of
string, then the associated
Bourne Shell Control Structures
14.6
commands
are executed until a double semicolon
syntax for the
test-string case patternl) commands [
case command
is
( ;
;
)
465
encountered. The
is
shown below.
in
]
# /
pattern2 commands
commands 1
esac The
"in" is again
used (similar to the for construct) and
is
again required.
Also, each pattern must be terminated with a closing parenthesis
case is closed case patterns can
the
the patterns. * in the
list,
with esac. Unlike the patterns in the
matches any
string, so * will frequently
doesn't match any of the other patterns, script illustrating basic use of the .
shell
)
).
Finally,
switch,
the
include wildcards. For example, a * can be used as one of
as a default/otherwise case (which
case sh"
C
(
it
is
be the
required). If
will definitely
Bourne
"simple-
case $1 in dog)
echo The lazy brown dog crawls under the rickety fence
echo The slippery fish swims all day *)
echo "No item of that type in case list"
A simple
*.
localhost> cat simplecase. sh /bin/sh # A simple case script
fish)
test-string
shown
shell case is
#!
echo The spunky cat plays with catnip
used
match the
below:
cat)
last pattern
in
466
Chapter 14 Bourne Shell Scripts
The execution of this
script is straightforward.
depending on which item the item doesn't
ment
is
is
echos
input,
match any of those
The user inputs an item and,
a string associated with that item. If
in the case
list,
then an amorphous state-
printed:
localhost> simplecase.sh dog The lazy brown dog crawls under the rickety fence localhost> simplecase.sh fish The slippery fish swims all day localhost> simplecase.sh octopus No item of that type in case list
and Sample Quiz Questions
14.7 Exercises
14.7.1 Exercises Exercise
1
Write a simple shell to
script.
Try
to execute
your
script.
You may want
review Section 3.6.2 on access permissions.
Exercise 2
What does
Exercise 3
Project: In this project you will create a shell script to customize your programming environment. With this script, you will need to enter only one character in order to edit, compile, or run the program you are currently working on. When executed the script will present the menu of choices below and prompt the user for a single character to direct its
mean
it
fork a process?
to
execution.
Menu Table
P.4:
E
edit
C
compile
L
list
R
run
Q
quit
Enter your selection (E, C. L. R, or Q):
Each choice
will
a child process.
correspond to another script
When
sented again and a until the user
the process
new choice
chooses
quit.
is
will be
that will
be executed as
complete, the
menu
prompted
This will continue
for.
will be pre-
14.7 Exercises
14.7.2 Problem
46
and Sample Quiz Questions
f
Sample Quiz Questions 1
How
Bourne shell scripts? Given an example
are variables identified (referenced) in
Give an example of a valid Bourne
shell variable.
of a Bourne shell variable you have seen in this manual.
Problem 2
What [minimal] step must be taken to make a Bourne shell script useWhat additional consideration for general use of the script would
able?
be wise?
Problem
3
Suppose that you are about to write a Bourne shell script that will take two arguments at the command line and set one variable, "path-
name,
"
in the script.
Suppose
"pathname" is the concatenation " tex " How would you asHow would you refer to the first comthat
of the second argument with the suffix sign the variable in the script?
.
.
mand-line argument from within the script?
Problem 4
You and
are about to write a list
Bourne
shell script that will take a user
name
of machine names which will vary but will never be smaller
than eleven.
What
are the considerations for deciding
these arguments into the script, and
how
how
to input
are they assigned and refer-
enced within the script?
Problem
5
What
are the differences
between Bourne
shell scripts
C
and
shell
scripts for the following constructs:
Problem 6
•
"if conditionals
•
numeric variable increments
•
"for" looping
Write a Bourne shell segment that uses the 'while' syntax and semantics to
execute an input
command N times, where
the
command and
its
arguments, and the number of times to execute the command, are com-
mand Problem 7
line input variables.
Write a Bourne shell script that takes a directory path and a as
command
arguments and recursively descends the
line
from the directory path given, finds and size greater than
Problem
8
1
Megabyte
to a file
file
file
type
system
prints all files of file type or
named
"
large-files
" .
how to write a Bourne shell script that asks the user among two choices on a fast food menu, each of which has a different number that will be printed and some way to identify the Write or describe
to select
item.
The
script will read the response,
with the response by
itself
increment the value associated
multiplied by California sales tax (you can
assume 87c). and print the result "your total comes to RESULT cents" where RESULT is the amount in pennies. From a logical viewpoint, do you see any problems with
this script?
468 14.8
Chapter 14 Bourne Shell Scripts
Recommended Reading sed and
awk
Dougherty, D. [1990]. sed
&
awk, O'Reilly Associates, Sebastapol, CA.
UNIX Programming Kernighan, B. and Pike. R. [1984]. The Prentice Hall,
Rochkind, Marc
J.
glewood
Englewood
[1985].
Cliffs,
UNIX Programming
Advanced UNIX Programming,
Cliffs, NJ.
Environment,
NJ. Prentice Hall, En-
A
Appendix
Answers
Chapter
1
to
Sample Quiz Questions
- The Login Session Problem length => Short passwords are easier to break. familiarity =>
simplicity =>
When
Problem
Too close and remember it. Too simple
is
it
is
easier to break, too distant and
easily broken,
and too complex
the fewest users are logged on.
are logged on.
is
you cannot
hard to type.
and when the fewest disk-hogs
Mornings are good, and very
late.
Problem •
slow => baud (transmission) rates are much slwer with dialin modems.
•
reliability =>
Long telephone
lines are not
designed for high speed, high
quality data transmission. •
demand
Problem 4
A
=> too
user ID
many people is
trying to use a finite
number of ports.
associated with an account, and can be the
ferent hosts (machines) with different directories.
The
same on
directory
is
dif-
sim-
ply a location in the file system where other directories and files can
be organized.
A home
directory need not be
named
the
same
as the
user ID.
Problem 5
You somehow
entered the mail
utility.
Type
"q" to exit and return to
the shell prompt.
Problem 6 •
•
whoami => returns your login ID who am i=) returns your login ID
and some information about your cur-
rent login session.
469
4#
Appendix A Answers to Sample Quiz Questions
Problem 7 •
A prompt is the
systems character for requesting input and changes with the
type of input requested. Each
response types •
password prompt, a
password:
has
its
own prompt and
accepts specific
will see a terminal-server prompt, a login prompt,
shell
,
prompt, and perhaps a mail prompt.
13_FOGNet>,
terminal-server prompt;
prompt;
utility
prompt.
During any session, a user a
•
at that
login prompt;
login: password ,
prompt; ;sf suvaxl>, mail prompt,
shell
&.
Problem 8 •
Login ID and password.
•
No
•
The user ID can only be changed by the superuser; however, the user can change the name associated with the ID with chfn. The password can be
and yes.
changed by the user and by the superuser.
Problem 9 •
sfsuvaxl> passwd
issue the
==>
command
at the shell
prompt without
arguments • • • •
Changing password for jhodges Old password: => type your old password, to verify who you are Enter new password: => type the new password Verify: => type the new password again, to make sure you know what you typed
•
sfsuvaxl>
Problem 10
Hayes command for use with a
for "ATtention Dial
modem
Telephone number 338-1200,
to dial in remotely.
Chapter 2 - Electronic Communications Problem
1
•
Advantages:
•
SPEED: You
can receive and respond
to information
much
faster than
through other media. •
RELIABILITY: You know
•
CONVENIENCE: You
•
Disadvantages:
•
NETWORK:
•
MISUNDERSTANDINGS:
message has been sent and
the
will receive
that the recipient
it.
can do
it
without leaving your work area or even
your keyboard.
You
are at the
whims of network It
is
difficult to
reliability/unreliability.
express your ideas and
understand those of others.
Chapter 2 - Electronic Communications
•
SOLICITATION:
There
is
no easy way
to stop
4#
1
junk mail.
Problem 2 •
must know how
•
mail
•
mail
•
q. x. or
to
address the mail
=> must be able
to
invoke the application
at the shell
prompt
commands => must know how to interpret and use the commands: how to read mail, how to compose, send, and respond to mail
ex
=> you must
know how
to exit the application
Problem 3 •
The > symbol
•
The
•
Examples:
•
Mail:
MH:
which message
MH equivalent to the >
"PI. •
tells
>U
symbol
the +
is
current
is
schulz-dieterich@vax Mon Mar
1
06:18
2
19/772
.
.
1+ 03/02 93 vcscl90
«
Problem 4 •
Get new messages
•
List all
•
Respond
•
Put message
messages to
into
in
MH:
inc
your working mail folder: scan
message number
number 32
into
32
32: repl
your "Personal" mail folder:
refile
32 +Person-
al •
Exit
MH
=> There
is
no need
to exit
MH because you are not in a separate
environment.
Problem 5 •
c
•
Slash, as in
vi.
Problem 6 •
SPEED: One
can respond to a posting so rapidly that
thing you •
ANONYMITY:
would
it
is
easy to say some-
later regret.
Because you are rarely close are communicating,
it
is
to the person with
whom
you
easy to say something you might
never say openly. •
EXPRESSION: The
receiver has no
flection,
way
to
guage your expression, your
in-
your pauses, each keys to understanding what a
person means.
Problem 7
A
flame
when
is
an electronic insult or criticism. Flames are usually sent
a person
thinking,
is
when
responding
to a flame,
a person has a
something, publicly, which
is
mean
when
a person
streak, or
when
this will
acting before
a user has said
way it was said draw flames from people
both incorrect and the
shows inconsideration. Oftentimes
is
472
Appendix A Answers
Sample Quiz Questions
to
who
are knowledgeble and insulted
by the poster's ignorance.
Problem 8
•
g
newsgroupname
•
f
or
F The name address convention
Problem 9
host name, such as
is
given below. The
SFSUVAX1. The
such as SFSU. and the third term
first
second term
item
is
the local
the institution,
is
the institution-type, such as
is
EDU: HOST.INSTITUTION.INSTITUTION-TYPE
.COM .EDU
.GOV .MIL
.UUCP Country Codes (such as
Problem 10
The
CA for Canada, FR "etc/hosts,
called
file is
"
for france. etc.
which contains a
which the present host knows about. This
work communications, •
•
ENTRY #2 => ENTRY #3. .
Chapter 3 - The UNIX Problem
is
list
of hosts
used for internet-
in particular electronic mail.
ENTRY #1 => Internet Protocol (IP) Address, which is used like a telephone number
•
table
1
.
for host-to-host
Symbolic or =>
Name
File
Name
communications
address for the machine
aliases for the host
System
-name \*foobar\* -print
find
Problem 2
mkdir ~/Docs/Letters/TeX-01d; cp -r -/Docs /Letters /TeX -/Docs/Letters/TeX-Old
Problem 3 Problem 4
Is -aF
which
tells
where
to find
tionally find source
an executable and whereis
and man pages. Whereis
is
more
produce the same answer as which. Then again the
example below. The user wants
A
may
is
not.
and may Consider
know what will happen when he /usr/ucb/Mail will be execut-
major difference between which and whereis
looks for the executable that
used to addi-
to
or she types mail, and finds out that ed.
it
is
versatile
is
that the
invoked when you type what you
but the latter looks for exact references to what
you typed.
former t\
ped,
I
Chapter 4 -
•
which
File Editing
473
which mail)
(e.g.,
/usr/ucb/Mail •
whereis
(e.g., whereis mail) /bin/mail /usr/ucb/mail /usr/man/manl/mail
mail: Problem 5
cp
Problem 6
Any
.
-r dir
of the following
•
cd
•
cd
•
cd
•
cd $home
•
cd /usr/fl/vcscl999
is
acceptable:
.
.
Problem 7 %
chmod 755
*
.
tex
Problem 8 %
rm ~/subl/sub2/* dvi .
Problem 9 • • •
• •
Chapter 4 -
The following
bin => usr => dev => lib => etc =>
are illustrative of directories off root
on
system binaries local files, user directories,
device
files
system
libraries
such as
ttys,
man
pages, etc.
disk drives, printers
system commands such as chown, chmod, mount
File Editing
Problem
1
access/open/create the
file
insert/append text
move around modify
text
the file in comfortable sized
chunks
on comfortable sized chunks
perform global manipulations/modifications exit in different
ways save
the file
include other files or parts of files cutting and pasting between files
Problem 2 •
vi
•
emacs
=>
:
57 => gotoline (prompts for
line:
57)
UNIX
systems:
4 #4
Appendix A Answers
Sample Quiz Questions
to
Problem 3 •
•
vi =» zz and :q or :q!, or :wq or :wq! emacs => ,
Problem 4
zz or :wq!
Problem 5
filename
Problem 6
/beezlebub
Problem 7
(slash)
Line and screen editors. of a
A
a time.
file at
A
line editor displays
and works on one
screen editor displays blocks of a
file
and
line
may ma-
nipulate and modify blocks of text.
Problem
8
:1,$ s/coconut/pineapple/g
Problem 9 •
vi
•
emacs
=>
$
,
:
1
=> ,
tells the OS where your home directory
•
MAIL
=>
=>
tells the
tells
the
A command
Problem 9
OS who you OS where alias
are
.
.
.
result of
whoami.
It is
terward. Another
way
your mail.
to look for
to look at
it is
that the alias
commands
to easily
names, but directory paths can be collapsed
The problem
most
is
new name
basically a lookup table entry for
collapse long or often used
trols the definition
is.
allows the user to provide a
more commands.
Problem 10
system associ-
shells.
to
one or
what comes
command
is
af-
used to
remembered command
this
way
as well.
likely an incorrect terminal type, since that con-
of keyboard equivalents, \example
echo $term set term=vtl00 rehash
•
%
•
%
•
%
•
Try
vi
again. If these did not work, then
go look
in
"
/etc/termcap"
for
a potentially suitable replacement term type.
looking
ways
at stty for
manually
•
try
•
talk to a guru or a system_administrator
Problem !
!
to
set the
keyboard
characteristics.
1
:s/755/700
Problem 12
process 215, 210, 206, 217 are sleeping % kill -9 214
(a) (b)
Chapter 6 -
File
Problem
1
Problem 2
Formatting and Printing WC
counts
lines,
words, and characters.
PostScript figures can take up a
lot
be a large printing job. The easiest users
is
to print
Problem
to
trees.
3
HEADINGS:
NAME SYNOPSIS or SYNTAX DESCRIPTION
number of
may
reduce the affect on other
pages with PostScript figures one
a previewer to reduce the
mention saving
of space, and even one page
way
at a time.
print cycles
you
Always use
require, not to
Chapter 6 -
File
Formatting and Printing
477
EXAMPLES BUGS FILES •
REFERENCE FORMAT: .
TH=> SH =>
•
EX,
•
.nf, •
Multipart title Subject header
.EE=> Examples .fi=> Verbatim Examples
DISPLAY METHOD: tbl file
|
nroff -man
|
col
more
|
The file has to be formatted in manpage format. There is a special macro package (the man package) for doing this. This command takes input from the SI. Output must go through more for paging. The TBI and COL commands are OK. but I do not even know what they do yet, so do not worry about them. Problem 4 •
nrof f =>
.
is •
in +NUNIT ...
.in -NUNITS, where N
a unit of choice, inches, cm.
latex => begin {quote}
mm.
is
a
number and
units
etc
end(quote)
...
Problem 5 •
nrof f-1 => .ps +2
•
nrof f-2 => \f 2 over \fl does local
•
latex-
•
latex-2 =>
1
=>
\
Large
two
increase the point size of text by italics
increases the point size of text by
{\em over} does
steps
two
steps
local italics
Problem 6
nrof f/trof f
:
The macros ".11 item, and
" .
1" starts the
el" ends the
latex: begin {enumerate}
starts the
and end {enumerate} ends the
nrof f/trof f:
.11 1 ,1s
less student support ,1s
increased tuitions and fees .Is
lower motivation .el
list,
" .
Is" begins each
list
list, list, list.
each item
is
begun with item,
4#8
Appendix A Answers
to
Sample Quiz Questions
latex:
\begin { enumerate \item{less student support} \item{ increased tuitions and fees} \item{ lower motivation} \ end { enumerate Problem 7
It
prints the file
"
code c" on .
8 1/2" x 11
"
pages with the header "my-
file" on each page using the vgrind program. Problem 8
latex filename (assumes
filename.tex)
bibtex filename (assumes
filename. aux)
latex filename (assumes
filename.tex)
latex filename (assumes
filename.tex)
dvi2ps filename (assumes
filename. dvi)
lpr -Plaser -J BINN filename (assumes filename.ps)
A full set of passes in latex may be all of these passes, depending on what you are doing. The minimum is a latex pass, a dvi2ps pass, and maybe an lpr pass, depending on how dvi2ps is interpreted by the system Problem 9 •
nroff
.ce •
.
•
.
•
1
"Mating Habits of Computer Generated Amoebas"
nroff sc
titlel:
title2:
"Mating Habits of Computer Generated Amoebas"
nroff
title3:
su "Mating Habits of Computer Generated Amoebas"
latex title: \title{Mating Habits of Computer Generated Amoebas)
•
.
•
\
nroff
heading:
su "Introduction"
latex
1
heading:
sect ion (Introduction)
Chapter 7 - Networking
4 #9
Problem 10
nrof £
no-fill command is nf and the fill command is f i. These commands would be placed at the beginning of a new line, the first
=> The
.
.
before the text and the second after the text.
latex => The
no-fill command is begin {verbatim} and the fill command is end {verbatim}. These commands would be placed at the beginning of a new line, the first before the text and the second after the
text.
Chapter 7 - Networking Problem
1
The idea here is that you do not know what the person's userid is, so you must find that out and then you use the userid to find out what
first
the person
is
up
to.
finger foobar fgrep foobar-ID rwho -a who fgrep foobar-ID users fgrep foobar-ID rusers fgrep foobar-ID I
I
I
I
Problem 2
tar
By
is
many files and concatenate them into a single file. minimum block size is forgone, resulting in a small compress removes unused/unecessary space in a file,
used to take
so doing, the
savings in size,
and usually •
results in
50%
savings in size, depending on
create an archive => c
•
view the contents of an archive =>
•
extract an archive =>
Problem
file type.
t
x
3
ftp sumex-aim.stanford.edu (ID anonymous, the password is generally your userid and domain address)
•
login sequence =>
•
find file =>
•
change retrieve mode => binary
•
retrieve
•
exit mode => exit or bye
Problem 4
cd pub.
Is,
cd
to directory, etc.
mode => get file, mget files
Approximately 507c of the
Problem 5
tar tvf cl.
3. 1.2.
tar
(this latter
file's original size is
supports wildcards)
saved.
4u0
Appendix A Answers
to
Sample Quiz Questions
The optimal choice
Problem 6
is
to use rep, since the alternative
of telnet or rlogin, cd,
Is, tar,
compress, and
is
a combination
ftp.
rep -r fuzz cs .blowtorch. edu: /usr/lib/tex/inputs texinputs .
Problem 7
who => Fastest for login. rwho => Same for remote hosts users => Fastest for login. rusers => OK as fastest.
w =>
Most
as well.
specific for idle time and current processing.
finger => The most personal. Shows login time. last => Most specific for idle time. Not fast if machine Problem 8
has been up long.
system management refers to the organization, distribution, and
File
allocation of resources within your personal directory.
keep your directories clean and organized be organized similarly to the
names of binary on.
UNIX
file
at all times,
You should
and they should
system as a whole. That
directories inside your directory should be bin,
You should make
sure that
you
is,
the
and so
are not wasting space by eliminat-
ing unnecessary files regularly, and by archiving or at least compressing those used infrequently, du, for filesystem
Problem 9
The
listings
df tar, and compress
were produced by the df command. Filesystem
have enough
free space
directories, so
we
on any filesystem
to support the
cannot use any of those disks. The
system A, associated with the device least
are used a lot
,
management.
space (-6.5 Meg); so
it
would be
"
A does not
uncompressed
" / "
disk on
file-
/dev/raom," has
the
the least likely candidate to sup-
port the port.
Problem 10 is intermittent, one would suspect someone running a number of different people running large processed, per-
Because the problem large process, or a
haps for a class.
To check
out this theory, or any theory,
and what they are doing. This 'w'
command,
this in
since
it
is
you could see who
is
on the system
most expeditiously accomplished with the
also provides system load information.
You could
use
concert with 'finger' to find out more about the user(s) in question.
is one of responsibility. If a user knowingly slows an entire work of all its users, then someone is violating basic common sense. The machine is not their own personal toy; it is not a PC. and they should respect other user's right to do work loo. The best thing to do would
The
issue here
host and the
be to ask them to nice their jobs, so that they took a low priority ing.
Another thing they might do would be
to run then job(s)
in
process-
on an isolated
Chapter 8 - C Programming
network where
part of the this
was
their
impact and seek ways to reduce
Chapter 8 - C Programming
•
481
not adversely affect anyone. If
the problem, then the user should also be spoken to, so that they re-
alize their
Problem
work would
I
it.
I
1
syntax => The structure/order associated with symbols and expressions
which comprise
legal expressions in the language.
Examples
are
the use of parentheses to define a function, or the use of a semi-
colon to terminate an expression. •
semantics => The meaning of expressions
are the concepts associated with the def-
inition of a function
and the termination of an expression (not
the
C
is
language. ..what they are
Examples
used
Problem 2
in the
for.
symbols or how
a high-level
it
is
implemented).
programming language. The strengths of a high-level
language are that they are somewhat hardware independent (portability),
they are easier to read because they appear more like a natural lan-
guage such as English, and they are easier
to
compose
into procedural
units.
level =>
The programmer is writing expressions which will manipulate hardware components almost directly. There is almost no ab-
•
low
•
high level => The programmer writes expressions which appear more En-
straction of the expressions.
and effect many lower level operations. The actual which are performed by the hardware are abstracted so
glish-like
tasks
that the
Problem
3
programmer need not be aware of how they
are done.
Portability means that a program which is implemented in a programming language on one hardware platform can be taken in source-code
version to another platform with a similar/same compiler, recompiled,
and
it
C
a
is
will run without modifications or affect
good language
language.
It is
to learn
because
it
is
on functionality.
ANSI
a standardized version of the
not currently the most portable dialect of C, but
it
has
the potential to be.
Problem 4
Programming
style
is
important for you, the programmer,
re-reading/modifying your programs
grammers
to read/understand
any kind of
text if
your programs.
We
find
it
in
terms of
other pro-
easier to read
follows certain grammatical, organizational
,
and
The same is true for a program. The opposite is also program doesn't comply with certain conventions, it is pain-
style conventions. true; if a
it
in the future, or for
ful to read.
*\o2. Appendix A Answers
to
Sample Quiz Questions
Banner => program name,
•
file
name, author, version, purpose,
ables, implementation notes, formulas, credits for
gorithms,
local vari-
borrowed
al-
etc.
Readability => veritcal and horizontal spacing of functional segments,
•
spacing between operators and variables/arguments, location of curly braces, lining
and so
Problem 5
up of assignment statements,
forth.
There are three component blocks
in a
C program:
( 1 )
headers, (2) del-
The header block defines the interfaces between functional components. The declaration block defines variable and function types. The statement block defines the expressions carations,
that will
Problem 6
The in a
{
C
and
(3) statements.
be evaluated during execution.
and
(open and close curly braces) define a functional context
}
program. They can be used around any expression, but, accord-
ing to convention, should only be used to surround
compound (more
than one) expressions.
Problem 7
Two
which
precompiler directive, er
Problem
scanf "%d" (
,
scanf and print f. They both reside in #include where stdio h is the name of a function head-
standard functions are
the stdio library,
is
accessed with the .
file.
8
&f oo)
printf("The annual rainfall in Springfield, MA is %d inches\n" Problem 9
It is
If
well inject
A
first error,
new
you
you
will [very] often serendipidously cor-
try to correct the other errors,
you may
errors.
function prototype
is
simply a duplicate of a function's type and
header, terminated with a semicolon. first
;
often the case that one error propagates and creates other errors.
you remove the
rect other errors as well. If
Problem 10
foo)
,
A
prototype
is
placed before the
procedural component in a program as a check on the types and
order of functions and parameters. After that, the actual definition of a function can occur in any location in the portant to use function prototypes
is
place functions where they semantically
reason
is
that
fined in the
it
sets the
file in
programmer up
a library.
file.
that
it
One
reason
why
it
is
im-
allows the programmer to
make
the
most sense. Another
for later using the functions de-
Chapter 9 - C Programming
Chapter 9 - C Programming Problem
483
II
The >= and = foo =
ScSc
foo = 10 && foo = 1 && bar find Problem
-name \*.eps -print
.
3
awk
'{print $1 "/" $2 "/" $3 "/" $4 "/" $5 >" f ileout"
}
Problem 4
sed -f sedfile
*
sedfile:
s/got a fair trial/got an unfair trial/g Problem 5
Whenever
a reserved character, such as a dollar sign, a backslash, or
an asterisk
is
ceding
it
used
in a
regular expression,
it
must be escaped by pre-
with a backslash. For example, the use of an asterisk
ular expression could be
in a reg-
done as follows:
-name \*UnixProject\* -print
localhost> find
where the asterisk is escaped because the find command has a meaning for it otherwise, so you must disable that meaning.
special
Problem 6
localhost> Is -1
|
awk '$4
>
100000 {print $4, $8 >"foo"}'
Chapter 13- C Shell Programming
491
Problem 7 Ll L2
=>
spiderweb, beetlejuice}
{anthill,
Problem 8
localhost> w localhost> w
|
|
awk '$1 /[a-z]*/ {print $1 $8}' or awk '{print $1, $8}'
Chapter 13 - C Shell Programming Problem
Shell variables are identified with a leading dollar sign ($).
1
ample would be $ Instances of
C
1
,
where
1
A valid ex-
refers to the first user-provided argument.
shell variables that
we have
seen are $term and $editor.
Problem 2 •
Change
•
Put the script into a [bin] directory with other scripts
•
Otherwise add the directory where the
Problem
the permissions
on the
script so that
it
is
executable
script resides to
your search path
3
(a)
set pathname
(b)
$1
Problem 4
=
$2.tex
The number of command-line arguments is not limited to 9, but the is not sure what number of arguments will be required, so the script cannot be written to take a fixed number of arguments. The best thing to do is to somehow get a list of machines from some other process (ruptime for machines which are currently up, etc.) and input them as a single argument and then use foreach to cycle through each user
machine and evaluate
it
in
whatever way the user intends.
set user = $1 set machines = 2 foreach machine in machines
Problem •
C
5
Variable assignment: shell => uses set
Bourne •
C
shell
and spaces between variable, equals
sign,
and value
=> doesn't have to use set and no spaces between variable, equals sign, and value
Numeric variable increments: shell => use the
Bourne
@
sign to assign and increment variable values
shell => uses standard notation to assign
ment variables
and uses expr
to incre-
492
Appendix A Answers
Sample Quiz Questions
to
Reading arguments
•
C
shell => uses the
Bourne
interactively:
echo and $< notation
shell => uses the
to read
arguments
echo and read notation
to read
arguments
Problem 6 #
!
/bin/csh
set count set limit set command
=
=
$2
=
$1
while $count $ command @ count++ end (
!=
$
limit
)
Problem 7
#!
/bin/csh
set dir = $1 set ext = $2
find $dir
\(
-name \*.$2 -o -size 100
\)
-print
Problem 8
#!
/bin/csh
echo What would you like to order from the menu below?' echo Big Mac - 150' echo Enter 1 for echo Enter 2 for Fries - 125" echo set answer = $< if
(
@
$answer == price = 150
1
then
)
else @ price endif @
price
=
=
(
125
$price
+
(
(
$price
*
8
)
/
echo " echo "Your total comes to $price cents"
100
)
Chapter 14 - Bourne Shell Programming
493
Chapter 14 - Bourne Shell Programming Problem
Shell variables are identified with a leading dollar sign ($), just like
1
A valid example would be $ user-provided argument. We have not seen shell variables.
cific to the is
C
the
Bourne
shell;
because the default
shell
1
,
where
1
C
refers to the first
instances of variables speshell
we have worked with
however, the same variables, such as $term, would be
defined for the Bourne shell.
Also identical to the
Problem 2
C
shell:
•
Change
•
Put the script into a [bin] directory with other scripts.
•
Otherwise add the directory where the script resides to your search path.
Problem 3 (al) (a2)
the permissions on the script so that
Reference
the
is
same
as for the
C
it is
executable
shell:
pathname=$2 tex or set pathname=$2 tex .
.
$1
(b)
Problem 4
The number of command-line arguments is not limited to 9, but the is not sure what number of arguments will be required, so the script cannot be written to take a fixed number of arguments. The best thing to do is to somehow get a list of machines from some other prouser
cess (ruptime for machines which are currently up, etc.) and input
them
as a single
argument and then use for
chine and evaluate
it
in
to cycle through
each ma-
whatever way the user intends.
user=$l machines=$2 for machine in machines do .
.
.
done
Problem •
5
"if conditionals:
Bourne
C shell •
shell
=> uses brackets around the conditional and ends with
"fi."
=> uses parentheses around the conditional and ends with "endif."
Numeric variable increments:
Bourne
shell => uses standard notation to assign
and uses expr
ment variables
C shell
=> use the
@
sign to assign and increment variable values
to incre-
494
Appendix A Answers to Sample Quiz Questions
"for" looping:
•
Bourne
C
shell => uses "for"
shell => uses "foreach"
and "done."
and "end."
Problem 6
#!/bin/sh count=0 limit=$2 command=$l
while
[
"$count"
!=
"$limit"
]
do $ command count='expr $count
+
1'
done Problem 7
#!
/bin/sh
dir=$l ext=$2 find $dir \( -name \*.$2 -o -size 100
-print
\)
Problem 8
#!
/bin/sh
echo What would you like to order from the menu below?" echo Enter 1 for Big Mac - 150" echo echo Enter 2 for Fries - 125" echo read answer "$answer" then price=150 else price=125 if
[
=
1
]
fi
price='expr
\(
$price
+
\(
\(
$price \*
echo " echo "Your total comes to $price cents"
8
\)
/
100
\)
\)'
The
system
file
A directory
is
)
(
comprised of directories and is
a branch point
and
is
files
used to organize
files
and other direc-
tories
ROOT directory:
•
•
•
•
A file •
•
file
system
ROOT organizes all files and directories ROOT is the access point for any file or directory ROOT is not organized by any other directory )
(
The top of the
in the file
system
contains or organizes information
|
A file cannot organize any other files or directories A file is always a terminal (end) of a branch in the file
system
ROOT DIRECTORY
DIRECTORY termcap
ACCOUNT DIRECTORY
.login
495
l
Appendix C Generic Login Session Terminal Server
I
**-
"t
host," "hosts," or "bye"
i Login: vcscl234 Password: gen$u3qd
Login Script
+ USER
set
HOME
set
I execute .cshrc set
TERM,
set history, etc.
Default (C) shell
define aliases
commands
execute
I execute .login set
MAIL,
set
PATH
Login
display prompt
commands
(e.g.,
pwd,
command I
Command
exit (to terminal server)
496
Issue
Is.
cd, cp.
mv.
rm, mkdir. rmdir. etc.)
\ execute
shell
is
"logout" or "exit"
^PmMMr
Appendix D
Bare Bones UNIX
Commands
NAVIGATION •
whoami:
what
USERID
•
pwd:
what
is
•
cd DIR:
relocate
me
new
is
associated with this session?
the path to this directory? to the directory
DIR
DIRECTORIES named DIR
•
mkdir DIR:
create a
•
mv DIR
change the name of directory
•
rmdir DIR:
•
chmod
DIR2:
delete
XXX DIR:
directory
DIR
to
DIR2
empty directory DIR
DIR
change permissions on directory
to
XXX
FILES copy of FILE and name
•
cp FILE FILE 1
create a
•
mv FILE FILE2:
change the name of the
•
rm
delete the file
•
chmod
•
cat FILE:
display a
file
named FILE
(scrolls)
more FILE:
display a
file
named FILE
(pages)
•
file
FILE:
XXX FILE:
FILE:
it
FILE
1
file
FILE
to
FILE2
file
FILE
to
XXX
FILE
change permissions on
identifies file type of
FILE
ARGUMENT TYPES and
•
File
•
Example:
director}'
arguments can be explicit
OR path, as required
cp foo mynewfoo cp /usr/f2/jhodges/UNIX/MOD5/DOTFILES/host
bin
497 i
498
Appendix D
SHORTCUTS
and
WILDCARDS
•
$home:
home/login directory
•
-[userid]:
account directory path for userid (defaults to $home)
•
.:
current directory
•
..:
parent directory (one up from current)
•
*:
matches any number/kind of characters
.
Shell
prompt
f
Invoke program
f
Program
J
_L Initialize
program
"." file
Display program f
prompt
Execute
commands J
Command Program
Shell
Legal program
J
exit
command
prompt
499
ppenaix mail as a Generic Utility
ELECTRONIC MAIL (EMAIL) • A method for communicating with other computer users • A method for submitting assignments • A method for requesting assistance • A method for seeing when someone is around, when they are busy
MAILER or MAIL HANDLER A program that allows you to compose, •
send, and read email
MAIL MESSAGE •
Text that you create or compose (and
•
When someone save
it,
edit)
sends you email, you can read and respond to
and delete
it
as
you would with any
it,
file
REQUIREMENTS
500
•
you must have access
•
you must know how a mailer works
•
you must know the "address" of the user you
to a mailer
are sending mail to
mail as a Generic Utility
Shell
prompt
program
Initialize
f
mail
(^
.mailrc
J
J
w
Display program
(
prompt (&)
1
?,
#,
r,
R, ,
s,
d,
q,T)
1
f
Execute
Command x,
Shell
mail 2.
mail foo
3.
mail foo
4.
~r
6.
s
7.
#
8.
r.
FILE
FILE
< FILE
q
prompt
* READ mail * COMPOSE mail to userid foo -*-
SEND
"*"
EDIT
-*•
INCLUDE
*"
SAVE
file
FILE
to userid foo
mail message (on a blank line, insert with T, stop insert with , exit with ZZ) file FILE into mail message (on a blank line)
mail message to FILE (after reading a msg)
* READ MESSAGE number # (e.g., 3) R
*-
RESPOND to mail
message
(r replies to all recipients)
10.
• DELETE mail message number # (e.g., d3) " SEND composed mail message (or )
11.
*"
HELP (info on commands/syntax)
*
EXIT, QUIT
9.
12.
d#
x.
q
the mailer
501
i^^^H^H^ as a Generic
File Editing
A file: What you •
•
•
create or
modify when you use an editor
Contents: Data or information •
A
•
A graphics application creates and
•
An editor creates and saves text into files (i.e., the two above are editors)
word processor
Operations:
Ways
to
creates and saves
manipulate
•
Create, copy, rename, delete
•
File operations take file
documents
into files
saves figures into
files
files
names
as
arguments
Types: Text and binary •
Text
refers to
[human] readable characters and numbers
Text is coded according to a convention, called ASCII (American Standard for Coded Information Interchange) If
you can read
Any
•
readable
a
file,
then
it
is
file (text file) is
a text
refers to the text version of a
Binary
refers to a
machine readable
human
file
editable
Source
program file
readable
Binary
files are
Binary
files are not, in general, editable
not
File editing: Creation, insertion into •
and modification of
file
Requirements:
AND You must know how to use the editor, AND The file must not exist, OR The file must exist and be editable. AND
502
Utility
You must have
access to an editor,
You must have
read/write permission on the
file
content
File Editing
Shell
Utility
0U3
GD
prompt
program
Initialize
as a Generic
(
.exrc
J
r
Display program
i,
prompt (emptv
k,
buffer)
D,
a, o,
O, ESC,
w,
1,
b,
h,
j,
x, y, d,
r,
J
p, P,
:,
.,
etc.
1
r
Execute
Command ZZ,
Shell
1.
VI
2.
vi
3.
i.
FILE
—
A,
O
a.
o.
—
4.
ESC
5.
h.j, k.
1
:q, :q!,
:wq, :wq!
prompt
* -*"
INVOKE INVOKE INSERT
vi
EDITOR
vi
on FILE
characters(here, right, end of sentence,
below, above) -*~
TOGGLE
to
-*•
MOVE
cursor
command mode
(left,
6.
w. b
7.
rCh
—
yy,
yNy
MOVE
WORD: cursor (forward, backward)
*•
REPLACE
character where cursor points
BY
dd,
dNd
Ch
*•
DELETE
character under cursor
**
YANK
sometimes called (one
10.
right)
->
with
9.
BY CHARACTER;
down, up,
—
DELETE
line,
sometimes called (one
line,
COPY;
N lines) N lines)
CUT;
504
Appendix
11.
d$,D
12.
p,P
13.
:
-
—
G
DELETE
to
PASTE
below, above
GLOBAL
command :#
end of
-
go
line
to line
FILE
#
include file FILE :s - search and replace :w - write :r
:q
REPEAT
14.
-
last
(in 15.
ZZ,
EXIT
vi
-
nG
GO
cursor
quit
command command mode
only)
(some save, some don't,
some 16.
at
write to
to line n. If
end of file.
n
is
new
file)
omitted, then go to
«-——^—
Appendix H Execution Path Program/command
EXECUTION
•
Running
•
Invoking the
the
program on data
command on arguments
REQUIREMENTS
AND
•
The program must be executable.
•
The program/command must be located you are currently located. OR
in the directory in
•
The program/command must be located
in a directory in
execution path. •
FILE •
command
a path to the
SYSTEM PATH A method of organizing
and locating
files
•
Full path: Starts with the root directory.
•
Directories are separated with slashes.
Example:
your
OR
The user must provide
•
which
and directories
/
/
/usr/local/bin
EXECUTION PATH A list of directories •
•
•
When you issue a command, for the command If •
found, and
if
it
is
each directory
executable,
Example: mail and
it
is
Most commands
searched
executed
vi
these clearly do not reside in the user's they are executed nonetheless •
in the list is
home
directory, but
are in the default (already provided) path, so
is
the bin directory in each account directory. If
you
EVER
command
see the response
exists, then
you
"Command
not found"
when you know
are not using the proper path to the
the
command
505
•
Each directory and
•
There are three
•
file
can be secured by the owner.
LEVELS
of security:
YOU in your directory)
•
Owner
(permissions for the owner:
•
Group
(permissions for a specific and predefined group)
•
Other (permissions
There are three
TYPES
for
anyone
else)
of permission:
•
Read
•
Write (allows users
•
Execute (allows users
(allows users in level to read a
•
Each permission type
•
The permissions
is
modify a
in level to
or directory) file
execute a
or directory)
file
defined with a binary flag (0
is off,
•
When
1
is
on).
OCTAL
4
111100100 rwxr--r-Own
or search a directory)
are concatenated into a single 9-bit string:
4
7
in level to
file
Grp
BINARY TYPE
LEVEL
Oth
a long directory listing
command
(Is -1) is
issued, the binary permissions
are displayed. •
When
•
The execute
a
user wants to change permissions (chmod), they can use the octal code. bit
must be
ON
for a user for a
program
to
be executable by the
user.
506
1
ppendix J ASCII Characters
ASCII (American Standard Code
for Information Interchange)
standardizing character sets for use in different computers. sists
column
case
a protocol for set
con-
of the integer equivalents in the table below. For example, the blank char-
acter, represented as 'bl' in the table, is represented 3,
is
The usable
2).
'a' is
The
capital 'A'
is
with the integer '32' (row
represented with the integer '65,' and the lower
represented with the integer '97.' Because the standard alphabets,
for either case, are represented with sequential integers, the conversion
from
uppercase to lowercase (or vice versa) can be accomplished through simple addition or subtraction (e.g.,
A
= a-32,
=
a
A+
ASCII Character 1
3
2
3
bl
i
*
+
3
4
5
7
F
G
H
8
P
Q
9
Z
10
4
(
)
5
2
6
4 "
32)
Set
5
6
7
8
9
#
%
&
'
$
-
1
1
'-
6
7
8
9
@
A
B
C
D
I
J
K
L
M
N
R
s
T
U
V
W
X
Y
/
A
[
]
_
a
b
c
d
e
f
5
o
h
i
J
k
1
m
11
n
o
P
q
r
s
t
u
V
w
12
X
y
z
{
1
}
E
507
C Functions
This text has introduced the basic concepts associated with programming in
ANSI
C.
The novice programmer may wish
to extent his or her skills
by mak-
ing use of function calls which have not been discussed in the text. For that pur-
pose, the followng table available with
ANSI
is
included.
The
table presents standard functions
C, their use, their library, their argument types, and their
function tyoes. Specific implementations
may
differ in location or explicit in-
vocation.
Function
Name abs
Library
Description
Argument
Function
Types
Type
integer absolute value
math.h
int
int
acos
arc cosine
math.h
double
double
asin
arc sine
math.h
double
double
atan
arc tangent
math.h
double
double
atan2
arc tangent
math.h
double, double
double
math.h
double
double
cosine
math.h
double
double
hyperbolic cosine
math.h
double
double
math.h
double
double
math.h
double
double
math.h
double
double
v.
ceil
ceiling (lowest
number Esq
cos
whole
not less than arg)
155
cosh
^ *
exp fabs floor
exponential function (e
)
double absolute value largest
whole number not
greater than argument
508
x
C Functions
Function
Description
Name
Library
509
Argument
Function
Types
Type
natural logarithm (base-e)
math.h
double
double
base- 10 logarithm
math.h
double
double
exponentiation
math.h
double, double
double
sine
math.h
double
double
sinh
hyperbolic sine
math.h
double
double
sqrt
square root
math.h
double
double
tangent
math.h
double
double
hyperbolic tangent
math.h
double
double
stdio.h
FILE*
int
stdio.h
char
*, int.
stdio.h
char
*,
stdio.h
FILE
log ""ST"
u
login
**
pow
^
sin
6q S3 En
^
tan
tanh fclose fgets
fopen fprintf
file
close
string input file
from
file
open
text file formatted output
FILE
*
char *
RLE*
char *
*,
const char *
int
types that match
output arguments
fread
binary
file
input
stdio.h
ft.
t3
fscanf
text file input
stdio.h
void
*, size_t,
FILE
*
FILE
*. const
size_t
char
*
size_t
int
types that match input arguments En
fwrite
binary
file
output
stdio.h
void
*, size_t.
FILE
size_t
size_t
*
ft*
getc **
character input from text
stdio.h
FILE*
int
stdio.h
no arguments
int
stdio.h
char
char *
file
getchar
character input from
keyboard gets
(stdin)
string input
keyboard
from
(stdin)
*, int
51
Appendix K
Function
printf
Argument
Function
Types
Type
Library
Description
Name
formatted output to
stdio.h
const char
*,
types
int
match output arguments
screen (stdout)
that
E*N
putc
character output to text
FILE
*
stdio.h
int,
stdio.h
int
stdio.h
const char
int
file
putchar
character output to screen
int
(stdout) "V,
scanf
input from keyboard (stdin)
that
*,
types
int
match input
arguments
ft.
sprintf
formatted conversion to
stdio.h
char
*,
const char
*,
int
types that match
a string
output arguments
sscanf
formatted conversion
from a
stdio.h
string
const char
*,
const char
*,
that
int
types
match input
arguments isalpha
logical function for
ctype.h
int
int
ctype.h
int
int
ctype.h
int
int
ctype.h
int
int
ctype.h
int
int
ctype.h
int
int
ctype.h
int
int
alphabetic character
OS
isdigit
logical function for
base- 10 digit character
islower
logical function for
lower
case letter as
ispunct
punctuation character
CC3
the
document
Run makeindex on ("file.idx"
=>
file
"file.aux," "file.dvi")
the
document
Run bibtex on
document
the
("file.aux," "file. bib"
Run latex on
the
=>
file
"file.bbl")
document
to include the
("file.aux," "file.bbl." "file.ind"
Run latex on
the
document
Run dvi2ps on =>
=>
is
all
the
manual you latex
"file.aux," "file.dvi")
"file.ps")
these files,
a trace of the
files
document
the
Other intermediate and debugging compiling
new
=> "file.aux," "file.dvi")
to resolve references
("file.aux," "file.bbl," "file.ind"
("file.dvi"
file
"file.ind")
namely
commands
files are
"file. log"
and
constructed along the "file. big."
way of
For example, below
run for compiling and printing the latex version of
are reading:
Unix-Man
makeindex Unix-Man bibtex
Unix-Man
latex
Unix-Man
latex
Unix-Man
dvi2ps Unix-Man
Oftentimes you will have to run
bibtex
latex
to get the cross-references correct.
a
number of times
This
is
after
running
especially true if
you are
520
Appendix L LATEX Templates
using a table of contents, because
it
lags the references
by one compilation cy-
cle.
L.4
A L A T^X Sample
Article or
Research Paper
localhost> cat paper.tex \title{The Mating Rituals of Computer-Generated Rabbits \thanks{The research reported in this paper was funded in part by a grant from the Food and Drug Administration (contract no. P00032-95-7615) }} .
\author{Foo B. Baz \\ Zip Z. Zappo \\ Artificial Reality Laboratory \\ 3290 Bunko Hall \\ University of Cachaphony, Los Alamos \\ Los Babbaloey, AC
09002}
\date{}
\maketitle \vspace{0 5in} \centerline{ \Large\bf Abstract} \bigskip .
This paper briefly reports on the computational mating habits of computer-generated rabbits. This work is part of a research project at the University of Cachaphony, department of Surrealism, and has been supported by the FDA for 23 years...
\section{Introduction}
Qf)
The computational model of rabbit mating patterns has been widely talked about since the discovery that computer-generated rabbits have unusual behavior traits, such as...
\vspace{0 2in} .
This example Item
(
1
)
Item (2)
is fairly
The paper
typical of latex papers:
title.
Embedded
in the title
support, which
is
reference
is
an acknowledgement for financial
printed in footnote format at the bottom of the
page.
Item (3) The author lines,
list,
separated by double backslashes to produce unique
and the address of the authors.
A LATEX
L.5
Item (4) The current date, which can be eliminated
if
is
Letter
Command
File
printed below the author/address
521 list,
and
desired.
Item (5) Tells latex to print the
title.
Item (6) Begins the paper abstract. Note that the abstract on the same page with the title.
in this
document
is
Item (7) The one item of note in the generic paper is the section command, with its text argument. In latex, all numbered sequences are per-
formed during the compilation, so the author need not worry about section numbering. If sections are moved, latex reorders the numbering the next time the document
Much of the detail graph look
L.5
A lATeX
at
missing in
this
compiled.
example, because none of the
such as itemize or enumerate are used.
styles,
ual chapters
is
is
is
also kept in the
something a
Letter
"tletter tex" .
bit
"manual"
directory so that interested users can
File
a simple version of a letter format that takes a standard
document, "newletter.tex,'* as input, and produces a terhead. this
You
letter
with a technical
let-
You can do below so that a PostScript figure is includallocated for it between paragraphs, in which case the
can place a PostScript letterhead on the page easily.
by modifying the command
ed, but that
para-
more complex.
Command is
list
A copy of one of the man-
no space
letter is written
is
on top of the
localhost> cat tletter.tex \documentstyle{ letter} -* \begin{ document} \location{Marine Mammal Laboratory, \telephone{338-2125} — \ signature {Joe C. Student} \include{newletter} -* \ end { document \end
file
figure.
(T)
901 Hensil Hall}
The command file, above, remains the same all the time. When you run latex, this is the file you run it on, as with other command files. For example, a sample
command would
appear as follows.
522
Appendix L LATEX Templates
localhost> latex tletter This is TeX, C Version 3.0 (tletter. tex LaTeX Version 2.09 /usr/f 1/ jhodges/TeX3 0/usr/ local /lib/ tex/ inputs /letter sty Document Style 'letter' .) (newletter.tex [1]) Output written on tletter. dvi (1 page, 220 bytes). Transcript written on tletter.log. .
(
.
)
The source it
letter
can be called anything.
"newletter." This
as
will overwrite
When you
are done, simple save
your previous
newletter,
but
is
OK as long as the letters, or whatever, are saved under separate names. A sample source letter
newletter
is
shown below:
localhost> cat newletter.tex \begin{letter} {Foo B. Baz \\ 123 Xyz St. \\ Anywhere USA 03246}
(l)
\opening{Mr. Baz,}
(2)
Regarding our phone conversation on Tuesday, May order a .
23
would like to
I
.
\closing{best regards,} \end{letter} Item (1) This
is
(3)
in the
example
is
usually printed on the
the address
left,
which you are sending the
just prior to the
tex will print today's date above the address
letter to.
opening statement. Also,
in the
same
la-
location. Item (2)
is
the opening, which could be Dear, To whom it may concern, Shirley, etc., as you choose. Similarly, item (3) is a closing statement, and will be indented to near the middle of the page, below the last bit of text. Other letter commands are used similarly to other latex commands: enclose arguments in curly braces. The commands available for letters are
Table L.1:
latex
Letter options reference
\opening { item}
Your opening
\closing{item}
Your closing
\cc{item}
A
\encl{item}
Enclosures
gratuity gratuity
carbon copy
line
L.6
All other latex formatting
LATEX
Bibliography Examples
commands can be used
in letters,
523
such as enu-
merate, itemize, and quote.
L.6
lATeX Bibliography Examples Bibliographies are very important in academic reports, latex has a special pro-
which
cessor, called bibtex.
pulls cites out of a latex
references from a reference database, formats desires,
document, takes these
them according
to the author's
and places them back into the document. The following two sections
illustrate the
use of cites in this manual, and the format of cite references in the
reference database.
The use of bibliographies
in the
preamble and closing seg-
ments has already been discussed.
examples\cite{pc87
}
.
The example at
There
L.6.2
Document
l
Bibliographic Entries in the L T X £
L.6.1
isn't
much
to
it,
(2)
is the most straight forward of
really.
Bibliographic Reference Templates
The following are templates for various document types. I will explain the format of one. and let that serve as a basis for their use. With this and the documentation, you should be able to incorporate cites in your documents. The first thing you will notice is that there is a broad range of text types that are referenced
in the
templates subsequently.
Manual Techreport
Book Article
Inproceedings Incollection
Phdthesis
Unpublished Ch6
Inbook
bib
in
The samples below the "bib" directory.
are included in a file
named "bibtemplates bib" .
524
Appendix L LATEX Templates
localhost> cat bibtemplates.bib Sample Bibliography Templates
% %
%
The percent sign is a comment character in TeX and LaTeX. Anything appearing after a percent sign will not be compiled.
%
Each entry in one reference will be described:
%
% %
% % %
%
@techreport { joy 80a, title = "An introduction to ..." = "Joy, W.", author institution = "Computer Science year = "1980", :
.
.
%
type
=
%
month
=
%
}
(1)
%
(2!
"Manual", "September"
.
"
368
290
backquote
in
C
shell
in emacs 18 273 282 1
subtraction operator in
C
291
list all files in
A/UX
decrement operator 314 subtraction assignment operator 315 variable assignment in
C
equality operator
in
Bourne
shell
427
458
>
in
awk 416
absolute path 60 access control 93 access mechanism
C struct
353
access permissions 94
change 95 account allocations 251
break-ins 94
mail message header 27
ID 6
>= greater than or equal operator in
Is 70
xii
in
290
variable assignment in shell scripts
in
275, 281
\n
to abort mail message 25
in
C
tagged expression reference 407
in
C
301
maintainence 251 name 6
439
in in
awk 413 C 310
B
athO
security
Hayes hang up command 13
password 10 athl
actions in
make
Hayes test 409, 411 ScSc 416 < 416 >= 416
390
awk
activity
network status 234 address IP 20, 21
command
13
action
machine 20
print
mail 19 address-of operator 280, 327
AIX
line
411
membership (~) 415 field reference 412 inequalities 416 logical AND and OR 413 piping ( ) 414 printf 414 redirection (>) 414 test action 409 test pattern 409 awkward phrases field
xii
algorithm 331 alias
command name 64 alias 31, 140
I
current alias bindings 141 aliases
mail 31 mail host 21
MH38
diction
178
ampersand (&) in
scanf 280
B
process backgrounding 162
AND in awk 414 anonymous ftp 244
background 162 backquote
ANSI C 275
evaluate expression in shell 148
append output redirection 158
apropos 64 archie 245, 246 command reference 246
Bourne
in
C
in
command
452 439
repetition 164
banner
comment 269 256
print 180
extract contents
256
batch processing 17 batch programs 278
archives
software 244
baud bg
argc 346 arguments
command argv 346
line
biff 149
342 access mechanism [x] 344 double-subscripted (2-D) 344 element comparison 345 element search 345 indices 344 parallel 345 parallel array comparison 345 single-subscripted (1-D) 344 sorting 345 vector 344
article selection level
binary digits 96 binary number 96, 366 binary search 345 bit
96, 366
bitmaps 174 bitnet bits
42
per second
baud
blow up program 369 body
C
102 checking utilities 176 assignment operator 314 file
asterisk
C
file
290
1
variable tests reference
for-in-done
459
461
relational operators reference
at
command
loop constructs 3
message content in mail 24 bounced email 40 Bourne shell 135 case-esac 464
ASCII
attention
4 156
rate
blast process
in
rn48
multiplication operator in asterisk wildcard 432
4
rate
background 163
346
array 282,
Hayes
shell
shell scripts
bang
archive create
in
script creation
13
455
459
537
538
Index
script executability script testing
455
increment and decrement table 314 precompiler directives
455
standard variables reference 460
printf
formatting options reference 275
branching statement in
C
shell scripts
program banner comment 269
435
browser for
BSD
elseif, and endif 374
if,
until-do-done 464 while-do-done 462
declarations block 271
HTML documents
528
documentation 268 header block 271 statement block 271
xii
buffer 118
programming module entry point 283 exit point 283
select 125
bulletin board
45
bus error 365
relational operators
301
statement terminator
semicolon string input
(
;
)
275
282
strings
character arrays 282
#include 273 style
address-of operator
functional indentation 295 horizontal space 295
ampersand (&) 327 assignment
vertical spacing
operator (=) 290 statement 290 assignment operators table
switch-case variable
naming convention 279, 288
315
while
case sensitivity 268 comparison expressions 300
entry condition 313 initialization
loop body 3 1 update mechanism 313
optimization 277
compound statement 272
C
272
shell
command
do -while for
loop 317 entry condition 317 loop 315
file
140
test
reference 437
435
script
goto
control expression 315 initialization expression
banner 319
320 lexical scoping 320 local variable 320 main header 271 output function 323 parameter 320 default type
441
switch-case 444 while 440 while counter variable 440 standard variable reference 437 variable assignment arithmetic 428 call by reference 287, 325 canned functions 279
carbon-copy implicit
parameter type consistency 322
30
email 20
in
272
types 320 functions 319 if construct 304 iteration 3 logical connectives 308 logical operators 308 loop nesting 318 mathematical operators 290 operator precedence 290 operators assignment operator table 314
441
infinite loops
315
update expression 315 function 271, 283
1
alias
argument
logical expression
body 316
return statement
313 312
exit condition
compiler -o option 276
curly braces
295
construct 305
mail 25 case-esac ;
465 Bourne
;
in
shell
464
1
cast explicit data type conversion
cat
91
cc
email 20
in
cd
76
$HOME 76 .
.
76
295
539 warnings 278 compile-time errors 364
77 - 76 ceil
compress 260
math function library 29 change directory 76
concatenation
character count
1
computer program 266
command
77
in
find 84
in
95
mode 96 mnemonic arguments 95 SJ mbolic mode 95
shell 137
conjunction
binary
in
awk
413
contents of directory 69 control characters xiv
chown 99 chsh 150
control structure
circular reference
email forwarding 31
COBOL
C
conditional compilation 374 conditional or test if construct 303
406 child process 163. 427, 450 resular expressions
chmod
options 71
conditional
character escape
in
Bourne
shell
in
C
435
shell
copy
266
with
cp
col
file
innroff, troff 200 command 57
remote 247
copy
in
emacs
117
options 62, 63 interrogative
count++
80
repetition 164
in
command command
in
concatenation 77
207
interpreter 61, 133,
426
arguments 346 variables 428
line
as shell
in shell scripts
429
command mode in vi command override in vi 113 command repetition
103
event selector 165 command sequencing 77, 157 command shell 133
prompt 61
command command
in
in in
440
in
Bourne
shell
in
C
440
shell
463
counter variable in C shell loops 440 country-code institution-tvpe in email 22
cp 83 alias to interrogative 141
creation date listing
72
creation mask 99 cross post
terminating character 77 wildcards 73
in
rn 46
C
shell variable
in
mail
137
29
csh 427
make 389
comment
shell scripts
crt
comment in
C
count-control loops
file
latex
text in
key 63
command command
83
emacs 124 core dump 381 corrupted file 258 cos math function library 291
140 arguments 62 alias
buffer
457
curly braces in
character shell 137
C C shell (#) 432 latex 204
comment-out source code 270 communications electronic 18
communications etiquette 17 comp 36 compilation 266 parse 267 compile text formatting 86 compiler 266 1
C
modules 272
current directory 68 current message pointer 27
cursor in text editins
103
cycle in
C
shell loops
437
D data transmission rate 4
data type 352 array 281 coercion 294
540
Index
FILE * 355 struct 352 declaration 358 field variable access 358
date
display in
MH
40
display message range
mail 30 document
149 debug 267 debugger
print
179
shell scripts
document body 204 document translation 185 documentstyle 204, 208 design 527 domain mail 22
trace function
dot
Gnu Debugger 380 debugging 278
dummy
functions 377 function drivers 379
433 372 decimal number 366 default printer 180 default values
command arguments 63
in
123
relation
make 390
in
diction 178 diff 83, 88
drafts folder
options reference 89
in
MH
direct text modification 107
directory 58
dvi 205
access control 93
change 76 contents 69 creation 78 current or working 68 flat 75 home 67 list contents 69 owner 67 ownership 93 parent 71
C
shell 427 debugging shell scripts 434 quoted arguments 434 shell variable value 69
removal 79
deep 75 shallow 75
dirs 140 disjunction in
awk 413
disk block 255 disk hog 255 disk space allocation 251 disk usage 254
contents 91
edit in latex compilation 207 Ecapsulated PostScript (EPS) 174 echo 6, 144, 158 Bourne shell 451
relocate 79
subtree
telephone number 13
E
permissions 93
recursive 81
dial
dvi2ps 205 dvips 205
path 60, 68
removal errors 80 rename 79 root 58 shorthand 68
40
dt
du 254 dump file cat
direct login 3
loop 317
laser printing 182
Hayes
366 96
and customization 426
awk 417
do -while draft mode
digits
70
double dot directory 71 double quote
device capacity 254 df 252, 253 field name reference 253
digit
lists
utility initialization
emacs
dependency
188
dot directory 71 dot file 9, 134 in directory
delete text in
command innroff
edit
MH
in
40
editable file 102 editor full
screen 101
line si
102
ream 102
VI file
access 102
egrep
83, 408 operator reference 88 options reference 88
electronic
communications mail 18
]
X
541 news 45
exp
math function
42
talk
emacs
explicit type conversion
118
key 118
cast
command
buffer
copy
1
text
1
24
exit
from
file
access 1 18 finding 118
file file
1
global effects command reference global options 126
incremental save 125 kill
mode
1
1
26
fclose
125 reference
1
27
126
fgrep in
if-else
delimiters in
awk 409 specifier
scanf
printf,
305
291
access mechanism 358
305
C 290
structure
357
field variable
for
struct 352
file
access 102 attributes listing 72 compression 260 copy recursive 83 creation 102
creation date in
364
error propagation 278,
364
errors
recoverable 278 escaping characters 406 etiquette
communications 17 in talk 43 network 18, 228 event selector repetition 165
executable file 276 execute permissions 95 execution path 146 //
awk 413 awk 412
matcher
EPS 174 equal sign
exit
83, 87, 453 process redirection 160
variable
OR if-else-if
command
350, 351, 355
field
reference 128
347 end-of4ine character 347 environment variables 68, 146, 429 EOF 347
in
loop control 440
278
foreground 162
end-of-file character
in
library 291
fg
Encapsulated PostScript (EPS) 181 encryption schemes in rn 52
error flag
type option 70
file
math function
AND
nested
embedded
452, 463
Is
word delete 123 email 18 email problems returned mail 41 service unavailable 41 system down 41
embedded
shell
fatal errors
command
utilities
Bourne
if 304
selecting a buffer 125 string search
in
while
open buffers 125 meta key 1 1
session
454
falling through
123
file
shell
fabs
1
list
save
Bourne
-F
buffer 125
kill text
in
expr
F
1
manipulation command reference 126 modification command reference 124
interaction
295
export
1
cursor control command reference 122, 123 cursor movement 122 delete text 123
file
library 291
explain 779
116
cntl
441
exit criterion
posting articles 49
Is -1
listing
display with more 90 dump with cat 91 locate
with string search 86
opening 102 ownership 93
Is -1
listing
path 60 permissions 93 reading from f scanf 350 relocation (mv) 82
removal 81 rename (mv) 82
72
72
542
Index
Is -1
revision date in
save
Is -1
size in text
emacs
in
listing
72
function
125
application
listing
72
347
type 70 writing to with fprintf 351 FILE * data type 355 file
copy 83
file
insertion
324 319 types 320 function call 274, 319 zero arguments 333 function driver 379 side effect
structure
vi 14 commands vi 112
in
1
file level
in file
merge
file
permissions octal and binary codes 98
file
pointer
file file
system 57 system management 251
file
transfer protocol
file
1
14
variable
C
function libraries user-defined 385 functional context 272
347
242
filec
shell variable
G gcc 275
138
58
files
compiler options 276 383 command reference 382 option reference 381
gdb
find 83, 84 command
options 86 options reference 86 use of wildcards 84
global effect in
find file in
emacs
1
18
1
12
global options
emacs
host 230
126
global search
43
login status user 230 finger file 230
remove extension
230
439
1
goto in
rn 46
grep
floor
math function
(:r)
Gnu C compiler 275 Gnu Emacs 15
flame 18 in
commands
file-level
invi 107
433
finger 229
talk
vi
global modifications
finding errors in scripts
in
320
320, 323 mixed parameter types 328 parameter call by value 324 prototype 284, 322 call
library 291
flow control
Bourne shell 457 folder 36 f open 348, 355 f oreach 438 in C shell 438 in
C
shell
441
83
example 145 family options reference 88 operator reference 88 group permissions 94
H
foreground 162 for-in-done in
Bourne
shell scripts
forking a shell 163. 427,
FORTRAN
266
f orw 36
forward email 31 fprintf 351 f scanf 350, 355, 359
ftp 242 binary 258 reference 243, 244
command
in
prompt 245 prompt 243 path 60 default
latex
compilation 206
hardware platforms xii Haves command set 13 head 91, 92 header file 273, 385 heading
innroff
192
headings
man page 65 high-level languages
history
ftp>
lull
help
461
450
266
139. 163
histon,
C home
shell variable
director) 6.
139
67
creen editors 101
.
543 mode vi 103 INT_MAX 368
home page
insert
WWW 531
host 5
hostname
HTML
INT_MIN
148
368
integer overflow error 367
528
document control options 528
interaction
figure control 53
font type options
529
interactive
paragraph style options 530 section header options 529 hung process 156 Hyper Text Markup Language (HTML) 185, 528
mode
emacs
in
17
1
program 278
interchange format 185
42
internet
20
Internet Protocol
intervention in infinite loops 441 IP address 20, 21 is a member of
I
awk 415
in
iteration
enter insert
mode
in
vi
105
I/O or 10 273 idle time with rwho 236
if 304 conditional or test 303 syntax of construct 303
C
shell
437
event accumulation 318 event counting 318 event selection 318 iteration in C 311 iterative refinement 283, 31i
if-else 303 C shell 435 if-else-endif
C
job 162
shell scripts
parentheses 435
number 162 print 181
if-else-fi in
Bourne
457
shell
remove
print 182
jobs 162
if-endif in cshrc 137 implementation file 386 implicit type conversion 294 inc 35, 36 .
K
include file in
email message 25
file in
latex 207
include files 385 include statement 273 increment operator 3 1 postfix 313 prefix 313 increment, decrement operators table
314
communications package 13
kermit
156 key bindings in
129
standard input 133 kill
342 338
awk 416
infinite
loop 369
emacs
125
emacs
123
kill text
in
inequalities in
buffer in
indirection operator
C
emacs
keyboard
indirect login 3
in
64
kill 156
indexing in arrays
man option kermit
kilobytes 254
shell scripts
441
kleene star 405 Korn shell 135
information shared 228 initialization file
inline
134
commands
in
latex 209
inner loop 318 input function 280, 324 input parameters 287
long listing
Is
star closure
of language
option 70
L*
L 404
J
544
Index
modem 4
language 401 concatenation operation 401 difference operation 401 intersection operation 401 star closure operation 401 union operation 401
network 4 terminal server 4 login shell 9 login status in finger 230 logout //
laser printer 181
last 233 latex 204
long listing 70
look
compilation e, x,
h debugging commands 206
font size options reference 2 font type options reference
body 311 control expression 3 control variable 3 in
1
217
commands 209 commands 209
1
sed 418
commands in
latex 209
line continuation character in
line
line printer
line
C
281
count 177
437
in
Bourne
shell
461
loop control variable in
Bourne
C
shell loops
461
438 looping in C shell 440 looping mechanism 31 low-level language 266 in
shell loops
lpq 181 lpr 179 command lprm 182 Is 69 -a -F
option option -1 option -R option
112
line editor 102,
shell
loop body
pagecontrol options reference 217 paragraph style options reference 219 reference control options reference 221 resume template 514 section header options reference 218 texformats path 49 texinputs path 149 leaf or terminal 58 line address range in
C
1
1
pass or cycle 313 update mechanism 313
inline
line
776
loop
document control options reference 216 documentstyle design 527 figure control options reference 220
line
prompt 6
login
180
option reference
70 70 70 75
range
vi
file-level
linear search
commands
1
12
345
M
LISP 266 list all list
70
directory contents
Is 69 In
symbolic link 169 load average 241
log
math function log out
I
MACH
library 291
nroff mail
institution-type
function in math library 291 logical connectives 308 logical expression in
C C
commercial 22 country-code 22 dialup 22 educational 22 government 22
300
435 300
shell
logical flags
22 network 22 military
logical test in
C
shell
137
logical tests in
190
I
log 10
in
xii
machine remote 241 machine address 20 machine language 266 macro in make 389
awk 413
login
organization 22 message abort 25 message content 24 path 27
direct 3 indirect 3
ID 6 remote 3 login
mail
19, 23, 24 display message range 30 institution type reference 22
1
80
n545
minimum
path 27
mkdir
read message reply to
number 27
message 30
commands 34 command options 34
special (~) tilde
mail aliases 31 mail bouncing 32
Mail Handler Rand Corporation 35 mail
ram 36 scan 36 send 36 show 36
internal command options 33, 34 message header reference 28, 29 message list reference 26 message pointer 27 paging 29
mm
innroff modem 4
MAIL
UNIX
make
387, 388, 394
subdirectories
60
motd
comments 389 dependency 388 macros 389 task actions 390 makefile 388 Makefile 388 makefile 388 make index 526 man 63
-k information man macros innroff 190 man page xvi, 63
move cursor invi 107 ms macros in
option 64
mv
emacs
address 280, 327 value 280 files in
mail
message of the day 7 Meta key 1
18
35 aliases
38
comp comp
147
36
ref ile 36 repl 36
next occurrence in string search 66 21
name address convention name alias in finger
name
230
server
problems with email 41 in
40
execution path 147
prev
N
nested
folder 36 f orw 36 inc 36 next 36
rename 82
negation
36
drafts folder
relocation 82
file
n
25
mesg 45 emacs
file
interrogative 82
maximum integer value 368 me macros innroff 190 memorv 280
in
79
directory renaming 79
65
124 markup language 1 85
MH
190
alias to interrogative 141
text
in
nrof f
multiplexer 12 multi-tasking 133 multi-user operating system 133
directory relocation 79
more
merging
7
mounted device 252 mounting a device 58 mounting point 254
display
in
display 65
next occurrence (n) 66 options reference 90, 91 reading saved mail 30 string search 90
31
lists
90
man page
major
mark
190
standard output 133
more
23 23
variable 146
mailing
368
moderated newsgroup 49 modular design 370 modular programming 318, 330 in debugging 374 monitor
utility
mail
MH
integer value
78 macros
C
awk 413 if-else shell
444
nesting decisions in
C
305
netiquette 18
network activity
234 228
etiquette status
of a host with ping 239
546
Index
uptime and newline character 347
ruptime 239
paging
utility
(pager)
more
65 paragraph environment in nrof f 192
newsgroup 45 36 755
next nice
345
parallel arrays
non-terminating loops 369
parameter
notify
parent directory 7 parent process 163 parent shell 427, 450 in process multi-tasking 162 parentheses operator precedence 29 parse
C shell variable nrof f 187
138
1 95 193 hyphenation control option reference 196 line control option reference 194
font type/size control option reference
formatting
utilities
macros
in
man package 190, 198 me package 190
mm package ms package
190 1
90
awk
PASCAL
271, 286
script
417
266
pass in C passwd
man macro
options reference 202 margin/tab control option reference 194
list
shell loops
437
11
account access control 94
password 6
me macros
selection advice 10
who
column control option reference 197 heading control option reference 197 paragraph style option reference 198 miscelaneous options reference 196 page control option reference 193 plain 190
can change 10
path directory 68
mail 19, 27 pattern delimiter
405
pattern matching 399, in
404
vi 404
period notation
O
in
struct
access, reference 353
permissions execute 95
-o
C
C compiler option 276 object code 266 off-by-one errors 314, 369 offensive postings to rn 52 online manual xvi
operating system xii, 3 multi-tasking 133 multi-user 133 optional command arguments 63
OR in
group 94 other 94 owner 94 read 95 types 95 write 95
compiler option 277
pipe 32, 160 piping
awk 414 nrof f 190 Pnews 49 in
plain
posting to a newsgroup 49
awk 413
ordinal position shell variable reference 453 other permissions 94 outdated phrases
diction
ping 239
178
outer loop 3 1 output function 280, 323 output parameters 286
owner permissions 94
pointer 287, 325 to a UNIX file 169
popd
140 postamble in
latex 204
postfix increment operator 428,
440
posting to a newsgroup 49
Postmaster in email 20 PostScript 174, 181
pow
page layout 208 pager 29
more 90 PAGER variable
148
math library function 291 preamble in latex 203, 207 in nrof f 192 precompiler directives if, elseif, and endif 374 in C 273
q547 prev
object code 266
36
object code execution 276
print
printf
debugging 370 330 source code 266 stub 286 style 267 syntax errors 364 task decomposition 283 programming language 266 grammar 266
banner 180 document 179 job 181
solution feasibility
printer laser 181
180 queue 181 printf 272 line
control string
274
numbers 293 debugging 370 format examples for integers and strings 293 format options 274 in awk 414 procedural abstraction 283, 330
interpreter4oased
field specifiers for real
PROLOG
LISP.
for
425
267 semantics 266 syntax 266 portability
project organization
make 387 prompt 5, 7 prompting Bourne shell 454
process 133
backsround subshells 162
156 execution blast
prototype declaration 357 with structures 357 pruning in search 345 ps 151 display field reference 152 execute field reference 152 options reference 154 removing header with sed 420 status priority reference 153 status swap field reference 153
background 162 foreground 162 execution status 152 foreground 162 ID number 162 memory status 153 piping 160 priority 153 redirection 157 remote 241 remove or kill 156 stacking 162
pseudocode 338
status f51
public software archives 244
stop 162
pushd
stopped 156 process pipe in conjunction with
in
more
90
program xii bugs (errors) 267 header 27 program crash 365 program design
quota
364 332
off4oy-one 369 operator precedence 369 propagation 364 run-time 365 execution escape 371 expressions 266 flagging errors 364
266 modular design 370 instruction
name
reference 252
quote
Bourne in
shell scripts
(
" )
awk 417
quoting the arguments in
compile-time 364
251
field
errors
library include
ftp 258
pwd 68
Q
iterative refinement 318 program stubs 375 programming commenting out debug statements 371 debugging 278 stubs 375
lexical
140
put
echo 434
R -R
Is
recursive option 75
rep 248 read
mail message
27
read in
Bourne
shell
read permissions 95
454
459
548
Index
reading from
350
recoverable errors 278 recursive copy 248 recursive descent directory contents listing 75 recursive file removal 81 redirection in
in
encryption schemes 52 newsgroup selection options 51 pager selection options 53
file
f scanf
awk 414 mail 25
47
startup
root directory 58
254 volume 254 rsh 250
root disk root
run-time error 365 ruptime 239, 241
rusers 237
mail messages 25 reference operator shell variable 69
ref ile
36, 40 regular expressions
400
full
symbol reference 408 421 pattern matching 404 special characters 408 special symbol escape 406 symbol references 406 regular language 404 alphabet 401 empty word 401 in
sed
401 infinite 401 word 401 finite
relational operators
300
mv
mail message 30 save-some-buffers in emacs 125
scan
for
82
in
command vi 111
vi
circularity in
linear
345
mode
in
vi
250
account 10 security risk .
156
sed
rename file 82 repl 36
rhosts 94
417, 418
command
options reference 422 range 418 regular expression 421 shell variables 421 line address
replace text
invi 110 reply
segmentation
30
fault
365
selecting a buffer
resource sharing 228, 242 response time -1 return statement 272 return statement for void function 286
rlogin 249 rmSI
in
emacs
125
selecting passwords 10
SELF-PACED semicolon
( ;
xvii
)
command
sequencing 77 275 send 36, 39 sequenced commands 157 in
interrogans 141
79
rntm 36
C
service unavailable 41
45, 46
set
.news re 46
139, 427 in
article selection level
article selection
66
security
remove process
alias to
111
111
more
option in
users 237 who 236 kill
modification 167
binary 345
processing 241
rn
451
in
machine 241 port 4
rmdir
debugging 371
search 345 and replace
login 3
mail
36
scanf
remote copy 247
shell
342
scalar variable
script
302
with
26
scheduling priority process 153 screen dumps 174
relocation file,
mail
scanf 279
rehash 457 table of
subject header option in
save
48
options 52
sh
C
shell
450, 456
138
549 shared information 228 shared resource 228, 242
status variable
shell
stdin
Bourne
standard input
shell
variable assignment 451
C
437
script
command comment
line
arguments 429
character
432
debugging 433 trace debugging 433 variable assignment 427 variable
environment variable 429 in sed 421 numeric 427 path 146 reference 146
427
string
prompt 7 399, 425 character 137
dot file scripts 425 login scripts 425 shell variable 427
Bourne
shell
451
138 history 139 notify 138 shorthand directory notations 68
show 36
debugging 380 stop a process 162
stopped process 156
strcmp strcpy
274 comparison 358 in search 64 string arguments in echo 434 string
string search
for file location
vi
in
111
data class 352 element access 353 field variable 352 hierarchical organization
354
type 352 variable declaration 353 structure
352
tag
type 352 structured variable 342 array 342
login style 177
72 254
style file
listing
slash disk
145
library function 291
size
in
.
19
208
sub-shell in
345
process multi-tasking 162
subshell 163,
Bourne
source
innroff source 151
sqrt
math function library 291 square brackets shell
427
shell
450
switch-case construct in C 305 in C shell 444
190
source code 266 spell 175, 176
Bourne
143
subdirectory 59
smiley face, :-) software xii
458
stack
directory access 140 stack frames
debugains 384
standard input 25, 133, 150, 157, 350 standard output 133, 150, 157 status file
86
emacs 126 in more 90 in
structure 351
math
in
358 350, 355
stream editor 102, 417
stty
sin
in
pointer 351
declaration 357
shell
filec
sorting
file
struct
shell script 134,
comment
pointer 350
step through
programming interpreters 425 programming language 399 remote 250
C
file
stdout
standard output
shell
iteration
shell
f scanf 350
scanf,
70
of processes 151
switching station 12 symbolic address 21 symbolic link 169
symbolic name 21 syntax errors 364 system administrator 14
system
down
email problems 41
system hogs 229
System
V
xii
550
Index
u Ultrix xii
tag
407
regular expression
in
tag-based language 528 Tagged Interchange Format (TIFF) 174 tagged regular expressions 407 tail 91
talk 43 etiquette
43
exit with
44
library function 291
tape archival 242, 256
tar
242, 256
command
reference 259
256 256 file selection 256 verbose (v) 256 create (c)
extract (x)
99
login 145 uncompress 260 UNIX xii, 3 in
.
untar 257 until-do-done in
tan
math
umask
Bourne
uptime
shell
464
239, 240
Usenet 42 user 6 user ID 6 userid 6 USER variable 146 user-defined structures 357 users number of 241
users 237 remote 237
target value
uucp 42
search 345 161, 409
in
tee telnet 249 command
reference 250
telnet>
prompt 250
vacation
term
C shell variable termcap file 144
138
declaration
struct 353 environment 146
terminal 5 terminal characteristics 5 terminal server 12 test-expression in
Bourne
shell
shell reference operator
declaration 358
if 457
structured array
function drivers 379
1
1
variable assignment
347
text formatter
in
185
verbose
text readability
with style 177 text source file 186 thesaurus awkward phrases 179 TIFF 174
69
commands
in
mail
in
C
34
include mail message 25 timesharing 5
vgrind vi
in shell script
debugging 433
option reference 183
102 :r 114
toggle out of insert mode command mode 03 command override (!) 113 cursor exit
movement
from (ZZ,
352, 357
file
manipulation
file
merge
I
1
13
command
reference
14
commands (:) 112 command reference 15 global effects command reference i (insert text) command 105 file-level
mode 103 cursor 107 replace character
1
insert
typescript
move 451
106
reference 109
:wq)
global
t\pc declaration 271
m script
138
182
command
trace
trof f 1H9 tset 143, 144
shell
mode ftp 242
1
tilde-r
file
352
unpredictable value 368
altering text
typedef
342
structure
text editing
tilde (~)
69
struct
testing
text file
32
variable
r 110 scope of cursor movement 109 (
)
1
12
1
14
w 551 search and replace 1 1 search mode 1 1 text alteration reference 110 text insertion reference 106 void function 286
W w238 field description reference
238
wc 177 What Now options in
What You See where is 93
which
MH is
40
What You Get 185
92
while in
C
shell scripts
440
uhile-do-done in
Bourne
462
shell
who 235 am option 229 in talk 43 i
remote 236 i 9, 229
who am whoami
229
8.
wildcard (*) in file
removal 8
wildcards in
find 84
word count 177 word delete in
emacs
123
working directory 68
World Wide
Web (W3)
185
write permissions 95 writing to a file
fprintf
WWW
351
home page 531
WYSIWYG
185
X X in
rn
52
x exit in
XENIX
latex
compilation 207
xii
ZZ exit
from vi
1
1
STUDENT
L SERIDs
AND ALIASES
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
Name
UserlD
Mail Alias
GROUP ALIASES
Userids in Alias
Group Alias Name
Userids in Alias
Group Alias Name
Userids in Alias
Group Alias Name
Userids in Alias
Group Alias Name
PRINTER INFORMATION Printer
Type
Printer
Network Name
Printer Location
:
—
INTRODUCTION
TO
Berkeley UNIX and ANSI C JACK HODGES For anyone who wishes to
use, understand,
and appreciate the
UNIX operating
system, this text provides the framework for a self-paced, self-teaching course. This timely volume introduces a widely used version of Distribution or Berkeley
UNIX, and
UNIX known
the functional basics of the
language. Using plain language, author Jack
Hodges
as
Berkeley System
C programming
divides coverage into three
main
parts:
The UNIX Operating System The C Programming Language System Programming with C and UNIX.
He
discusses the elements of programming and the interactions that the operating system mediates between the user and the computer in generic terms, making the
concepts easily applicable and transferable to any hardware platform, language, or data
file.
In addition. •
BSD
UNIX and ANSI C: widely used UNIX type
Introduction to Berkeley
Introduces readers to one of the most the
1
An
operating systems
dialect.
Offers numerous examples tested in the Ultrix
UNIX dialect,
including examples
•
executed on a number of BSD-compatible UNIX hardware platforms. Provides a conversion table for all examples presented in the text in a separate
•
Develops a fundamental working knowledge of
appendix. simple but correct use of syntax, programming
program
C
for
style,
programming, including
debugging, and logic
in
simple
writing.
In addition. Jack
Hodges encourages
i
shares with other languages since the?
new programming
skills easier later
o
_!
\
3-068495-3
PRENTICE HALL Englewood
Cliffs,
NJ 07632 099 I
it ii
I
II III
60 138 "684958
..j..