Introduction to UNIX
Copyright 1988, 1990, 1991
Computation Center
The University of Texas at Austin
Austin, Texas 78712
CCUG-1
Revised, July 1991
----------------------------------------------------------------------
Foreword
This manual is for new users of the UNIX* operating system on the
Computation Center's Encore Multiprocessor system, UNIX VAX, Sun
workstations, and DECsystem 5810. Although UNIX functions essentially
the same way on all these, the systems will be referred to in this
manual as the EMX, IX, Sun, and 5810 whenever there is a need to
distinguish among them.
The reader of this manual is assumed to have some prior experience
with computers, but not with UNIX. This manual does not attempt to
teach any programming language or the details of using a text
formatting program. It does explain the following:
+ How to connect your terminal to a UNIX system, log in, and
log out.
+ How to change your password.
+ How to set up your terminal to communicate with UNIX.
+ How to display online help on your terminal and how to get
printed copies of online documents.
+ How to create and maintain files and directories of files.
+ How to use editing programs to alter files.
+ How to print files and how to move them to and from other
computer systems.
+ How to compile and run programs you have written.
+ How to send mail to other users and read messages they send
to you.
+ How to do elementary shell programming.
___________________________________________________________________
*UNIX is a trademark of UNIX System Laboratories.
The Computation Center will appreciate any comments about this
manual in order to improve later editions. Address comments
to:
Documentation Staff
Computation Center
The University of Texas at Austin
Austin, TX 78712-1110
Or through campus mail, use the address:
Documentation Staff
Computation Center
COM 1
Corrections to Computation Center publications are maintained in an
online errata file. For a list of corrections to this manual, type:
doc intro_errata
The dates on Computation Center publications indicate the
last time the material was reviewed for technical accuracy
rather than the actual publication date.
1. Introduction
-------------------------------------------------------------------
1.1 Notational Conventions
Throughout this manual, the following notations are used:
1. Command words and arguments in commands are shown exactly
as typed, including the distinction between uppercase
letters and lowercase letters. On UNIX, uppercase letters
are not interchangeable with their lowercase counterparts.
Thus UNIX treats "MAIL", "Mail", and "mail" as three
distinct entities. If an item is underlined or in italics,
substitute an appropriate value for it. {NOTE: no
underlining or italics in this ASCII file}
2. If an item in a command syntax line is shown in square
brackets [], the item is optional. These brackets are
purely a documentation device; don't type them in the
command itself. However, do type any other punctuation
that is shown, such as a hyphen.
3. If an ellipsis ... is shown, it means you can repeat the
previous item.
4. No command line is actually processed until you press the
RETURN key. This manual does not show the RETURN.
5. Control characters are issued by holding down the key
marked CTRL while you type another character. They are
designated in this manual in the form . For
example, means to hold down the CTRL key while you
type c.
6. The backspace key may be marked DEL, DELETE, RUB, or RUBOUT
on your terminal keyboard. This manual designates it as
DEL.
As an illustration of conventions 1 through 4, here is the format of
the cp command, which is used to copy one or more files into a
directory:
cp [-i] [-r] file ... directory
The command name cp must be given in lower case; the -i and -r may
be omitted or given exactly as shown; then at least one filename (or
several filenames) of actual files; and finally an actual directory
name. To send this command to the UNIX system, press the RETURN
key.
--------------------------------------------------------------------
1.2 Special Characters
Many punctuation characters and control characters have special
meaning to UNIX. Those you should know even before your first login
are listed below. Others are given in Chapter 4, "The Shell and
Command Processing".
--------------------------------------------------------------------
1.2.1 Correcting Typing Mistakes
At any time before you press RETURN, you can correct a typing error
by using:
DEL to erase the previous character,
to erase the previous word,
to discard the whole command line.
--------------------------------------------------------------------
1.2.2 Controlling Output to Your Terminal
If output scrolls up on your terminal screen faster than you can
read it, you can suspend it by typing . To resume the
display, type . You can abort a process, and discard its
output, by pressing . You can also discard output or skip
over part of the display by using . When you do this, the
program producing the output does not stop, but its output is
discarded until you give another or until there is no more
output to send.
2. Getting Access to the UNIX Systems
--------------------------------------------------------------------
2.1 Connecting to a UNIX System
Before you can use any of the Computation Center's UNIX systems, you
must have a Computation Center user number and you must have it
validated for the system you will use. Forms to request validation
are available in Room 27 of the Computation Center Building. Once
your user number has been validated, you can connect to UNIX through
the MICOM port contention system. If you have a validation for the
Center's TELESYS dial-up system, you can connect from a terminal or
microcomputer that has a modem.
--------------------------------------------------------------------
2.1.1 Connecting Through the MICOM
If your terminal is on the MICOM system, turn it on, let it warm up,
and press the RETURN key. You will see the MICOM prompt:
CLASS=
Respond by typing:
CCNET
and press the RETURN key.
When the MICOM responds "GO", wait 2 or 3 seconds, then press RETURN
(more than once if necessary), until you see a CCNet> prompt. X
"CCNet> prompt" Now type:
connect host
to get the "login:" prompt from that Computation Center host machine
(for example, connect sneezy to reach the Sun workstation named
sneezy, or connect bongo to reach the 5810 Ultrix system). If
the MICOM says "BUSY, WAIT? nnn", all the ports are already in use.
You can respond by typing y to wait, or n if you don't want to wait.
The number nnn tells your position in the wait queue.
If your terminal is on a dial-up line and you don't have a TELESYS
validation, you can connect to the MICOM system by dialing 471-9400
if your terminal is running at 300 or 1200 baud, or 471-9420 for
2400 baud. Wait for the carrier signal, connect the modem, then
press RETURN to get the CLASS= prompt. Proceed as described for
terminals on the MICOM system.
--------------------------------------------------------------------
2.1.2 Connecting Through TELESYS Dial-Up
If your terminal is on a dial-up line and you have been validated
for TELESYS, dial 471-9496 to connect to the terminal server. You
will then be prompted for your Computation Center user number and
password (initially, as issued when you first obtained a user
number, but you can change them). After giving them, you will see
the prompt:
Telesys>
Now type:
connect host
to get the "login:" prompt from that host.
--------------------------------------------------------------------
2.1.3 Connecting from Other Computers
If you are already logged in on aonther computer, you can use telnet
or (from another UNIX system) rlogin to connect to any Computation
Center UNIX System. Note that since you remain logged in to the
original system as well, you may be accruing charges on both
machines at once. If you want to connect through telnet or rlogin,
read the online help on your local system.
--------------------------------------------------------------------
2.2 Logging In the First Time
In response to the "login:" prompt on your first UNIX login, type
the 7-character user number (in lower case) issued you by the
Computation Center accounting office, and press the RETURN key.
UNIX then prompts for your password, like this:
Password:
Type, in lower case, the password issued to you when you received
your user number, followed by RETURN. For security, the password is
not displayed when you type it.
Because you have never logged in on this system before, a special
program now runs. It may ask you for a UNIX login name that you
will use for future logins. It will certainly ask you for a new
password. If you are not asked for a login name, your user number
will be your login name. If you are asked for a login name, choose
up to 8 lowercase letters that help others identify you for mail
purposes--such as your first initial and surname. If someone else
has already used your choice of login name, UNIX will not accept it,
and you will have to choose something else.
In selecting a password, choose one 6 to 8 characters long that is
meaningful ONLY to you; you don't want anyone else to guess it
easily. In other words, don't choose your initials or nickname. On
the other hand, be sure the password is something you can remember
yourself. If you forget your password, you will have to go in
person, and with proper identification, to the UNIX consultants to
have them assign you a new one. UNIX will not accept a password
that is too easy to guess; for example, it won't accept ordinary
words from a dictionary.
Now the program prompts you for optional data you may want to put in
the "personal information block", a database that other users can
look at. The block can include your real name, address, phone
number, and projects you are working on. If you don't want to
include any individual item, just press RETURN in response to the
prompt for it. If you want to get out of the program entirely,
press , the interrupt character.
After this login program has completed, you can use your new login
name and password for future logins.
At this point, UNIX displays any message of the day that may be
posted. Should the message scroll off the screen before you can
read it--or if you want to see it again later--redisplay it by
typing:
more /etc/motd
If you see the message "You have mail." or "You have new mail.", it
means another user has sent you a message. See chapter 8 to learn
how to read and send mail.
Finally, you will see a prompt indicating that UNIX is ready for
your commands. On Computation Center UNIX machines, the prompt
includes the hostname (the machine's network name). On other UNIX
systems, this prompt is typically either a $ or % character.
--------------------------------------------------------------------
2.3 Future Logins
After your first UNIX session has established a login name and
password, use this name and password in response to future login:
and Password: prompts. After you do this, you next see a line
showing the time and date of your most recent previous login.
You then see any message of the day and notification of mail as you
did on the first login, followed by the command prompt.
--------------------------------------------------------------------
2.4 Logging Out
At the end of the UNIX session, do not just hang up the phone or
turn your terminal off. Instead type the command:
logout
Some UNIX systems give no job summary or logout message. You simply
see another "CCNet>" prompt if that's how you connected. To get
from CCNET back to the CLASS= prompt, type:
exit
Occasionally, after typing "logout" you may see another login
prompt. In this case, you can press the BREAK key three times about
one second apart, then press RETURN to get the "CCNet>" prompt.
Another way to log out is to type , which signals the end of
input you are typing into a file. If you are at command level
instead of typing text into a file, then ends your UNIX
session. But there is a potential danger here: it is possible to
log out by accident--say if you finish typing the input to a file
and inadvertently type twice instead of once. You can
prevent from logging you out. If your commands are being
read by /bin/csh (see next section), do this by giving the command:
set ignoreeof
at the beginning of each UNIX session. A convenient way to do this
is to place the command in an initialization file that is executed
automatically every time you log in. Such files are discussed in
section 2.6 and in chapter 4.
--------------------------------------------------------------------
2.5 Identifying Your Terminal to UNIX
Some programs that read input from your terminal, or send output to
it, need to know what type of terminal you are using so they can
process control characters correctly. Before you tell UNIX what
type of terminal you have, you must know what command interpreter,
called a shell, is reading the lines you type. To find out, type:
printenv SHELL
If the response is /bin/csh, then identify your terminal by giving
the command:
setenv TERM type
where type identifies your terminal.
If the response is /bin/sh, use the two commands:
TERM=type
export TERM
Here are some common terminal types at UT Austin:
terminal type
DEC VT100 vt100
MacKermit vt100
Micro-Term Ergo Series vt100
Micro-Term MIME-2A emulating mime2a
enhanced VT52
MS-Kermit vt100
Sun Workstation sun
Televideo 950 tvi950
If your terminal is not any of these, you can leave your terminal
unidentified without serious problems. But you will not be able to
use a screen editor without specifying your terminal type (see
chapter 6).
--------------------------------------------------------------------
2.6 Login Initialization Files
Whenever you log in, the UNIX shell searches your directory for
certain initialization files and executes the commands in those
files. If you always want your terminal set up a certain way, you
can put commands to do this into the initialization file named
.login ("dot-login"). To insert command lines into .login, you must
use an editing program, as described in chapter 6. Here are sample
lines from a .login file:
echo 'confirm term type (RETURN) or enter correct value:'
setenv TERM `tset - -Q -m 'network:?vt100' -m 'unknown:?vt100' -m 'dumb:?vt100'`
The "echo" line displays the following text at your terminal:
confirm term type (RETURN) or enter correct value:
Then the "setenv TERM" line displays:
TERM=(vt100)
and you can respond by pressing RETURN if you are really using a
VT100 or by typing, say, mime2a if you are using a MIME-2A. This
"setenv TERM" line is another method of making sure you have
correctly identified your terminal to UNIX. If you use such a line
in your initialization file, substitute your most frequently used
terminal type for vt100. Be sure to use the accent grave (`) and
the apostrophe (') exactly as they are shown here. Of course if you
always use the same terminal, you could simply have a single line in
the .login file that identified your terminal type. The pair of
lines used here, however, allows you to change the type in case you
are not using your regular terminal.
When your user number is validated for a UNIX system, you will
probably be given a default .login file. If not, you can copy the
file /usr/local/lib/Login to your own directory. (See sections 5.2
and 5.9 to learn how to do this.)
--------------------------------------------------------------------
2.7 Changing Your Password
It is a good precaution to change your password frequently to
protect your files and your user number. To do this, type the
command:
passwd
You will be prompted first for your current password, then twice for
a new one (the second time for verification). Remember that the
password should be something you can remember but others can't
easily guess. It can be 6 to 8 characters long. As when you first
chose a UNIX password, you must not choose a word from a dictionary.
On Computation Center UNIX systems, the passwd program also rejects
passwords that match anything you put into the personal database
(for example, your nickname). Putting uppercase letters or digits
in the middle of your password helps make it harder for others to
guess. Neither your old password nor your new one will appear on
the screen when you type it.
3. Getting Help While On the System
--------------------------------------------------------------------
3.1 Finding Out What's Available
All commands on the UNIX system are described online in a collection
of pages of the UNIX Programmer's Manual. They are commonly
referred to as "man pages", and the full set of them is organized
into eight sections:
1. commands 5. file formats
2. system calls 6. games
3. library routines 7. miscellaneous
4. special files 8. system maintenance
If you know the name of a command, you can view its man page at your
terminal. If you don't know the name of the command you need, you
can use a command called apropos.
Apropos searches through the header lines of the man pages for
whatever keyword you supply and shows you the list of the man pages
it finds. To use it, type:
apropos topic
where topic is a word describing what you want to know. For
example, if you can't remember what command or commands can be used
to perform copy operations, you might type:
apropos copy
which would produce a list of man pages that contain "copy" in their
header lines, like this:
bcopy, bcmp, bzero, ffs (3) - bit and byte string operations
cp (1) - copy
cpio (1) - copy file archives in and out
dd (1) - convert and copy a file
fork (3F) - spawn a copy of this process
rcp (1C) - remote file copy
uucp (1C) - unix to unix copy
copysign, drem, finite, - copysign, remainder, exponent
logb, scalb (3M) manipulations
ncargtest (1NCARG) - create, compile, link, a program that
uses an NCAR Graphics utility.
The numbers in parentheses identify the section number for each man
page. From this list you could choose the one you need. Try
"apropos intro" to see the names of some man pages that are
introductory information.
--------------------------------------------------------------------
3.2 Getting Specific Information
Once you know the names of the man page or pages you need, you can
display them on the screen by giving the man command:
man [section] name
If you omit the section number, the man program searches through
each section in turn until it finds the named man page. This is
fine if name is unique. But a few names exist in more than one
section. For example, there is a section 1 man page for lpr--the
command that sends files to a line printer--and also a section 7
page with miscellaneous information related to lpr. To get the
section 1 page, you can type:
man lpr or man 1 lpr
but to get the section 7 page, you must type:
man 7 lpr
Most manual sections have an "intro" man page, describing that
section in general. So to find out more about library functions
(section 3), type:
man 3 intro
When the information produced by man is displayed on your screen, it
pauses after each screenful and displays the term --More-- as the
bottom line. This gives you time to read the current screenful
before you go to the next one. To request the next screenful, press
the space bar. To request just one more line, press RETURN. You
can read the entire man page this way or, if the information at the
beginning is all you want, you can type q to quit.
If you expect to refer to this man page more than once, or if it is
not convenient to display at the terminal, you can make a printed
copy. To do this, type:
man name | col | ul -t dumb | lpr -Psite
where site designates the location of the line printer where the man
page is to be printed. The vertical bars are called "pipes", used
to pass results of one command on to another command (see section
4.2.2). To see a current list of sites, type:
man sites
--------------------------------------------------------------------
3.3 More Extensive Information
The man pages are generally short descriptions of specific commands
and functions. There are also longer documents accessible through
the doc program. To see a list, simply type:
doc
which displays the document titles, each with a one-line description
and a page count. It is impractical to view these long documents at
your terminal. Instead, you should print them with the lpr command,
like this:
doc name | lpr -Psite
For example, to print the vi introductory document, viin, at the
Taylor Hall site, use:
doc viin | lpr -Ptaylp
--------------------------------------------------------------------
4. The Shell and Command Processing
The UNIX program that interprets your commands is called the shell.
The shell examines each command line it receives from your terminal,
expands the command by substituting actual values for special
characters, then calls the program that executes the command. After
the command has completed execution, the shell prompts you for a new
command.
Most UNIX systems offer a choice of two shells--the Bourne shell
(sh) and the C shell (csh). The default shell for the Computation
Center's UNIX systems, and the one described in this manual, is the
C shell. Since this manual describes the C shell, you should make
sure that is the one you are using. To do so, type:
printenv SHELL
If the C shell is reading your commands, the response will be:
/bin/csh
For a thorough discussion of the C shell, see An Introduction to the
C Shell by William Joy. It is available online in the document area
described in chapter 3. The following sections discuss only those
shell concepts most needed by novice UNIX users.
--------------------------------------------------------------------
4.1 Command Syntax
UNIX commands begin with a command name, often an abbreviation of
the command's action (such as cp for copy). Most commands include
flags and arguments. A flag identifies some optional capability and
begins with a hyphen. An argument is usually the name of a file,
such as one to be read. For example, the command line:
cat -n stuff
calls the cat program (to "concatenate" files). In this case, cat
concatenates the file named stuff to your terminal. The -n flag
tells cat to include line numbers as part of the display.
Besides the hyphen that precedes a flag, and that serves to keep the
flag from being interpreted as a filename, there can be other
special characters in the command line. The shell interprets such
characters, and possibly replaces them with other values, before it
passes the command with its flags and arguments to the processor
that actually executes the command.
One characteristic of command syntax peculiar to UNIX is that
uppercase and lowercase letters are not interchangeable. Thus if
you tried to give the cat command by typing CAT, it would fail;
there is no program named (uppercase) CAT. Or notice in the
printenv command above that SHELL (and only SHELL) must be all upper
case.
--------------------------------------------------------------------
4.2 Special Characters
The following sections describe a number of characters that have
special meaning to the C shell. Keep in mind that MOST punctuation
characters have some special meaning. Therefore, you should not
include any punctuation character other than a period or underscore
when you name files and directories, or you may get surprising
results.
--------------------------------------------------------------------
4.2.1 Input/Output Redirection (the >, >>, and < Characters)
If a program normally reads its input from the terminal (referred to
as "standard input" or "stdin") or writes its output to the terminal
("standard output" or "stdout"), you may want it to read from, or
write to, an alternate file instead. For example, if you give the
command:
who
the login names of others currently logged in are displayed at your
terminal. If you want to have this list placed into a file called
namelist, you could use the character > to redirect standard output
from your terminal to namelist, like this:
who > namelist
If you already had a file named namelist, the shell truncates it,
then calls "who" to write the new information, destroying anything
already there. If you did not have a file named namelist, the shell
would create a new one before it called the who program.
Now you can do what you want with namelist--print it, sort its
contents, display it with the cat command, or whatever.
If you want to append output to an existing file instead of
overwriting it, you can use the symbol >>. Thus, if you first type:
who > namelist
and later:
who >> namelist
namelist will contain two sets of results; the second set is
appended to the first instead of destroying it.
Normally, you will not need to redirect standard input, as most
commands that can read from standard input also accept an input
filename as an argument. However, for commands that normally expect
input from your terminal, the character < followed by a filename
tells the command to read from that file instead.
The shell performs input and output redirection before it calls the
command you want to execute. This means that your existing output
file could simply be destroyed if your command fails to write to it
for some reason.
--------------------------------------------------------------------
4.2.2 Pipes (the | Character)
A pipeline is a useful way to use the output from one command as
input to another without creating an intermediate file. Suppose
that you want to use the "who" command to create file namelist and
that you want to sort the contents of namelist and see the results
on your terminal. The sort program reads a file and displays the
sorted results on your terminal--the standard output. So you could
give the following sequence of commands:
who > namelist
sort namelist
Afterward, you could give another command to get rid of the file
namelist.
A pipeline enables you to do all this on a single command line,
using the pipe symbol | (vertical bar). When commands are
separated by the | symbol, output from the command on the symbol's
left side becomes input to the one on the right of it. Thus you
could type:
who | sort
so that the results of the who program are passed to the sort
program as input and displayed, by default, on your terminal.
More than one pipe symbol can be used to string a series of commands
together, the standard output from each becoming the standard input
to the next in the series.
NOTE: If a command is not capable of reading from standard input,
it cannot be placed to the right of a pipe symbol.
--------------------------------------------------------------------
4.2.3 Characters Used to Expand File and Directory Names (the * ?
[ ] - and ~ Characters)
The shell also interprets other special characters it finds on a
command line before it passes this line to the program that will
execute it. These characters are normally used in place of
filenames or directory names:
* An asterisk matches any number of characters in a filename,
including none. Thus, the command:
cat a*
would display the file named "a", if it exists, as well as any
file whose name begins with "a". An asterisk will not match a
leading period in a file name, but it will match a period in
any other position.
? The question mark matches any single character. Thus:
cat ? F?
would display all files with single-character names, plus all
files that have a two-character name beginning with F. Like
the asterisk, a question mark does not match a leading period,
but does match a period in any other position.
[ ] Brackets enclose a set of characters, any one of which may
match a single character at that position. For example:
cat draft[125]
displays files draft1, draft2, and draft5 if they exist.
Notice that there is a difference between
cat draft[125]
and
cat draft1 draft2 draft5
The shell considers it a match if any of draft1, draft2, or
draft5 exist. It builds a cat command showing only the names
of files that it actually finds. If there is no draft2, for
example,
cat draft[125]
displays draft1 and draft5. Explicitly giving the command
cat draft1 draft2 draft5
displays draft1 and draft 5, but also gives an error message:
"draft2: no such file or directory". If there were no match at
all (no files beginning with "draft" in our example), the C
shell gives the message: "no match." and does not call any
command.
- A hyphen used within [ ] denotes a range of characters. For
example:
cat draft[1-9]
has the same meaning as, and is shorter to type than:
cat draft[123456789]
Again, the shell expands this to build a cat command that
includes only the filenames that actually exist.
~ A tilde at the beginning of a word expands to the name of your
home directory (the directory in which you are placed when you
log in on UNIX). It is a useful special character if you
changed to some different directory after you logged in, and
want to copy a file to or from your home directory. If you
append another user's login name to the ~ character, it refers
to that user's home directory--useful if you are sharing files
with someone else. For example:
~cssmith
means "the home directory of the user whose login name is
cssmith". The usefulness of this notation will become more
apparent in chapter 5, "Directories and Files".
NOTE: On most UNIX systems, the ~ character has this special
meaning to the C shell only, not to the Bourne shell. At UT
Austin, the Bourne shell on the EMX system also recognizes the
tilde, but in shell scripts, you should use the variable $HOME
to mean your own home directory.
--------------------------------------------------------------------
4.2.4 Action Characters
As already noted in section 2.4, signals the end of input.
If a program is reading data from standard input (your terminal), it
reads everything you type until you use .
Another special control character for the C shell is .
Whenever you type , it suspends the current program. UNIX
responds with the message:
stopped:
and gives a new shell prompt. You can later resume the suspended
program by giving the fg (foreground) command, or resume it in the
background with bg. To kill a suspended program, give the command
"jobs -l" to get the program's job number, then use:
kill %job-no
to terminate it.
If you fail to kill or resume such a process, when you try to log
out you will get the message: "There are stopped jobs". You may
want to disable entirely under UNIX. To make the "suspend"
character undefined, type the following command or place it in your
.login file:
stty susp u
You could also use the stty command to redefine the suspend
character to something else.
--------------------------------------------------------------------
4.2.5 Quotation Characters ( \ and ' )
Although you should not use special characters in filenames and
directory names, there may be a need to include these characters in
command lines without having the shell treat them as "special". To
make the shell's special characters be treated as ordinary
characters, do one of the following:
prefix a single character with backslash \
enclose a sequence of characters in apostrophes ' '
For example, suppose you wanted to use a question mark as your C
shell prompt. If you give the command:
set prompt=?
the prompt will actually be set to the first single-character
filename the shell finds in your directory. The correct form is:
set prompt=\?
--------------------------------------------------------------------
4.2.6 History Substitution Characters (the ! ^ : and $ Characters)
If you use the command:
set history=n
then the C shell keeps a record of your most recent n events, where
an event is one command or one pipeline. You can use special
characters to reissue the commands without retyping them. The
simplest uses of this are:
!! on a line by itself simply reissues the most recent
event.
!cmd recalls the most recent event that started with the
command cmd.
!?string recalls the most recent event that contained string.
!-n recalls the nth previous event. For example, !-1
recalls the immediately preceding event; !-2 recalls
the one before that.
!n recalls command line n. To see how previous comands
are numbered, just type:
history
It will display as many lines as you told "set history"
to keep.
^old^new^ substitutes the string new for the first occurrence of
the string old in the most recent event, and reissues
that command line.
: selects specific words from an event line, so that you
can reissue parts of a command. The : also separates
these words from actions to take on them, such as a
substitute command to correct a misspelled filename.
For example,
!15:s/troff/triff/
would substitute the string "troff" for the string
"triff" in event number 15.
$ the words on each event line are numbered, beginning
with 0 and ending with $. So for the command
cat -n stuff > outfile
the word "cat" is word 0, the "-n" argument is word 1,
and so on. The word "outfile" is word 4 but can also
be designated $. So to build the command
cat -n stuff > test
you could type
!-1:0-3 test
Note that the event (!-1, the previous event) is
separated from the selected words (cat -n stuff >) by a
colon. Then you supply the desired filename, test.
There are other special characters for history substitutions, but
these are the basics.
Examples:
!f77
This reissues the most recent f77 command--say, after you have
corrected a source-file error that caused the previous attempt to
abort.
!!:0-2 !-2:$
This takes words 0, 1 and 2 from the immediately preceding event and
appends to it the last word from the event before that.
--------------------------------------------------------------------
4.2.7 The Command Separator Character ( ; )
It is possible to put more than one command on a line. To do this,
separate complete commands with a semicolon, like this:
cd ~colleague;ls
--------------------------------------------------------------------
4.2.8 Other Special Characters
Besides the special characters discussed in this chapter, there are
some others that have special meaning to the shell and which, if not
quoted in a command line, will not be treated as ordinary
characters. They include:
& ` { } #
Within a program, you should not need to quote special characters to
make them ordinary; it is only when the shell interprets your
command that such characters are expanded instead of being treated
as text.
--------------------------------------------------------------------
4.3 Shell Initialization Files
As noted in section 2.6, every time you log in under the C shell, it
executes any commands that you have in a file named .login. But
logging in is not the only way to start up the C shell. The command
"csh" starts a new copy of the C shell for you if you are already
logged in. If, for example, you logged in under the Bourne shell
and wanted to run certain programs under the C shell, you would give
the csh command to start up the C shell, and then it would interpret
your commands. In this case, since you are not logging in, the C
shell ignores the file .login and executes commands it finds in the
file .cshrc. In fact, when you log in, the C shell also executes
commands in .cshrc--before it executes those in .login.
Until you become quite experienced with UNIX, you should not have
any reason to change to a shell other than the login shell. But if
you later find that you have some reason to start a new copy of the
C shell, be sure to use the file .cshrc for commands you want
executed every time you start a new C shell. In general,
environment variables specified by a setenv command (such as your
"setenv TERM" command) should be in .login. Any "alias" or "set"
commands should be in .cshrc so every new copy of the C shell will
be able to use them. If you do not have a .cshrc file, you can copy
the file /usr/local/lib/Cshrc instead of making a file from scratch.
See sections 5.2 and 5.9 to learn how to do this.
NOTE: The Bourne shell uses just one initialization file--named
.profile.
--------------------------------------------------------------------
4.4 Search Paths
Recall that the shell is itself a program. When the shell examines
and interprets each of your command lines, it determines if the
command is one of its own built-in commands, which it can execute
directly. The command:
set ignoreeof
(described in chapter 2) is one such built-in command that is
executed by the C shell itself.
If the command is not a built-in command, the shell must call some
external program to execute it. To determine where to look for the
needed program, the shell examines a variable called PATH.
Normally, PATH tells the shell to look first in your own current
directory, then in one or more system directories.
To see what your current path is, give the command:
printenv PATH
You will see a sequence of pathnames, separated by colons. The next
chapter describes how to read these pathnames.
You should not have to do anything to create or change the path
variable. But it is useful to be aware of the shell's default
search order in processing input lines: first its own built-in
commands, next files in your current directory, then system files.
Thus, if you should give one of your own executable files the same
name as a built-in command, your own command will never be executed.
The shell will use its own code instead. But if you name your own
executable program file the same as a command external to the shell,
your program will be executed when you give it as a command name.
You should also be aware that most built-in commands do not have
individual man pages. Instead, they are listed within the man page
of the shell itself--"man sh" or "man csh".
5. Directories and Files
--------------------------------------------------------------------
5.1 Directory Hierarchy and Pathnames
When you log in to UNIX, you are placed in a directory that contains
your files. This directory is part of a hierarchy of directories
for the entire UNIX system. Your login directory, known as your
home directory, is on one path down that hierarchy from the top (the
root). Each user's home directory has its own path from the root.
You can create subdirectories below your own home directory. And
you may need to copy files to or from another user's directory. If
so, you must know how to specify pathnames of these directories in
order to get to the files in them.
Pathnames can be either relative or absolute. An absolute pathname
begins with a slash, /, and starts at the root. Each successive
directory name down the path is also preceded by a slash. So the
absolute pathname to the home directory for user "person" might look
like this:
/u1/cs/person
The directory "person" might in turn contain names of directories
below it in the hierarchy, as well as the names of the files in
"person". "Person" is itself under the directory cs, which is under
the directory u1 which is just below the root. That is, the cs
directory is the "parent" of person; files and subdirectories below
person are the "children" of person. This hierarchy is (in part):
(root)
/ | \
/ | \
/ | \
u0 u1
/ \
/ \
cc cs <=== person's parent directory
/ \ / \
/ \ / \
person <=== person's home directory
/ \
/ \
projects papers <=== person's subdirectories
/ \ / \
A relative pathname starts with the directory you are working in and
moves downward to a lower directory. Instead of starting with a
slash, a relative pathname starts with the name of the first
directory below this working directory; each lower directory down
the path is prefixed by a slash.
In both absolute and relative pathnames, the rightmost component is
either the ultimate directory name, or a file in that directory.
For example, suppose "person" has two subdirectories: "projects"
and "papers". If "report1" is a file in "papers" and if "person" is
the working directory, then here is the command to display
"report1":
cat papers/report1
Another user (not "person") who wanted to display the same file
would use an absolute pathname:
cat /u1/cs/person/papers/report1
Remember from section 4.2.3 that ~person is interpreted by the C
shell to mean "the home directory of the user whose login name is
person". Thus, an equivalent command under the C shell would be
simply:
cat ~person/papers/report1
--------------------------------------------------------------------
5.2 Seeing What is in Your Directory
To see the names of any files or lower-level directories you have in
your current working directory, use the ls (list) command. Its
simplest form is:
ls
which lists the names of any files and directories that do not begin
with a period (.). To see all the names, use the -a (all) flag:
ls -a
Even if you have not yet created any files in your directory, you
should see the names ".login" and ".cshrc" (the C shell's
initialization files) and two entries named "." and "..". The
entry named only "." is the directory you are working in, and ".."
identifies its parent, even though these directories have full
pathnames, too. All directories use "." to refer to their own names
and ".." to identify their parents.
To see how big your files are in bytes, and to see their protection
modes, use the -l (long) flag:
ls -l
The protection modes determine what actions you and others may
perform on the files. Section 5.6 describes what these modes mean
and how to change them.
If you want to see directory information about selected files and
lower-level directories, you can use the special characters
described in section 4.2.3. For example, suppose you are "person"
in the hierarchy shown above, working in your home directory
/u1/cs/person. If you give the command:
ls p*
this expands to:
ls papers projects
and lists the names of files in those two subdirectories, except for
ones whose names start with a period.
To list the names of all files in the lower directory "papers", you
might use:
ls -a papers
and get something like:
. .. abstract outline report1 report2 report3
Note that this directory, like your home directory, uses "." to mean
its own name and ".." to identify its parent. So to list the names
of files in the directory above the current one, you could type:
ls ..
If you only need to know which files in "papers" have filenames
beginning with "report", you could use:
ls papers/report*
or
ls papers/report[1-9]
or
ls papers/report?
The last two forms would, of course, not list a file named
"report12".
Note from these examples that the ls command lists files in the
working directory only, unless you include the pathname to another
directory whose filenames you want to list.
--------------------------------------------------------------------
5.3 Naming Files and Directories
Whenever you make new directories below your home directory and
create files in any of your directories, you must give these
directories and files appropriate names. In general, names should
include only letters of the alphabet, digits, period (.) and
underscore (_). As shown in the description of the ls command,
files whose names begin with period do not show in a directory
listing unless you use the -a flag. You might use this leading
period for fairly permanent files that you don't want to clutter up
your normal directory listings. The underscore character is useful
for readability in filenames (e.g. test_scores). Remember that most
other punctuation characters have special meaning to the shell.
Don't use them in filenames or directory names.
A common use of period is to separate a filename into two segments-
-a base and a suffix. Related files might be those with a common
base name and different suffixes, such as:
fortprog.f fortprog.o
or with different base names and matching suffixes, such as:
prog.c newsource.c test.c
Using the period this way enables you to select sets of files for
particular operations, for example by using *.c to select all files
whose suffix is "c".
Certain programs such as compilers use filename suffixes to
determine what action to take when they process files. Examples of
this are in chapter 7.
The only other rule is that no file or directory name can exceed 255
characters and no complete pathname can exceed 1024 characters.
--------------------------------------------------------------------
5.4 Making New Directories and Moving Between Directories
If you have many projects and you want to separate one project's
files from another's, you can create a separate subdirectory to
contain each file set. Such subdirectories are below your home
directory in the hierarchy, and you can make additional
subdirectories below these if needed. To make a new subdirectory
immediately below your home directory, assuming you are working in
your home directory, use the command:
mkdir name
where name is the name of your new subdirectory. Suppose you call
it A. To make another subdirectory, A1, below it, you could use:
mkdir A/A1
specifying the relative pathname from your home directory to the new
subdirectory you are calling A1. Alternatively, you could move from
your home directory to directory A first, then make A1 from there.
By moving to subdirectory A, you make it your "working directory".
To move to A from your home directory, use the cd (change directory)
command:
cd A
Now you can create directory A1 below it by giving simply:
mkdir A1
This concept of "working directory" is very important. Whatever
directory you are currently in is your working directory. Any time
you use a filename with no pathname preceding it, that file is
assumed to be in your working directory. If, for example, you give
the ls command when your home directory is the working directory,
you will see the names of files and subdirectories in your home
directory. File and directory names will be intermixed in the
alphabetic listing. But you won't see the names of any files in
these subdirectories (such as files in subdirectory A of the
preceding example). If you use cd to make A your working directory
and then give an ls command, you'll see names of files in A, but not
names of files in your home directory. Likewise, if you start from
your home directory and type:
cd A/A1
cat xyz
then cat looks only in subdirectory A1, now the working directory,
for file xyz.
When you give the cd command with no arguments, it changes your
working directory to be your home directory.
If you use cd very often to change working directories, you may need
to be reminded where you are. To display the absolute pathname of
your working directory, give the command:
pwd
which means "print working directory".
--------------------------------------------------------------------
5.5 Moving and Renaming Files
To move existing files from one directory to another, use the mv
(move) command:
mv fromname toname
Include the needed path information about the "from" or "to"
directory if it is not your working directory. For example, suppose
you want to move file source.a from your home directory to the next
lower directory A. If A is your working directory, give one of
these commands:
mv ~/source.a source.a
or
mv ../source.a source.a
And give this one if your home directory is the working directory:
mv source.a A/source.a
The mv command can also be used to rename files. For example:
mv xyz abc
simply renames file "xyz" to "abc" in your working directory, since
no directory change is involved.
To prevent destroying a file that already exists, include the -i
flag. For example, if you type:
mv -i source.a oldsource.a
and oldsource.a already exists, mv will prompt you to be sure you
really want to overwrite it.
--------------------------------------------------------------------
5.6 Protecting Files and Directories
Your files and directories are protected from other users' perusal
and destruction by protection mode settings. These settings control
who may read, write, and execute your files. To see what the
settings are, issue the ls command using the -l (long) flag. To the
left of each file or directory name is displayed a sequence of 10
characters, such as:
drwx------
or
-rw-r--r--
The leftmost character is "-" for a filename entry, "d" for a
directory name entry. The next 9 characters are three triplets of
protection settings; the first pertains to you (the owner), the
middle triplet pertains to members of your group (if a group has
been established), and the rightmost pertains to everyone else.
Thus the first line shown here says the entry is a directory for
which you have read (r), write (w), and execute (x) permission and
for which everyone else has no privileges at all. The second entry
is a file. In this case, you may read and write it, but others can
only read it.
To change the protection modes of your files or directories, use the
chmod (change mode) command. It has several forms, but the easiest
to use is:
chmod [who]oppermission names
where names identifies one or more file or directory names. The
whooppermission sequence must be given with no blanks between
portions. The who portion can be any combination of:
u user (owner of the file or directory)
g group members
o others
a all of the above (default if you omit the who argument)
The op is one of:
+ add the designated permission
- remove the designated permission
= replace all existing permissions for the relevant who with
the new ones of this chmod command
and permission is any or all of:
r read: allows a file to be read, provided the directory has x
permission; allows a directory to be listed with filenames
only.
w write: allows a file to be written (altered or replaced);
allows new files to be added to a directory and existing
files to be removed.
x execute: makes a file executable (see chapter 10); for a
directory, allows a detailed listing (if r is also set), and
allows files to be read or written according to their
individual permission settings.
The usual protection for a directory that you want others to be able
to look through is r-x for everybody.
Examples:
chmod u=rx *.prog
permits the owner of all files whose suffix is "prog", in the
working directory, to read and execute these files. If write
privileges had existed for the owner, these privileges are removed.
No one other than the owner is affected, and no protection settings
of any other files are affected.
chmod +r abstract
gives everybody permission to read file "abstract". No other
existing permissions are altered for the file. The directory
containing "abstract" must have x permission as well, if attempts to
read "abstract" are to succeed.
chmod +rx .
gives everyone permission to read and search the working directory.
NOTE: If there is a need to establish a "group", the account
sponsor should arrange this by contacting the UNIX consultants. For
instructional accounts, the instructor is normally in each student's
group, but students are not in each other's group. This allows the
instructor to read students' files, but does not allow students to
read each other's files.
--------------------------------------------------------------------
5.7 Displaying Files in Your Directory
We have already discussed the cat command, which concatenates one or
more files to standard output, your terminal. Its format is simply:
cat file1 [file2] ...
To display files that take up several screens, you may prefer to use
the command:
more file1 [file2] ...
The "more" program pauses after each screenful. At the bottom of
the screen, it shows you what percent of the file has been displayed
so far. To see the next screenful, press the space bar. To see
just one more line, press RETURN. For help on other options, type
h. To quit without seeing the rest, press q.
If you just want to see the end of a file, use the tail program:
tail [-n] file
which displays only the last n lines of the file. The default for n
is 10.
--------------------------------------------------------------------
5.8 Printing Files
To get a printed copy of a file on a line printer, use the lpr
command:
lpr -Psite file
where site identifies the location of the line printer where you
want to pick up your output. To see the names and locations of
available sites, type:
man sites
If the file is PostScript output--as produced by the troff or TeX
and LaTeX text formatters--it cannot be sent to a line printer. (A
PostScript file has %! as its first two characters and contains
special instructions that a line printer cannot interpret.) In this
case, you must send the output to a laser printer. Locations and
site names of the Computation Center's laser printers are also in
the "sites" man page. You should also see
man 7 lpr
for more information about printing PostScript output files. You
can send ordinary files to a laser printer instead of line printer
if you want, but this is much more expensive.
At each output location, output is filed according to the last three
digits of the Computation Center user number.
--------------------------------------------------------------------
5.9 Making Copies of Files
To make a duplicate of a file's contents, use the cp (copy) command:
cp fromfile tofile
or
cp file directory
The second form copies a file to a new directory, keeping its
original filename. The filenames fromfile and tofile should include
appropriate pathnames whenever the files are not in your working
directory. Be sure that they are two different files.
Examples:
cp test backup/test.old
copies file "test" to the next lower directory, named "backup", as
file "test.old".
cp ~csaa123/report report
makes a copy of file "report" from the home directory of user
"csaa123" and places it, with the same name, in your working
directory. Since you are not changing the file's name, just copying
it to the working directory, you could also have typed:
cp ~csaa123/report .
--------------------------------------------------------------------
5.10 Removing Files and Directories
To get rid of files that you no longer want, use the rm (remove)
command:
rm file1 [file2] ...
The files are assumed to be in your working directory if no pathname
is included. If you use any special characters such as * or ? with
the rm command, it is a good precaution to add the -i flag, like
this:
rm -i file*
This interactive option displays, one at a time, each candidate for
removal. If you want to remove the file, type y and press RETURN.
Any other response keeps the file.
Once rm has removed a file, the file is irretrievable, so you might
want to put this command in your .cshrc file:
alias rm 'rm -i'
This will always use the "rm -i" form, even if you just type "rm".
If you have a filename that contains one of the shell's special
characters, rm may not be able to delete it. (A common case is if
you somehow create a filename whose first character is a hyphen.)
In this case, use the "-" flag and put the filename in quotation
marks, like this:
rm - "badname"
To remove a directory that you no longer need, use rmdir:
rmdir path
where path is the relative or absolute pathname of the directory.
The rmdir command will not delete a directory while it is your
working directory, nor will it delete a directory that contains any
files.
--------------------------------------------------------------------
5.11 Disk Quotas
When your user number is validated for one of the Computation
Center's UNIX systems, you are assigned quotas limiting the amount
of disk space you may use. For instructional accounts, each user is
normally assigned a "soft" quota of 1000 kilobytes and a "hard"
quota of 1200 kilobytes. (A kilobyte is actually 1024 bytes, or
characters.) If you exceed your "soft" quota, you receive a warning
message, but you can continue running programs and can log out
without losing any files. However, there are two reasons that you
should take prompt action to remove unneeded files to get below the
soft quota.
The most serious reason is that if you continue using resources, you
may reach the "hard" quota. If you do, you will not be able to run
programs. In fact, a running program will abort if it attempts to
use disk space that exceeds the hard quota.
The second reason is that even if you do stay below the hard quota
but remain over the soft quota for three successive logins, your
work on the third such login will be limited to two actions:
listing filenames with ls and removing files.
To see how much disk space you are using, give the command:
quota -v
or
charges -d
If it is essential that you have a higher disk quota than the one
originally established, contact the UNIX consultants to request the
additional space.
--------------------------------------------------------------------
6. Creating and Altering Files
One way to create new files in your directory, or to alter files you
already have, is to use a text editor, a program written
specifically to perform these tasks. The most commonly used text
editors under UNIX are ed, ex, and vi (pronounced "vee-eye"). On
the Center's UNIX systems, the GNU EMACS screen editor is also
popular. For more information, see "man emacs".
Ed is a line-oriented editor, meaning that commands you give it
operate on whole lines of text. For example, if you ask ed to
search for a particular string of characters, it finds and displays
the entire line containing that string. Ed is not further described
here--for an introduction to ed, read A Tutorial Introduction to the
UNIX Text Editor by Brian W. Kernighan (Murray Hill, N.J.: Bell
Laboratories, 1978). It is part of the Unix User's Manual
Supplementary Documents, available for study in the Reference Room,
WCH 9.
Ex is also a line-oriented editor, but it has an "open" or "visual"
mode so that you can move the cursor anywhere within a line or
screenful of text and make alterations to the text at the cursor
position. When you go into visual mode, you are actually using the
display editor, vi. If you initiate editing with vi, you can
likewise move from visual editing mode into ex.
The following sections offer an elementary introduction to vi and
ex. There are many more ex and vi commands and features than this
chapter presents, but these should be adequate for you to create and
modify files and to learn how these editors work. For complete
information, see the following online documents available through
the doc command described in section 3.3:
viin Introduction to the 'vi' screen editor
vicommands Commands and functions in 'vi'
exrm The 'ex' editor reference manual
exsummary Summary of 'ex' editor commands
--------------------------------------------------------------------
6.1 Vi, The Display Editor
Before using vi, be sure that you have correctly identified your
terminal type to UNIX. Vi uses information associated with your
terminal type when it moves the cursor and scrolls lines of text up
and down. If you have not yet identified the terminal type, as
described in section 2.5, do so before calling vi. Under the C
shell, you can do this by giving the command:
setenv TERM type
where type identifies your terminal, such as vt100, mime2a, or sun.
If you use vi without identifying your terminal type, vi responds
"[Using open mode]". You will be able to use only ex commands, each
prefixed by a colon (:).
NOTE: If your terminal type is not listed in section 2.5 and you
want to use vi, see the file /etc/termcap. This file is a database
of all supported terminal types, but is not designed as a document.
If you can't figure out how to find a valid terminal type in it, use
the following command to get an explanation:
man 5 termcap
To call the vi editor, use the command:
vi name
where name is the name of the file you want to create or modify. If
the file exists in your working directory, vi displays the first
screenful and, at the bottom line of the display, tells you how big
the file is. If vi can't find a file by that name, it tells you the
file is new. To edit an existing file and begin somewhere other
than the first screenful, call vi this way:
vi +n name
This makes line n of the file the initial starting point. It will
appear as the middle line of the first screenful vi displays.
Unless stated otherwise, commands to vi do not need the RETURN key
to terminate them. Most commands you type to vi will not show on
the screen. One exception to this is commands that begin with a
colon (:). When you type such a command, which ends with a RETURN,
it is displayed in the lower left corner of the screen. Such a
command is actually passed to the ex editor, which performs the
command and returns you to vi.
--------------------------------------------------------------------
6.1.1 Controlling the Cursor and Window
The portion of the text that you see displayed on the screen appears
to be in a window that moves forward and back (downward and upward)
over the file. Within that window, you move the cursor to the
position where you want to insert, delete, or replace text. When
you begin editing a file that already exists, the window is the
first screenful, or page, of the file (unless you used an n
argument). The following control characters move the window up and
down:
Moves the window down half a screenful.
Moves the window up half a screenful.
Moves the window forward one full screen (there are
a few lines of overlap for continuity).
Moves the window backward one full screen.
To redraw the current window, use (the letter l).
To move the cursor within the window, use any of the following:
h,j,k,l Moves the cursor one character position left, down,
up, or right, respectively. On some terminals, the
arrow keys may serve the same function.
+ Moves the cursor to the beginning of the next line.
- Moves it to the beginning of the previous line.
RETURN Same as +.
H Moves the cursor to the top (highest line) of the
screen.
L Moves it to the bottom (lowest line) of the screen.
M Moves it to the middle line on the screen.
w Moves the cursor right, to the beginning of the next
word.
b Moves it left, to the beginning of the previous word.
0 (zero) Moves it to the beginning of the current line.
$ Moves it to the end of the current line.
To move the cursor to a position that may be outside the current
window, use one of the following commands:
/stringRETURN
Searches forward for string, centering the line
containing it in the new window. If the search
reaches the end of the file without finding the
string, it "wraps around" to the beginning of the
file and continues searching.
?stringRETURN
Searches backward for string, also wrapping around if
string is not between the starting point and the
beginning of the file.
nG Moves to line n.
G Moves to the last line of the file.
The value of string is "remembered", so /RETURN searches forward for
the most recently named string, and ?RETURN searches backward for
it.
--------------------------------------------------------------------
6.1.2 Inserting Text
Several commands put vi into insert mode, in which everything you
type is inserted into the file. Press the ESC (escape) key to end
insert mode. Here are some of the most useful commands to initiate
insert mode:
i Begins inserting text immediately before the cursor.
a Begins inserting text immediately after the cursor.
o Opens a new line below the current one and enters insert
mode. You may type any number of new lines.
O (uppercase o) Opens a new line above the current one and
enters insert mode.
--------------------------------------------------------------------
6.1.3 Deleting Text
In general, use the d command to delete material. The d command
takes an argument to describe what kind of entity to delete, and it
can be prefixed with a number to indicate the number of repetitions
for this deletion. Typing d and then striking the space bar deletes
the character under the cursor. If you type 5d, then space, you
delete 5 characters--the current one plus the next four. Other
forms are:
dw Deletes from the cursor position through the end of the
word. 4dw deletes 4 words: the current one and the next
three.
dd Deletes the entire current line; 2dd deletes two lines.
To simply delete the character under the cursor, you can also type
x. There is also a D command (capital D). It deletes the rest of
the current line, beginning with the character under the cursor.
--------------------------------------------------------------------
6.1.4 Altering Existing Text
To replace existing text with new text, you could simply use an
appropriate d command, then insert new material at the same point.
Here are some other commands that replace existing text with new
text:
r Followed by a single character, replaces the character
under the cursor with the new character. For example,
rs replaces whatever is under the cursor with an "s".
Rtext Followed by the ESC key, acts as if you had given a
sequence of r commands, one for each character of the
new text. That is, it is a one-for-one character
replacement of any length you choose. For example,
suppose the cursor is on the h of the line:
"This is a funny example."
^
and you give the command:
Rry a non-
where is the ESC key. The line now reads:
"Try a non-funny example."
^
nstext Followed by , substitutes text for the next n
characters, starting with the one under the cursor. A
$ covers the nth character when you begin the
substitution.
ncwtext Followed by , changes the next n words, beginning
at the cursor position, to text. When you start, a $
covers the last character of the nth word to be
replaced.
nStext Followed by , replaces n lines, beginning with the
current one, with text. If you don't give a number,
the entire current line is replaced.
Ctext Followed by , replaces the rest of the current
line (starting with the cursor position) with text.
Notice that, except for the r and R commands, all these commands
delete a fixed amount of material, then put vi into insert mode.
The new text you supply can contain any number of characters or
lines--whatever you type until you press the ESC key.
Two additional commands are useful here. One is the xp (transpose)
command. It interchanges the character under the cursor with the
one to the right of it. The other is the u (undo) command. It
undoes the most recent command that changed the file you are
editing. This is useful for correcting an editing mistake if you
give the u command immediately after you make that mistake. You
cannot "back up" to an earlier change because a second u would only
undo the previous u.
--------------------------------------------------------------------
6.1.5 Moving and Replicating Text
To rearrange lines or groups of lines, use "yank" and "put"
commands:
nyy "yanks" (actually duplicates) n complete lines, beginning
with the current one, and places them in an unnamed buffer
(a temporary storage area).
nyw Yanks n words, starting at the current cursor position, and
places them in the unnamed buffer.
p Puts text from the unnamed buffer after the current line if
the buffer contains full lines, or after the cursor position
if the buffer contains partial lines.
P Like p, but puts text from the unnamed buffer before the
current line or cursor position.
After you have used p or P, the yanked material exists in both its
original and new location in the file. You can now delete it from
its original location. If you need several copies of some original
text, you can yank it once, then use p or P to put it in as many new
places in the file as you need.
IMPORTANT NOTE: There is only one unnamed buffer. Every time you
place text into this buffer, the buffer's previous contents are
destroyed. This unnamed buffer is also used for any piece of text
you delete with an x, d, D (or other) command. This is why the xp
(transpose) command works: it is really an x command followed by p.
If you have several pieces of material to move, you may prefer to
use several named buffers to do this. See the online vi documents
listed at the beginning of this chapter to find out how.
--------------------------------------------------------------------
6.1.6 Saving Your Work and Leaving the Editor
When you are working on a file using vi, you are actually making
changes to a working copy of the file. In order to make the changes
permanent, you must write out the working copy, either replacing the
previous file or making a new file. You can write out the file,
exiting from or remaining in vi. You can also leave vi without
writing out any file, thus discarding any changes you make in this
editing session and preserving the version of the file you started
with. Some of the ways to do these tasks are:
ZZ Exits from vi, saving any changes you made.
:wq Writes the file unconditionally (even if it was
unchanged) and quits.
:w Writes out a new version of the file, replacing the
previous version, but keeps you in vi.
:w name Writes the new version as file name, leaving the
original file untouched, and keeps you in vi.
:q! Quits, discarding all your editing changes.
:e! Discards your editing changes and starts editing the
previous version of the file all over again.
All the forms beginning with a colon (:) must be terminated by the
RETURN key.
NOTE: If you cannot write out your file because you have reached
your "hard" quota, you have two choices.
1. Give the :q! to discard your editing and leave vi.
2. Give shell commands to remove unwanted files without
leaving vi, then write out your work. To do this, you
would typically do the following:
:sh
This gets you to the shell, temporarily setting aside your
vi editing session. Now use:
ls -l
to perform a long-form directory listing and:
rm files
to delete unneeded files. Then use to exit from
the shell and come back to vi. Now write out your edited
file.
-------------------------------------------------------------------
6.1.7 Moving From Vi to Ex
It is possible to move from the vi visual editor to the ex editor
without losing your place in the file and without writing out a
copy of your editing. To do this, use the vi command:
Q
The line containing the cursor becomes the current line for the ex
editor and you can proceed, using ex editing commands. To come
back to vi from ex, give the command:
vi
To give just a single ex command and return to vi automatically,
merely prefix the ex command with a colon (:) and terminate it
with the RETURN key.
-------------------------------------------------------------------
6.2 Ex, The Line Editor
The line-oriented editor ex operates on lines of text. Commands
you give to ex designate in several ways which line or lines are to
be affected. You can give actual line numbers or relative line
numbers, or you can give a pattern, telling ex to operate on lines
that contain an expression that matches the pattern.
Before you use ex, be sure that you have identified your terminal
type correctly to UNIX (see section 2.5). This is especially
important if you move to the vi visual editor from within ex, as vi
uses information about your terminal to adjust the display.
To call ex, give the command:
ex filename
where filename is the name of a file you want to create or edit.
If the file already exists, ex tells you how many lines and
characters are in it, makes its last line the "current" one, and
gives you its command prompt, the colon. If the file is new, ex
tells you that and displays the colon as a prompt. If you want to
edit an existing file and start out somewhere other than the end,
call ex using the command form:
ex +n filename
where n is the line you want to be the initial current line.
-------------------------------------------------------------------
6.2.1 Line Numbers and Ranges
Commands to ex have the general form (but without any embedded
spaces):
linecommandargumentsRETURN
or
rangecommandargumentsRETURN
where line designates a single line to which the command applies
and range is a pair of line designators, separated by commas,
giving the inclusive range of affected lines. Commands are
generally one or two letters. The permissible arguments, of
course, depend on the command. Using the p (print) command as an
example, to print line 5 you could use:
5p
and to print lines 1 through 11:
1,11p
You do not need actual line numbers, however. Here are some other
useful line designators and their meanings:
$ the last line in the file
+ the next line
+n n lines forward (e.g. +5) from the current one
-n n lines backward from the current one
% the entire file, same as the range 1,$
/pattern/ the next line containing pattern
?pattern? the previous line containing pattern
Here are some print commands using these designators:
.,+10p prints the current line and the next 10 lines
-,/format/p prints the previous line and subsequent lines
through the first one of them that contains the
string "format"
.,$-10 prints the current line through the 10th line
before the last one in the file
For most commands, there is a default range if you omit line
designators.
To find out the actual number of the current line, type the
command:
.nu or .=
To find the number of the last line, type:
$nu or $=
-------------------------------------------------------------------
6.2.2 Inserting Text
When ex is in insert mode, everything you type becomes part of your
file until you type a line consisting of just a period. This line
is not placed into your file, but signals ex to leave insert mode
and return to command mode, prompting you with a colon.
Three commands, a, i, and c, place ex in insert mode. The a
(append) command places all text you type AFTER its designated
starting line. Its syntax is:
linea
and the default value for line is the current line. You must use
this command to put text into a new empty file.
The i (insert) command has the syntax:
linei
and places text BEFORE the designated line. You cannot use the i
command if the file is empty. The default line for the i command
is the current line.
The c (change) command accepts a single line or a range of lines
(the default is the current line):
linec or rangec
The c command first deletes the given line or range, then puts you
in insert mode. Here are some examples:
:10a
Here are two lines I'm adding
after the existing tenth line.
.
:
Note that the line containing only "." returned ex to command mode;
it did not become part of your file. If lines 11 and beyond had
already existed, ex would automatically and immediately renumber
them to accommodate the new lines 11 and 12. Another example:
:.,+5c
I didn't like these six
lines, so here are
three new ones.
.
:
Here the current line and the five lines after it are deleted
before ex goes into insert mode. Once again, existing lines are
renumbered as needed.
After you insert text, whether with a, i, or c, the "current" line
is the last one inserted.
-------------------------------------------------------------------
6.2.3 Altering Existing Text
The c command just described is one way to alter text, by replacing
entire lines. Another way is the s (substitute) command:
ranges/old/new/gc
For all lines in the given range that have the character sequence
old, s replaces old with the sequence new.
The g and c at the end mean "global" and "confirm". If you omit
the g, then only the first occurrence of old in each line is
replaced, not every occurrence of old. The c (confirm) option
causes the substitute command to display the line so that you can
confirm or reject the substitution by responding y (yes) or n (no)
for each substitution.
For example, suppose lines 1 through 3 look like this:
All men should refrain from mentioning
that their mentors are really
women, lest they be tormented by other men.
The command:
1,3s/men/persons/
would change the first occurrence of "men" in each line, producing:
All persons should refrain from mentioning
that their personstors are really
wopersons, lest they be tormented by other men.
The command:
1,3s/men/persons/g
would also change "mentioning" to "personstioning" and "by other
men" to "by other persons". But
1,3s/men/persons/gc
would display the lines for each potential substitution, like this:
All men should refrain from mentioning
^^^
then, after you type y to accept that change:
All persons should refrain from mentioning
^^^
and so on.
-------------------------------------------------------------------
6.2.4 Deleting Lines
To remove a single line or range of lines, use the d (delete)
command:
ranged
The default range is the current line. Lines beyond the deleted
ones are automatically renumbered.
-------------------------------------------------------------------
6.2.5 Operating on a Selective Range
The s and d commands, and others as well, operate most effectively
when combined with the g (global) command, not to be confused with
the global option discussed above. The g defines a range to mean
all lines that match a given pattern, not just the next matching
line or previous matching line. Its format is:
rangeg/pattern/command
where the default for range is the entire file. All lines in this
range that have an expression matching pattern are then operated on
by the command. For example:
.,$g/continue/d
deletes every line from the current one through the end if it
contains the expression "continue", whereas:
/continue/d
deletes only the next line that contains "continue".
The command portion of the g command can include a line designator
and arguments. For example,
.,$g/continue/.,.+2w >> outfile
examines each line from the current one to the end. For each line
that contains "continue", that line and the next two lines are
appended to the file "outfile".
The form g! selects lines that DON'T match the pattern:
rangeg!/pattern/command
Note carefully the difference between the global command and the
global option on the substitute command. The global command
selects lines within a range (all lines that match the pattern).
The global option selects all patterns within a line.
-------------------------------------------------------------------
6.2.6 Duplicating and Rearranging Text
To move a range of lines (or just one line) to a different place in
the file, use the m command:
rangemline
where range designates the lines you want to move and line
identifies the line after which they should be placed. For
example:
1,10m20
moves lines 1 through 10 and places them after line 20.
If you want text to remain in its original position as well as the
new one, use co (copy) instead:
rangecoline
As with the m command, co places the contents of range after line
and renumbers lines as needed.
-------------------------------------------------------------------
6.2.7 Correcting Editing Errors
Like vi, ex has an undo command that negates the most recent change
you made to the file. This command does not take any range or
arguments. To negate your most recent change, just type:
u
-------------------------------------------------------------------
6.2.8 Leaving the Editor and Saving Your Work
When you use ex to edit a file, you are actually working on a copy
of that file. The original is unchanged until you write out the
new, edited version to replace it. You can write out the new
version when you leave ex, or at any time during editing. You can
also discard your editing changes, leaving the file as it was when
you called ex. Here are some common ways to write out your file
and to leave ex.
x Exits from ex after writing the new version of the
file over the previous version.
q and q! Quits the editor without writing anything. If you
made any changes during the editing session and
haven't yet saved them, using "q" gives you a
warning message. If you really want to discard
the editing changes and keep the previous version
of the file as is, use the "q!" variant.
rangew Replaces the previous version of the file by
writing the edited range of lines over it.
Default range is 1,$. You remain in ex.
rangew filename Writes the designated range of lines (default:
1,$) to the file you specify, keeping you in ex.
NOTE: If you cannot write out a file because you have reached your
"hard" quota, you have two choices:
1. Use q! to discard the entire editing session and get out
of ex.
2. Temporarily leave ex to remove unwanted files (and get
below quota), then come back to ex and write out your
work. To do this, first give the ex command:
sh
to temporarily set aside your ex session and get to the
shell. Then type:
ls -l
to see your files. Use rm commands to remove unneeded
ones. Finally, to get back to where you were in ex, use:
You should then be able to write out your file.
------------------------------------------------------------------
6.2.9 Moving from Ex to Vi
To make extensive changes within lines of text, you may prefer to
move into the vi editor. To do this without leaving ex, give the
command:
vi
You will move to vi with the cursor positioned on whatever line
was the current one at that point. You can continue editing in vi
or come back to ex by giving vi the command Q.
------------------------------------------------------------------
7. Running Programs on UNIX
This chapter describes the general procedures needed to compile,
load, link, and execute programs written in FORTRAN 77, C, and
Pascal. To run programs written in other languages, see the
appropriate man pages.
------------------------------------------------------------------
7.1 FORTRAN 77 Programs
The FORTRAN 77 compiler on UNIX is f77, which is called with the
command:
f77 [options] file ...
where file is the name of your FORTRAN source file. If the last
two characters of the source filename are ".f" or ".F", the file
is treated as a FORTRAN 77 program. Source files with the ".F"
suffix are passed through the C language preprocessor before f77
compiles them. If the last two characters are ".r", the source
file is treated as a Ratfor source program and transformed to
FORTRAN 77 by a preprocessor before compilation.
In the simplest form of the f77 command, when no options are
specified, the source file is compiled and the object program is
written to a file with the same name but with the ".r", ".F", or
".f" suffix replaced by ".o". This object file is then loaded
into an executable file named a.out. If you have specified more
than one filename, all of the ".o" files are loaded, in order,
into a.out. To execute the program, simply type:
a.out
as a command.
Here are some of the most useful f77 options:
-c Suppresses loading into a.out and merely compiles--
that is, creates ".o" files. (Normally, the ".o"
files would be deleted after a.out is created.) These
".o" files can be used as arguments to subsequent f77
commands.
-g Generates a special symbol table that can be used by
debugging programs (described in section 7.4).
-o outfile Names your executable file outfile instead of a.out.
Note that whenever a compiler makes a new file a.out,
it destroys any previous one.
-U Prevents uppercase characters from being converted to
lowercase ones.
For a complete list of options, see:
man f77
------------------------------------------------------------------
7.2 C Programs
The C compiler on UNIX is cc, which is called with the command:
cc [options] file ...
Like f77, cc gives special interpretations to filename suffixes.
It treats filenames ending in ".c" as C source programs. When it
compiles these programs, it writes object programs to files that
have the same name but with the ".c" suffix replaced by ".o".
Unless options on the cc command prevent it, the ".o" file is
loaded into an executable file named a.out and the ".o" file is
then removed. To execute the resulting program, simply type:
a.out
as a command.
Here are some of the most useful options to cc:
-c Suppresses loading and compiles only: it merely
creates ".o" files (which may be used as input to a
subsequent cc command).
-g Generates a special symbol table that can be used by
debugging programs (see section 7.4).
-o outfile Names the resulting executable file outfile instead of
a.out, thus preventing destruction of any existing
file named a.out.
For a complete list of options, see:
man cc
------------------------------------------------------------------
7.3 Pascal Programs
The Pascal compiler on UNIX is pc, which is called with the
command:
pc [options] file ...
The pc compiler treats any file whose final two characters are
".p" as a Pascal source program. When Pascal compiles a source
program, which may exist as one or several ".p" files, it writes
object code to files with the same name but with ".p" replaced by
".o". These object files are then loaded, in order, into an
executable file whose name is a.out. To execute the program,
simply type:
a.out
as a command.
Here are some useful pc options:
-c Suppresses loading of executable file a.out and
compiles only, producing ".o" files, which can be used
as input to a later pc command.
-g Generates output to be used by debugging programs (see
section 7.5).
-o outfile Names the executable file outfile instead of a.out.
-s Issues warning messages if any nonstandard Pascal
constructs are encountered.
For a complete list of options, see:
man pc
------------------------------------------------------------------
7.4 Getting Listings of Source Code and Error Messages
To get a listing of your source file that includes line numbers,
use the -n flag on the cat command, like this:
cat -n mysource.p | lpr -Pcomlp
To intersperse compiler error messages with source code, you can
use the "error" program. For example, suppose you have a C
program in a file named testrun.c and you compile only:
cc -c testrun.c
If there are errors, the C compiler will write its error messages
to your terminal. It would be more useful to intersperse these
error messages into the source code at the place where they occur,
and the error command does this. For the sample C program, you
could use:
cc -c testrun.c |& error -q
The |& assures that both standard output and error output from cc
will be piped to the error program. The -q option tells the error
program to query you before putting error messages into the file
testrun.c. Its output might look like this:
File "testrun.c" has 3 errors.
3 of these errors can be inserted into the file.
Do you want to preview the errors first?
At this point, if you answer y, you will see just the messages and
be prompted again:
Do you want to touch file "testrun.c"?
If you answer y again, error rewrites testrun.c with each error
message preceding the line that caused the error. You can then
edit the file and try compiling it again.
For more information about how the error program handles messages,
see:
man error
------------------------------------------------------------------
7.5 Debugging Programs
You can use one of the debugging programs to monitor and control
execution of an f77, C, or Pascal program, or to examine the state
of a program whose execution aborted. Depending on which UNIX
system you use, you may have any of the following debugging
programs available to you: dbx, gdb, sdb, cdb, and pdb.
The dbx program is used for debugging all three languages. The
sdb and cdb programs are used to debug f77 and C programs, pdb to
debug Pascal, and gdb to debug C programs.
The common features of these programs are described very generally
here; for complete information, see the appropriate man pages.
If you want to use one of these debugging programs, include the -g
option on the f77, pc, or cc command that compiles your source
program. This option will produce symbol tables that the debugger
can use to locate all source files that the compiler uses in
producing its object files (the ".o" files).
To execute the program under control of the debugger, do not give
"a.out" as a command. Instead, use dbx, gdb, cdb, sdb, or pdb as
appropriate. For example, if you have compiled a Pascal program
on EMX, you could use the command:
pdb a.out
to control its execution. Pdb will give you a prompt, after which
you can perform such commands as setting and removing break
points, stepping through execution, and tracing execution by
printing each statement before it is executed.
These operations are common to all three debugging packages,
although the specific commands that request the operations are not
identical from one debugger to another.
If an executing program aborts, it dumps a core image as a file
that is typically named core. The sdb, pdb and dbx programs can
also be used to examine this core file, in order to determine the
state of the program when it aborted.
------------------------------------------------------------------
7.6 Tools for Maintaining Large Programs
If you write large programs that require libraries of routines, or
programs that consist of many files, there are UNIX tools to help
you maintain them and keep track of modifications.
The ar command maintains groups of files combined into a single
archive file. The ranlib command converts archives to random
libraries for easier loading by the loader.
The "make" program is useful for maintaining and updating groups
of programs in parallel.
The sccs and rcs programs provide mechanisms for tracking
revisions so that you can revert to an earlier version of a
program or maintain multiple versions.
For more information on these programs, see their man pages.
------------------------------------------------------------------
8. Receiving and Sending Mail
When you receive the message "You have mail." or "You have new
mail.", it means another user has sent you a message. To read it,
use the mail program. There are three common ways to use mail.
1. To send a message to someone else, type:
mail username
where username identifies the recipient. Mail prompts
you for a subject line. Type the subject, then the text
of your message. To send the message, type on
a line by itself.
2. To read new messages, simply type:
mail
This examines the "system mailbox" to find your incoming
messages and displays a header line for each. Within
mail, you may display, save, delete, or answer
individual messages.
3. To read messages that you have already seen and saved in
a file, type:
mail -f filename
The following sections discuss these three tasks more fully, but
do not attempt to cover every possible mail command and option.
For complete information, see:
man mail
or
doc Mail | lpr -Psite
If you exchange electronic mail with others who are on other
computer systems, or other computer networks, you may also want a
copy of Usage Note DG-04, Sending and Receiving Network Mail. It
is available free from the consultants in WCH 6.
------------------------------------------------------------------
8.1 Sending Mail to Others
To send a message to another user on the same UNIX system, type:
mail username
where username is the recipient's login name. When you do this,
the mail program prompts you for a subject line. After you type
this subject line and press RETURN, you then continue (without
any more prompts from mail) to type the text of your message.
End the message by typing on a line by itself. At this
point, mail prompts with cc: and you can type the name of any
user who is to receive a copy.
-----------------------------------------------------------------
8.1.1 Multiple Recipients and Nonlocal Recipients
To send a message to multiple users, include their login names,
separated by spaces, on the mail command line. If you regularly
send messages to the same group of people, you can define a
single alias to represent all of them (see section 8.4). If the
recipients of your message are not on the same computer system
you are using, you must designate them as username@host, where
host is the name of the other computer system and username is the
user's name on that system. If you are on bongo and you want to
send a message to "csab123" on that system, as well as to "jones"
on the EMX systems, type:
mail csab123 jones@emx.utexas.edu
The host names of computers operated by the Computation Center
can be found in:
man uthosts
For mail purposes, all of the Center's Sun workstations that are
named for cartoon characters should be treated as having hostname
ccwf.cc.utexas.edu.
NOTE: To send mail to the Computation Center's UNIX consultants,
address your message to "remark".
-----------------------------------------------------------------
8.1.2 Revising a Message Before You Send It
If you start a message and want to change its subject, recipient
list, or content, you can do so by using a feature called "tilde
escape". By typing the tilde character (~) at the beginning of a
line, you signal that the next character is a mail command, not
text. Here are some of the most useful tilde escapes:
~e Calls up the ex text editor to edit the message you
have typed so far. When you leave the editor, you
can type to end the message, or continue
typing message text.
~v Like ~e, but uses the visual editor vi.
~r filename Appends the contents of filename to the message you
have typed so far. You can then continue typing
text.
~m When answering a message, puts the text of that
message into your own, shifting it right by one
tabstop. This is useful for providing the context of
your reply.
~f Like ~m, but without shifting it to the right.
~p Prints at the terminal the message so far, including
its header.
~t names Adds names to the "To:" field.
~s string Replaces the original subject with whatever you type
as string.
~h Edits the entire header of the message line by line.
To leave a line as is, just press RETURN. To change
it, backspace over and replace bad information or
append additional information.
~? Displays a list of tilde escapes.
If, while you are preparing a message, you abort out of mail, the
text you have typed thus far is saved as a file named dead.letter
in your directory. You can revise this file to send later, or
delete it.
-----------------------------------------------------------------
8.2 Reading New Mail
When you want to read your incoming mail messages, just type:
mail
This displays a header line from each of your messages in the
"system mailbox" /usr/spool/mail/username, where username is your
own login name. Each message is numbered and prefixed with a
capital letter, such as N (newly arrived since the last time you
used mail) or U (unread, but not new). Mail prompts for a
command with the ampersand (&). Type a question mark (?) to see
a brief list of the most common commands. Most commands can be
abbreviated to one or two characters. Here are some of the most
useful.
-----------------------------------------------------------------
8.2.1 Reading Messages
To display message number n, type:
type n or t n or n
To display the next message, type:
next or n or +
To display the previous message, type:
- (a hyphen)
To redisplay the current message, type:
. (a period)
-----------------------------------------------------------------
8.2.2 Answering Messages
To reply to message n, type:
reply n or r n
Your response will go to the sender and all other recipients of
message n.
To respond only to the sender, use:
Reply n or R n
Mail prepares the To: field and the Subject: field (using the
original subject preceded by "Re:") and puts you into text input
mode as if you were sending original mail. All tilde escape
commands are available. After you terminate your message with
, you will still be in the mail program.
-----------------------------------------------------------------
8.2.3 Saving Messages
To append a copy of message n to a file, type:
copy n filename or co n filename
This does not delete the message from your message list.
To append a copy of a message to a file and mark it for deletion
from your message list, type:
save n filename or s n filename
Note that both commands append messages to filename, so that you
can build up a file of saved or copied messages--perhaps all
those relating to a single topic.
-----------------------------------------------------------------
8.2.4 Forwarding Messages
The mail program has no command specifically to forward a
message. If you receive a message that you want to forward to
someone else, one way to do this is to save it as a file. Then
do the following:
mail newrecipient@host
Give an appropriate subject, and use
~r file
to insert the saved message. Then use to send it.
-----------------------------------------------------------------
8.2.5 Deleting Messages
To delete a message, type:
delete n or d n
If you change your mind about deleting a message before you leave
the mail program, you can undelete it:
undelete n or u n
-----------------------------------------------------------------
8.2.6 Leaving the Mail Program
There are two ways to leave the mail program. If you use:
quit or q
then any messages that you deleted or that you saved to a file
with the s command will be removed from your system mailbox,
messages that you read but didn't delete will be appended to file
mbox in your home directory, and messages that you did not act on
at all will be sent back to the system mailbox, where their
headers will be redisplayed next time you use mail.
If you leave mail by typing:
exit or x
then all your messages will go back to the system mailbox, even
the ones you deleted or saved.
-----------------------------------------------------------------
8.3 Reading Messages You Have Saved
If you used mail's save or copy command to append messages to
files, you can use mail to examine these files. This gives you a
method of sorting your mail by topic for later perusal. For
example, if during a mail session you saved incoming message 3 to
file project.mail with:
save 3 project.mail
you can later read it, and other messages similarly saved, by
typing:
mail -f project.mail
The only difference between reading mail in project.mail and mail
in the system mailbox is that nothing from project.mail will ever
be moved to file mbox unless you specifically save it there with
mail's save command.
To read mail in mbox, type:
mail -f mbox
or just:
mail -f
NOTE: Mail files contain message separators that could be
destroyed by an editor. So don't use ex or vi or some other
editor on mbox or a saved message file if you want the mail
program to be able to read it.
-----------------------------------------------------------------
8.4 Setting Up Mailing Lists and Other Special Conditions
If you often send mail to the same list of people, you can create
an alias. Suppose your classroom project team is made up of four
members--csab110, csab111, csab112 and csab113. If you make
"team" the alias for these users, then you can type:
mail team
instead of:
mail csab110 csab111 csab112 csab113
To do this, place the command:
alias team csab110 csab111 csab112 csab113
in the file .mailrc in your home directory.
Every time you call mail, whether to read or send messages, mail
reads and executes any commands in .mailrc before it reads from
your terminal. Consequently, you can use .mailrc to customize
the way you use mail. The typical commands for such
customization are "set" and "unset". For example, to signal the
end of a message with period on a line by itself (as in the ex
editor) and not with , you could place these commands in
.mailrc:
set dot
set ignoreeof
If you do not want to be prompted for a cc: recipient when you
send mail, use:
unset askcc
If you don't want long messages to scroll off the screen, use:
set crt=24
This invokes the "more" program when you are reading mail.
You can also set a default editor, or prevent placing aborted
messages in dead.letter. For a complete list of options, see the
mail man page or get a copy of The Mail Reference Manual, USD:7,
from the consultants in WCH 6.
9. Other Useful Commands
-----------------------------------------------------------------
9.1 Determining Your Charges
The charges program displays the resources you have used and an
estimate of their cost. On the EMX system, it can report your
disk usage and the current job's connect time and CPU time. On
the Suns, IX2, and bongo it can also give information about your
accumulated expenditures for computer time and supplies (as of
the previous night's accounting run). See "man charges" to
determine what options are available on the UNIX system you
regularly use.
-----------------------------------------------------------------
9.2 Using finger and chfn to View and Update Personal Information
If you supplied information for the "personal information block"
at the time of your first UNIX login, other users can see this
information when they type:
finger -m username
This information can be useful in reading and sending mail, to
determine who sent you a message if the sender is identified only
by login name, or to ensure that your own messages are directed
to the correct recipient.
If you did not supply information in the first login session, or
if you want to add, delete, or revise information, use the chfn
program:
chfn
When you do this, chfn displays, one line at a time, the
information it already has for you. For example, its first line
might be:
Name [Pat Pending]
To leave the value Pat Pending as is, just press RETURN. To
change it, type the new value and press RETURN. To remove a
value completely, type none and press RETURN. After you have
responded to all the prompts, chfn gives the message "Updating
user information" and exits.
To verify that the revised information is as you intended, use
finger with your own login name.
-----------------------------------------------------------------
9.3 Searching for Pattern Matches in Files
The grep (get regular expression) program is a means of searching
in one or more files for a particular expression or pattern.
There are three variants of the grep program: grep, egrep
(extended grep) and fgrep (fixed-string grep). Only grep and
fgrep are discussed here. See the man pages for a discussion of
egrep.
To use grep or fgrep, give the command:
grep [options] expression [file] ...
or
fgrep [options] [string] [file] ...
If you give no filenames, grep and fgrep search standard input
(your terminal). The grep program expands special characters in
the given expression. The fgrep program does not expand any
characters, but searches for exact matches of the string you
specify. Normally, both grep and fgrep produce a list, on
standard output, containing every line that contains a matching
expression or string from the files searched.
Grep can expand special characters to find matches in much the
way the shell expands special characters in file and directory
names. Below is a simplified explanation of that expansion.
(For a full description of pattern matching, see the ed man
pages. Grep uses the same method of matching regular expressions
as the ed editor does.) The term "special character" here means
the seven characters
\ [ ] . ^ * $
and a delimiter (such as apostrophe) used to mark the beginning
and end of an expression. The delimiters are required if the
expression contains any blanks or special characters, and can be
used even if the expression does not contain such characters.
Any nonspecial character matches itself. Thus:
grep 'man page' intro.draft
searches through the file intro.draft for all occurrences of the
expression "man page".
A period matches any character. So:
grep 'an.' intro.draft
would find "and", "any", "manual", "can", etc. in the file
"intro.draft".
If a set of characters is placed inside square brackets, each one
is considered for matching in that position. Thus:
grep '[mh]an' intro.draft
would match any words containing the sequence "man" or "han", but
not "band", "can" or " and".
You can use the hyphen within square brackets to denote an
inclusive range. Thus:
grep '[a-c]r' intro.draft
would match expressions containing "ar", "br" and "cr".
The character \ turns off the special nature of the character
following it, provided that \ is not within square brackets.
There, \ is an ordinary character. So:
grep 'manual\.' intro.draft
turns off the "special" nature of the period and matches
"manual.", but not "manual ", "manuals", or "manually".
Fgrep does none of this expansion; however, you can search for
more than one string of characters with a single fgrep command.
To do this, place the possible expressions, one per line, in a
file. Then use the option "-f file" instead of the string option
when you call fgrep, like this:
fgrep -f list intro.draft
If file "list" contains:
manual.
manuals
manually
fgrep would find all lines in intro.draft that contain "manual."
or "manuals" or "manually".
Here are some useful options to both grep and fgrep:
-i Ignores case, so that uppercase and lowercase characters
match each other.
-n Displays the line number of each line containing a match
(as well as displaying the line itself).
-l Displays only the names of files that contain matching
lines, but not the lines themselves. This is useful if
you are searching through a set of files to see which of
them contain a particular pattern.
-v Displays lines that don't match a given pattern.
-----------------------------------------------------------------
9.4 Comparing Contents of Files
The diff program is useful in determining how the contents of two
files or directories differ. The simplest way to use it is:
diff file1 file2
This produces, on standard output, a list of lines that must be
changed (c), appended (a), or deleted (d) to make the first file
match the second. Lines from the first file are prefixed by "<"
and lines from the second are prefixed by ">".
If you only want to determine if two files differ, and not how
they differ, you can use cmp:
cmp file1 file2
The cmp program merely notifies you if the files don't match,
stopping its comparison after it encounters the first difference.
-----------------------------------------------------------------
9.5 Transferring Files To and From Other Computer Systems
The Computation Center's UNIX systems, as well as its other
computer systems, have FTP (File Transfer Protocol) programs to
move files from one computer to another. To use FTP, you call
the FTP program, open a connection to the remote computer (remote
host), and within FTP log in on that remote host. The remote
host will be running its own version of FTP as well, with the
local FTP (called the user interface) talking to the remote FTP
(the server). Within this FTP environment you can list the files
in your remote directory, make local copies of remote files, make
remote copies of local files, and delete remote files.
NOTE: It is generally not a good idea to transfer binary
executable files from one computer architecture to another.
Compiled programs will almost certainly not run on the receiving
system without recompilation, so you should transfer source files
of programs instead.
To illustrate a simple FTP session, the following example shows
user csab123 logged in on one of the Sun UNIX systems, using FTP
to get file cprog.data from EMX and naming the Sun copy of it
cdata.sun. User csab123 has the login name jdoe on EMX. What
the user types is shown in boldface {not in this ASCII file}.
Everything else is from the local Sun FTP or, in the case of
numbered responses, remote EMX FTP:
% ftp
ftp> open emx.utexas.edu (1)
Connected to emx.utexas.edu.
220 emx FTP server (Version 4.116 Wed Nov 18 14:28:20 CST 1987) ready.
Name (emx.utexas.edu:csab123): jdoe
331 Password required for jdoe.
Password: xxxxxx (2)
230 User jdoe logged in.
ftp> get cprog.data cdata.sun (3)
200 PORT command successful.
150 Opening data connection for cprog.data (128.83.1.21,2979) (43 bytes).
226 Transfer complete.
local: cdata.sun remote: cprog.data
47 bytes received in 0.016 seconds (2.8 Kbytes/s)
ftp> quit
221 Goodbye.
(1) In the open command, the user gave the full name of the EMX
host, but just "emx" would suffice. For hostnames of other
computers operated by the Computation Center, see:
man uthosts
(2) The password is not actually displayed.
(3) The get command has the syntax:
get remotefile localfile
The remote file is assumed to be in your login directory on the
remote machine. The local copy will be placed in your current
working directory. However, you can give a pathname as part of
either remotefile or localfile to transfer between different
remote directories and local directories to which you have
appropriate access.
Had user csab123 wanted to send, instead of get, a file, the
command would be:
put localfile remotefile
Besides the man page, FTP has internal help that provides terse
explanations of all available commands. Just type:
help
in response to the ftp> prompt. There is also a free FTP Pocket
Reference List, CCRL-33, available from the consulting offices
and most output sites. It describes basic FTP commands on each
of the Computation Center's computer systems.
-----------------------------------------------------------------
9.6 Reading News
Besides the message of the day that is displayed when you log in,
there are also electronic bulletin boards, called newsgroups,
that you can read. A file in your login directory, named
.newsrc, keeps track of the newsgroup names and which messages in
each group you have already seen. The rn program examines your
.newsrc file and displays unread articles in the same way "more"
displays a file--one screenful at a time. To use it, just type:
rn
The first time you run rn, it will create the initial .newsrc
file in your home directory. Then it will present the newsgroups
in turn. For example, for the newsgroup named "system", it might
give the message:
******** 6 unread articles in system -- read now? [ynq]
To see the articles, respond with y; to have rn go to the next
newsgroup, type n; and to get out of rn altogether, type q. For
any group to which you respond y, rn presents the first part of
the first unread article and asks if you want to continue. You
can finish the article, go to the next article, go to the next
newsgroup without reading any more articles from this one, or any
of several other options.
To post a message yourself, use the Pnews program. It prompts
you for necessary information. Before you post anything, you
should read the articles in the newsgroup news.announce.newusers
to see guidelines for what is called net etiquette--common
courtesy to use in posting messages.
The rn program has many more options than can be discussed here-
-selecting articles by subject matter, subscribing and
unsubscribing to newsgroups, saving articles, searching for
patterns within an article. For a complete description, see:
man rn | col | ul -t dumb | lpr -Psite
The man page is too long to make viewing it on your terminal
practical.
-----------------------------------------------------------------
9.7 Text Formatting
The Center's UNIX systems have troff, nroff, TeX, and LaTeX text
formatters. These use embedded commands within a text file to
produce formatted output.
Troff can typeset output for PostScript laser printers and for
the Mergenthaler Linotron 202 typesetter at Texas Student
Publications. It has preprocessors tbl and eqn to format tables
and equations. Troff output cannot be sent to line printers, but
nroff can process a troff source file and ignore such troff
requests as font-size and typeface changes. Nroff's typewriter-
like output can be printed on a line printer.
TeX and LaTeX produce output suitable for PostScript laser
printers but not for line printers.
Descriptions of these formatting languages are outside the scope
of this beginner's manual. To learn more about them, get the
following Usage Notes from the UNIX consultants:
Introduction to UNIX Text Processing, UNIX-01
LaTeX on Computation Center UNIX Machines, UNIX-02
Text Processing on the Suns, UNIX-04
Troff Text Processing at the Computation Center, UNIX-05
TeX on Computation Center UNIX Machines, UNIX-10
-----------------------------------------------------------------
10. Some Basics of Shell Programming
As described in chapter 4, a shell is a command language
interface to the UNIX operating system. But a shell can also be
used as a programming language. Shell scripts can be used to
make a complicated sequence of commands easy to execute or can be
used as a substitute for writing a program in a more conventional
programming language. The Bourne shell is the one most used for
shell programming and it will be described in this section. When
you call a shell script from the C shell, and #!/bin/sh is the
first line of the file, it is the Bourne shell that executes the
script. Note carefully, then, that any shell built-in commands
you use in a shell script must be those for the Bourne shell.
For a description of the Bourne shell, see:
man sh
This chapter does not try to teach you to write shell scripts.
Its purpose is to give you an understanding of what capabilities
the Bourne shell has as a programming language.
-----------------------------------------------------------------
10.1 Running a Shell Script
A shell script is simply a file containing shell commands and is
executed like this:
sh filename [arg1 arg2 ... argn]
A shell script may also be executed by name if the file
containing the shell commands has read and execute permission.
If file "do_it" contains the shell commands and has such
permissions, then the previous example is equivalent to:
do_it [arg1 arg2 ... argn]
In this case, executing a shell script works the same as
executing a program. Remember that its first line should be
#!/bin/sh to be sure the Bourne shell is the command interpreter
that reads the script.
-----------------------------------------------------------------
10.2 Simple Shell Scripts
The simplest shell scripts contain one or more complete commands.
For example, if you wanted to know the number of files in your
current directory, you could use:
ls | wc -l
If you were to create a file called "countfiles" that contained
this line (and with the correct read and execute permissions),
you could count the number of files by typing:
countfiles
Any number of commands can be included in a file to create shell
scripts of any complexity. For more than simple scripts, though,
it is usually necessary to use parameters and make use of special
shell programming commands.
-----------------------------------------------------------------
10.3 Shell Variables
Shell variables are used for storing and manipulating strings.
Shell variables begin with a letter and can contain letters,
digits, and underscores. Examples of legal shell variables
include:
x
x1
abc_xyz
Shell variables can be assigned values like this:
x=file1
x1=/usr/man/man1/sh.1
abc_xyz=4713241
Notice that there are no spaces before or after the equals sign.
The value will be substituted for the shell variable name if the
name is preceded by a $. For example:
echo $x1
would echo:
/usr/man/man1/sh.1
Several special shell variables are predefined. Some useful ones
are $#, $*, $?, and $$.
Arguments can be passed to a shell script. These arguments can
be accessed inside the script by using the shell variables $1,
$2,...,$n for positional parameter 1,2,...,n. The filename of
the shell script itself is $0. The number of such arguments is
$#.
$* is a variable containing all the arguments (except for $0) and
is often used for passing all the arguments to another program or
script.
$? is the exit status of the program most recently executed in
the shell script. Its value is 0 for successful completion.
This variable is useful for error handling (see section 10.6).
$$ is the process id of the executing shell and is useful for
creating unique filenames. For example,
cat $1 $2 >> tempfile.$$
will concatenate the files passed as parameters 1 and 2 and
append the result to a file called tempfile.31264 (assuming the
process id is 31264).
-----------------------------------------------------------------
10.4 Flow Control
Most programming languages provide constructs for looping and for
testing conditions to know when to stop looping. The shell
provides several such flow control constructs, including "if",
"for", and "while".
if
The "if" command performs a conditional branch. It takes the
form:
if command-list1
then
command-list2
else
command-list3
fi
A command-list is one or more commands. You can put more than
one command on a line, but if you do so, separate them by
semicolons. If the last command of command-list1 has exit status
0, then command-list2 is executed. But if the exit status is
nonzero, then command-list3 is executed.
for
The "for" command provides a looping construct of the form:
for shell-variable in word-list
do command-list
done
The shell variable is set to the first word in word-list and then
command-list is executed. The shell variable is then set to the
next word in word-list and the process continues until word-list
is exhausted. A common use of for-loops is to perform several
commands on all (or a subset) of the files in your directory.
For example, to print all the files in your directory, the
following commands could be used:
for i in *
do echo printing file $i
lpr $i
done
In this case, * would expand to a list of all filenames in your
directory, and i would be set to each filename in turn. $i would
then substitute the filename for i (which is passed to the echo
and lpr commands).
while
The "while" command provides a slightly different looping
construct than the "for" command. It is used like this:
while command-list1
do command-list2
done
While the exit status of the last command in command-list1 is 0,
command-list2 is executed.
-----------------------------------------------------------------
10.5 Test
The "test" command can be used to compare two integers, to test
if a file exists or is readable, to determine if two strings are
equal, or to test several other conditions. For example, to test
whether the value of shell variable x is equal to 5, use:
test $x -eq 5
If $x is equal to 5, test returns true.
Other useful tests include:
test -s file (true if file exists and has a size
larger than 0)
test -w file (true if file exists and is
writable)
test -z string (true if the length of string is 0)
test string1 != string2 (true if string1 and string2 are
not identical)
The "test" command is often used with the flow control constructs
described in section 10.4. Here is an example of "test" used
with the "if" command:
if test "$1" = ""
then
echo usage: myname xxxx
exit 1
fi
This tests to see if the command line contains an argument ($1)
and if it does not ($1 is null), prints a message.
A complete list of test operators can be found in the man page
for test.
-----------------------------------------------------------------
10.6 Error Handling
Each time a program is executed from within a shell script, a
value is returned to indicate whether the program ran
successfully or not. In most cases, a value of zero is returned
on successful execution, and a nonzero (usually negative) number
is returned if the program encountered an error. This exit
status is available in the shell variable $?. For example:
grep $1 phonelist
if test $? -ne 0
then
echo I have no phone number for $1
fi
will run a program (grep) and examine the exit status to
determine if the program ran properly.
-----------------------------------------------------------------
10.7 Traps
Some signals cause shell scripts to terminate. The most common
one is the interrupt signal typed while a script is
running. Sometimes a shell script will need to do some cleanup,
such as deleting temporary files, before exiting. The "trap"
command can be used either to ignore signals or to catch them to
perform special processing. For example, to delete all files
called "tmp.*" before quitting when an interrupt signal is
generated, use the command:
trap 'rm tmp.*; exit' 2
The interrupt signal is signal 2, and two commands will be
executed when an interrupt is received (rm tmp.* and exit). You
can make a shell script continue to run after logout by having it
ignore the hangup signal (signal 1). The command:
trap ' ' 1
allows shell procedures to continue after a hangup (logout)
signal.
-----------------------------------------------------------------
10.8 Command Substitution
A useful capability in shell programming is to assign the output
from a program to a shell variable or use it as a pattern in
another command. This is done by enclosing the program call
between accent grave (`) characters. For example, the command:
where=`pwd`
will assign the string describing the current working directory
(the results of the pwd command) to the shell variable "where".
Here is a more complicated example:
for i in `ls -t *.f`
do f77 $i
a.out >output
cat $i output | lpr -P$1
rm a.out output
done
In this case, the shell script executes a series of commands for
each file that ends with ".f" (all FORTRAN programs). The
`ls -t *.f` is executed and expands into all filenames ending
with ".f", sorted by time, most recent to oldest. Each is
compiled and executed. Then the source file and output file are
sent to the line printer identified by the first argument ($1)
passed to the shell script. Then these files are deleted.
-----------------------------------------------------------------
10.9 I/O Redirection
Besides the I/O redirection already described, there are a few
additional forms of redirection. Under the Bourne shell,
standard input is also associated with file descriptor 0,
standard output with file descriptor 1, and standard error output
with file descriptor 2. So >file and 1>file redirect standard
output to file, and 2>file redirects standard error output to
file.
To merge standard output (file descriptor 1) and standard error
output (file descriptor 2), then redirect them to another file,
use this notation:
command >&2 >file
Another method of redirecting input is often used in shell
scripts. This method allows a command to read its input from the
shell script itself without using temporary files. For instance,
to run the editor ed to change all x's in a file to z's would
require creating a temporary file of ed commands, then reading it
to perform those commands, and finally deleting it, like this:
echo "1,$s/x/z/g" >edtmp.$$
echo "w" >>edtmp.$$
echo "q" >>edtmp.$$
ed filename