PROBLEM SOLVER
Shell environment settings that make life easier
by Bob Toxen
Greetings from The Problem Solver. I specialize in finding handy UNIX
solutions to vexing data processing problems -- and I mean to pass them on to
you. Anything from shell scripts to kernel enhancements on any common UNIX
version and hardware is fair game.
This month, my focus is on shell environments, with an emphasis on how you can
specify your terminal type so that programs such as vi and
rogue work correctly. By "shell environments" I mean the
information you give the system regarding the type of terminal you are using,
the place you want your programs kept, the style you want for programs such
as vi and the abbreviations you prefer for commonly used long
commands.
With a bit of effort up front, you can avoid specifying your terminal type each
and every time you login. One way to specify your terminal type is to enter:
% setenv TERM type
(if you typically use the C shell) or:
$ TERM=type
$ export TERM
(if you prefer the Bourne shell) each time you login. (You should, of course,
enter the appropriate termcap description in place of the word
"type".)
This is rather repetitive, though, and is best done automatically. Each time
you login, the shell can automatically invoke commands stored in a file in your
directory called .login if you use csh, or
.profile if you use sh. If such a file does
not already exist, create it and enter the lines given in the example above.
This will work great if you always use the same terminal, but most of
us are not so static! U. C. Berkeley has solved this problem by creating a
database called /etc/ttytype that contains the name of the
terminal connected to each port of the computer. The Berkeley people also
supplied a program to interpret this database called tset.
Almost all UNIX implementations have /etc/ttytype and
tset.
The file /etc/ttytype may be edited with any text editor.
It consists of one entry per line. Each entry has the name of the terminal
followed by the port it's connected to. These items must be separated by a
tab. One can then put a line in .login or
.profile that will allow tset to find the
correct entry in /etc/ttytype and set the appropriate
environment variable TERM. This can be done by entering:
tset -Q -s > /tmp/$$
source /tmp/$$
/bin/rm -f /tmp/$$
in your .login file or by entering:
eval `tset -Q -s`
in your .profile file.
The -s option causes tset not to print the
message, "Erase is Control-H" or "Kill is delete" (these messages may vary
slightly depending on what your erase and kill characters are). The
-Q option causes tset to generate the shell
commands needed to set the
TERM and TERMCAP variables. Since
tset knows which shell you are using, it tailors the
commands it generates to be appropriate.
One must get the shell to read and process these commands. For
csh (the first example) we do this by directing
tset output into the file /tmp/$$ and
ordering csh to read it with the source
command. We then remove the temporary file with the rm
command. The $$ in /tmp/$$ is converted to a
different number (process ID) each time you login and is unique among all
current users, so as to guard against two users accessing the same files
simultaneously.
The Bourne shell tset command shown above is surrounded by
back primes (`) in order to invoke the command inside and supply its output as
parameters to eval, which in turn causes the shell to treat
the parameters as though they were from standard input. In effect, the shell
processes tset output as a command.
There are several things besides setting the terminal type that can be done
automatically by .login or .profile at
login time. For example, the addition of the line:
msgs -f -p
to .login will automatically give you a listing of office
messages when you login under csh. The -f
option causes msgs to give the message "No new messages"
if there aren't any. The -p option causes
msgs to filter long messages through more
so they won't fly off the screen.
Since different terminals have screens holding different numbers of lines
(mine displays 40), more must be informed how many lines it
should display before stopping. You can do this by specifying your terminal
type in .login or .profile. That is why
msgs should be executed only after one's terminal type has
been specified with tset.
Th check news, use the command:
news -n
to list all current news items. Certain terminal characteristics such as the
erase and kill (line delete) characters may be set with stty
in .login or .profile. To set your erase
character to CTRL-H and your kill character to CTRL-U, give the command:
stty erase ^H kill ^U
In some UNIX implementations, one can enter a ^ character followed by an H
character. In others, you have to enter an actual CTRL-H (by holding down the
CTRL key and pressing H). If you have to enter an actual CTRL-H, you will
probably have to precede it with a backslash (\). Likewise for CTRL-U. If you
are using vi to edit, you can precede either CTRL-H or
CTRL-U (or any other special character) with a CTRL-V to be certain it is
entered correctly.
Another common command to issue is the umask command. This
will determine the access permissions of any files you subsequently create. To
allow anyone to read, write or execute your programs and shell scripts, issue
the command:
umask 000
To prevent others from writing to your files, enter the command:
umask 022
The command:
umask 077
will prevent others from accessing any file you subsequently create in any
way whatsoever.
There are also a number of environment variables, like our friend
TERM, that can be set so as to save you legwork when writing
programs. One such variable is EXINIT, which can be set to
make ex and vi act according to your
preferences. For example, a C programmer at a company called Silicon Graphics
might want to give the command:
setenv EXINIT 'set ai aw|ab sg Silicon Graphics'
if she is using csh and:
EXINIT='set ai aw|ab sg Silicon Graphics'
export EXINIT
if she is using sh. This will set
auto indent so that when she enters program
source, the current indent level will be maintained (although she can also
reduce it by entering CTRL-D on a new line). This entry will also set
auto write mode to automatically write out an
edited file whenever she wants to start editing another file. Finally, it will
also set up sg to be an abbreviation for
Silicon Graphics that will be expanded during text entry when
followed by a space, punctuation or carriage return (this feature is only
available on System III vi and later versions).
Rogue players may set their desired options by defining an
environment variable called ROGUEOPTS. I use:
setenv ROGUEOPTS "fruit Orange, \
jump, askme, flush, name Bobby"
USEFUL COMMAND ABBREVIATIONS
The C shell allows one to create abbreviations for commonly used long
commands (this is also possible under sh in System V.2,
though with different syntax). Some abbreviations are useful to almost
everyone and some are useful only in particular applications. The C shell has
a history capability that can remember recently invoked
commands. This history list can be displayed and particular commands can be
re-invoked (even with changes). To set history to keep track
of the most recent 20 commands, give the command:
set history=20
This can be put in one's .cshrc file (or entered at the
keyboard). It is also useful to give the command:
alias h history
to create an abbreviation for the history command itself.
Thus to have csh display the last 20 commands invoked, you
can enter:
h
Each command is numbered, so to re-invoke any of them you can simply refer to
the desired one by preceding the appropriate number with a ! character such as:
!17
To avoid having to issue the history (or h)
command frequently, you can have csh put the
history number into the prompt itself by issuing the
command:
set prompt='\!% '
This will cause the screen to look like this:
1% who
2% mail its!jim
What time is the party?
-Bob
^D
3% vi mousetrap.c
<editing stuff>
4% cc -o mousetrap mousetrap.c -lgl -lm
<compiler errors>
5% !3
<more editing stuff>
6% !4
<more compiler errors>
...
Still another feature that can be utilized is the terminal bell. Since I
can often do desk work during long compiles, I have set up my environment
such that my terminal's bell is rung when my compile is done. I've done
this by putting the line:
alias G echo ^G
in my .cshrc file and when I want to compile a program, I'll
enter the command:
cc -o mousetrap mousetrap.c -lgl -lm ; G
to cause a CTRL-G to be sent to my terminal when the cc is
done. Printing a CTRL-G on almost any terminal will cause its bell to be rung.
I'll cover the history facility, including editing, in more
depth in a later issue.
Another useful feature to put into your .login and
.profile file is the command:
set mail=(60 /usr/spool/mail/name)
(substituting your login name for "name," of course).
This will cause csh to check for new mail belonging to you
every 60 seconds. System III and System V users, however, should use the
command:
set mail=(60 /usr/mail/name)
When one logs out, csh will invoke any commands stored in a
file called .logout. One common set of commands is:
/usr/games/fortune -w
clear
The first command delivers a random fortune message at logout while the
-w flag causes fortune to wait for you to
read the message before finishing (using the ESP feature).
The clear command will then clear the screen in case you
have displayed confidential information (or in case you also gave the
-a or -o flags to
fortune ). The clear program and the
enhanced fortune program are from Berkeley.
Readers are encouraged to write in with questions and suggestions to:
Bob Toxen
c/o ITS
520 Waller Street
San Francisco, CA 94117
ucbvax!Shasta!olympus!bob
Bob Toxen is a member of the technical staff at Silicon Graphics, Inc.
He has gained a reputation as a leading uucp expert and is
responsible for ports of System V for the Zilog 8000 and System III for the
Motorola 68000.
Copyright © 1984, 2006, 2007, 2014, 2020 Robert M. Toxen. All rights reserved.
Back
|