Sie sind auf Seite 1von 434

UNIX Part I

UNIX I

Welcome to
UNIX

ECT001118
Rev 2
10/1/99
Sect. 1

Bruce Hunter 2/21/01 page 1


UNIX Part I

Welcome to UNIX Part I

This is an interactive course to get you started in contemporary networked


UNIX. The material is in separate modules and contains sections on

What is UNIX?

Logging In

The UNIX File System

The Shells

vi

Devices (Terminals, Printers, ...)

mail (E-Mail)

Security

The UNIX Network

The X-Windows System

EC

SAMBA

eXceed

Each section will build on the one proceeding. For some, it will be all you
ever need to know to get started in UNIX. For most, it is the stepping stone
to UNIX Part II and on to programming in, C and PERL.

The ultimate goal of this course and the other courses in the UNIX and pro-
gramming series is to increase your knowledge and therefore your produc-
tivity in UNIX.

Bruce Hunter 2/21/01 page 2


UNIX Part I

Productivity

UNIX is a productivity system. Intel is a productivity company that makes


productivity products. It only makes sense that they go and in hand.

In each of the 14 parts of this class attention will be given to the areas that
increase your productivity.

Learning the UNIX power tools

Semi-automated report generation

Command chaining

Task automation by scripting

Faster programming cycles by using X Windows and history

Saving typing through the use of

aliases

history

command-line editing

abbriviation expansion in vi

vi shortcuts

Bruce Hunter 2/21/01 page 3


UNIX Part I

Prerequisites

There are no prerequisites for UNIX part but being comfortable with the
keyboard and having knowledge of any operating system is. The material is
comprehensive, the pace fast. Feel free to ask questions at any time.

You will have frequent quizzes, exercises, and labs. They are all geared to
fix the material in you mind and give you the opportunity to try and familiar-
ize yourself with the commands and files.

Bruce Hunter 2/21/01 page 4


UNIX Part I

Conventions

I have used the following type-font conventions to make reading the text
easier.

Courier bold is used for command names, screen output, screen frag-
ments like the “ed editor.”

Italic is used the first time a name is used, for emphasis, and for “fuzzy”
words such as wild cards like /pattern/. It is also used for special key
names like enter for the

Enter

enter key.

Bruce Hunter 2/21/01 page 5


UNIX Part I
What is UNIX?

What
is
UNIX?
Welcome to
UNIX

Section 2
Rev 7/12/99

Bruce Hunter 2/21/01 page 6


UNIX Part I
What is UNIX?
What is UNIX?

UNIX is an operating system that is

interactive (not batch)

multiprocess

threaded (multi-processor)

multi-user

multi- (general) purpose

open source

portable

secure

networked

Today UNIX interfaces with NT and shares files through software like
Samba and Exceed.

Bruce Hunter 2/21/01 page 7


UNIX Part I
What is UNIX?
The purpose of this section is to show you some of the major concepts of
UNIX and expose you to a few of the commands.

Flags and Commands

Boxes show the flags (options) of


the commands for the curious who
want to know about them before
they are introduced.

Bruce Hunter 2/21/01 page 8


UNIX Part I
What is UNIX?
The Kernel

The Kernel

Drivers

Hardware

The heart of any operating system is the hardware. UNIX surrounds it with
the device drivers. The kernel cannot be contacted directly. It controls the
drivers, the bus or channels, and therefore the hardware. Here’s another
view.

Bruce Hunter 2/21/01 page 9


UNIX Part I
What is UNIX?

system calls
file systems

front ends
processes control

driver
scheduler
memory management
driver bottom ends

Here is another view of UNIX. System calls are the primitives that allow
higher-level activities to communicate, programmatically, with the kernel.
The scheduler schedules jobs to run on a priority basis. It also sets and
resets the priorities. Each time a job gets a time slice and runs its priorities
are reset.

UNIX memory is complex. It consists of high-speed storage (DRAM) and


rotating storage (the swap disk). Memory is increased as needed by paging.
Memory is freed, when necessary by paging out a job in high-speed mem-
ory to the swap disk.

The I/O systems (buffered and raw) are managed by the kernel and all
device input and output is handled by device drivers.

Bruce Hunter 2/21/01 page 10


UNIX Part I
What is UNIX?
Kernel Tasks

scheduling

initiation states

swapping

memory allocation

text

stack

heap

file system components

devices

inodes

directories

mounts

process control

system primitives (system calls)

interrupt handling

Bruce Hunter 2/21/01 page 11


UNIX Part I
What is UNIX?
The Shell, File Systems, and Utilities

The Shell

Net-
File The Kernel work
System

Utilities

The shell is the user’s interface to the kernel and, therefore, UNIX. Also sur-
rounding the kernel are the hundreds of utilities and tools, the network, and
the file systems.

Bruce Hunter 2/21/01 page 12


UNIX Part I
What is UNIX?
The UNIX Family Tree

UNIX has a long history, starting from MULTIX and beginning at Bell Labs
"UNIX" is now owned by SCO but free unix like Linux is dominating.

Bell Labs ‘69


Ken Thompson
Dennis Ritchie

‘76
V6
BSD
‘79
V7

4.2 83 4.3 86
XENIX
SVR2 ‘80
ULTRIX SunOS
‘82

SVR4 SCO ‘90


‘89 ODT
mach
Solaris ‘90
‘93
POSIX
‘82-
Linux OSF/1
AIX
‘90
‘88

Bruce Hunter 2/21/01 page 13


UNIX Part I
What is UNIX?
Major UNIX Concepts

A high-level, portable kernel

The shell (as a command interpreter) and shell forking

The hierarchical file system

Data stream, STDIN, STDOUT, STDERR, pipes, redirection, and filters

The UNIX environment, argv and envp

C (90% of kernel in C, 100% of utilities on C or sh)

lex and yacc (parsing and lexical analysis)

Universal screen and terminal control (termcap, curses, termio, X)

Security (login process, passwd, permissions, groups, ...)

Built-in mail

Process control (BSD C-shell, and ksh)

Built-in networking

NFS

The X-Windows System

YP

DNS

Mosaic -> Apachy -> Netscape

Free and open software

Bruce Hunter 2/21/01 page 14


UNIX Part I
What is UNIX?
The UNIX File System

UNIX arranges its files in a tree hierarchy, like an inverted tree with its root
at the top. The base of every mounted file system is its root. File trees are
mountable.

/ (root)

/bin
/var
/usr mount
point
/spool
/include

/lp /mail
mount path

leaf
/ node

/sys

/errno.h

Bruce Hunter 2/21/01 page 15


UNIX Part I
What is UNIX?
Here is a partial UNIX file tree.

/dev /bin /tmp /var /etc /unix


link
/spool
/mail
/usr

/ucb /lib /include /src


/bin
/man
/mail
/mail /ls
/man1
ls.1
mail.1

The following commands are file and directory


related.

ls see the directory contents

pwd where am I?

cd change directories

Bruce Hunter 2/21/01 page 16


UNIX Part I
What is UNIX?
Quiz

UNIX is open, portable ... What else?

Is UNIX stateless or stateful?

Describe its file system?

Scheduling, process control, interrupt handling etc. are ___ tasks.

What is the name of the base of every unmounted UNIX file system?

Who owns “UNIX”?

How about unix?

Who created C? UNIX?

When?

Bruce Hunter 2/21/01 page 17


UNIX Part I
What is UNIX?
The Data Stream
UNIX treats its data stream in a special way. Every UNIX command has
three file streams built in:

STDIN Standard input, normally the keyboard

STDOUT Standard output, normally the screen

STDERR Error output, also normally the screen

stdin stream 0

stdout stream 1

stderr stream 2

a.out

Bruce Hunter 2/21/01 page 18


UNIX Part I
What is UNIX?
Pipes
What if you could connect one command, program, or script to another?

stdin STDIN

who wc -l
STDOUT stdout

stderr stderr

You can and the mechanism is called a pipe. Its symbol is |.

304% who|wc -l
3

Bruce Hunter 2/21/01 page 19


UNIX Part I
What is UNIX?
Redirection

Extending the concept of a pipe, you can redirect the data stream into a file
or a device.

306% who>who.file
307% ls -l who.file
-rw-r--r-- 1 bhunter system 189 Feb 01 14:55
who.file
308% cat who.file
bhunter hft/0 Jan 21 07:32
bhunter pts/1 Jan 21 07:32
bhunter pts/0 Jan 21 07:32
309%

You can also redirect the error stream, STDERR.

309% sh
$ wc foobarsnarg 2> error.file
$
$ cat error.file
wc: 0653-755 Cannot open foobarsnarg.
$

Note the first line. The user has switched shells.

Bruce Hunter 2/21/01 page 20


UNIX Part I
What is UNIX?
You can also redirect into a command from a file.

310% wc -l<who.file
3

A redirection to a file wipes out its former contents. You can save the con-
tents and append to them with the >> symbol for redirect with append.

315% who>who.file
316% echo fmsu04>>who.file
317% rsh fmsu04 who>>who.file
318% cat who.file
bhunter hft/0 Jan 21 07:32
bhunter pts/1 Jan 21 07:32
bhunter pts/0 Jan 21 07:32
fmsu04
shilo ttyp0 Jan 31 16:57 (galadriel)
gsady ttyp3 Jan 28 16:09 (gaffa)
319%

Redirection can be used to empty an existing file or create a new (empty)


file.

321% sh
$ >who.file
$ wc -w who.file
0 who.file
$

Note that occasionally we have shifted from C shell (%) to Bourne shell($).
Borne shell does things with the data stream that C shell will not.

Bruce Hunter 2/21/01 page 21


UNIX Part I
What is UNIX?
The Shell

The UNIX shell is a major concept. The second process created by the ker-
nel as the system comes up is init (the first is the swapper.) init
becomes mother to each log-on shell as each user logs on to the system.

init (PID 1)
getty

login password

shell

When you execute a command your shell creates (forks) another shell just
like it. Only its process ID (PID) number is different.

sh PID 429 (parent)

fork(2)

sh PID 430 mail(1)


(child) exec(2)

Bruce Hunter 2/21/01 page 22


UNIX Part I
What is UNIX?
When the command executes, it dies on exit..

sh PID 429 (parent)

sh PID 430 mail(1)


(child)

Actually there is a little more if you consider the systems calls that the kernel
has to make. They are all a part of a process.

sleep
sh wait sh

SIGCHLD
fork

exec exit

Bruce Hunter 2/21/01 page 23


UNIX Part I
What is UNIX?
Quiz

What is init?

Describe how a shell works with a process.

How many data are streams in a standard shell or process? What are they?

Stream 0

Stream 1

Stream 2

How do you get a data stream to a file?

How do you link commands into a larger command?

Bruce Hunter 2/21/01 page 24


UNIX Part I
What is UNIX?
Memory Allocation

UNIX executables (processes) have three areas of memory.

Heap (dynamic storage)

Stack

Text (intructions and data)

The text area is fixed; it cannot grow. The stack grows up. The heap grows
down.

Bruce Hunter 2/21/01 page 25


UNIX Part I
What is UNIX?
Process Table

The kernel has been described as a maze of tables held together by end-
less code. The scores of tables keep track of what is happening in the ker-
nel and out. The kernel watches and governs everything: files, mounts,
processes, memory, and so on. Many of these kernel tables can be format-
ted by UNIX commands. One of these, ps, formats the process table. The
first processes you see are started by rc when the system boots.

374% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 12:30 /etc/init
root 1721 3095 0 Jan 16 - 0:00 /etc/writesrv
root 2032 1 0 Jan 16 - 4:22 /etc/syncd 60
root 2740 1 0 Jan 16 - 5:09 /etc/cron
root 3095 1 0 Jan 16 - 0:00 /etc/srcmstr
root 3570 1 0 Jan 16 - 0:00 /usr/lib/errdemon
root 3616 3095 0 Jan 31 - 0:01 /etc/syslogd
root 4644 3095 0 Jan 16 - 0:00/usr/lib/sendmail -bd
root 5050 1 0 Jan 16 - 0:00 /etc/uprintfd
root 5163 3095 0 Jan 16 - 1:24 /usr/etc/portmap
root 5422 3095 0 Jan 16 - 0:00 /etc/inetd
root 5683 3095 0 Jan 16 - 1:14 /usr/sbin/snmpd
root 6071 3095 0 Jan 16 - 0:11 /etc/qdaemon
root 6206 3095 0 Jan 16 - 0:14 /usr/etc/ypbind
root 6465 3095 0 Jan 16 - 0:05 /usr/etc/biod 6
root 6724 6465 0 Jan 16 - 0:05 /usr/etc/biod 6
root 6981 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 7238 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 7495 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 7752 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 8010 3095 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8267 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8524 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8781 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9038 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9295 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9552 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9809 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 10067 3095 0 Jan 16 - 0:00 /usr/etc/rpc.mountd
root 10326 3095 0 Jan 16 - 0:00 /usr/etc/rpc.statd

Bruce Hunter 2/21/01 page 26


UNIX Part I
What is UNIX?
Other process belong to the user(s):

bhunter 12370 14663 0 Jan 21 - 0:00 aixterm -e xrlogin


forever
bhunter 12642 17498 0 Jan 21 pts/0 0:00 -sh
bhunter 12911 1 0 Jan 21 hft/0 1:09 xterm
root 13260 3095 0 Jan 16 - 0:00 /usr/lpp/info/bin/
infod
bhunter 13444 14979 0 Jan 25 pts/2 0:26 tee /usr/system/
bhunter/fmconsole
.log
bhunter 13649 14663 0 Jan 21 hft/0 0:14 xclock
root 13825 1 0 Jan 16 - 3:21 loadavgd 2112
root 14301 1 0 Jan 16 - 0:00 pld -s:7 -L:/usr/
spool/pld_jeager
.log
bhunter 14485 18836 0 Jan 25 pts/5 0:01 -sh
bhunter 14663 15160 0 Jan 21 hft/0 0:00 /bin/csh /usr/sys-
tem/bhunter/.xin
itrc.aix
bhunter 14979 1 0 Jan 25 pts/2 0:00 sh /usr/local/frame/
3.0x/bin/scri
pts/maker
bhunter 15160 15961 0 Jan 21 hft/0 0:00 csh -f /usr/local/
bin/x11
root 15863 1 0 Jan 16 - 18:24 /usr/local/bin/
laserv
bhunter 15961 1 0 Jan 20 hft/0 0:02 -csh
bhunter 16257 19584 0 Jan 21 pts/3 0:00 -sh
root 16779 1 0 Jan 27 - 2:06 /usr/local/bin/gar-
con.out-4.0 -p
-l
bhunter 16985 14663 0 Jan 21 hft/0 13:24 xautolock
bhunter 17216 1 6 Jan 21 hft/0 5721:48 /usr/bin/X11/X -
n :0 -T -a 3
bhunter 17498 14663 0 Jan 21 - 0:00 aixterm -e /usr/
system/mcdcad/bin
/console.aix
bhunter 17777 12911 0 Jan 21 pts/2 0:00 -sh
bhunter 18115 19138 0 08:21:03 pts/4 0:00 rlogin fmsu04
bhunter 18369 1 0 08:21:03 pts/1 0:00 xterm -e rlogin
fmsu04
bhunter 18531 12370 0 Jan 21 pts/1 0:00 sh /usr/local/X11/
bin/xrlogin for
ever

Bruce Hunter 2/21/01 page 27


UNIX Part I
What is UNIX?
bhunter 18836 1 1 Jan 25 hft/0 0:51 xterm
bhunter 19138 18369 0 08:21:03 pts/4 0:00 rlogin fmsu04
bhunter 19333 13444 0 Jan 25 pts/2 75:41 /usr/local/frame/
3.0x/bin/bin.ibm
.rs6000/maker
bhunter 19584 1 0 Jan 21 hft/0 0:23 xterm
bhunter 21592 14663 0 Jan 21 hft/0 1:26 mwm
bhunter 21761 14485 6 09:35:19 pts/5 0:00 ps -ef
375%

The ps command
Any time you need to look at the process
table execute ps from the shell.

Bruce Hunter 2/21/01 page 28


UNIX Part I
What is UNIX?
A long list of the process table shows the parent process id (PPID) as well.

376% ps -efl
F S USER PID PPID C PRI NI ADDR SZ WCHAN STIME TTY
TIME CMD
202803 S root 1 0 0 60 20 1004 256 Jan 16 - 1
2:30 /etc/init
240801 S root 1721 3095 0 60 20 45b1 120 1f63cc Jan 16 -
0:00 /etc/writesrv
.
.
bhunter 15160 15961 0 Jan 21 hft/0 0:00 csh -f /usr/local/bin/x11
root 15863 1 0 Jan 16 - 18:24 /usr/local/bin/laserv
bhunter 15961 1 0 Jan 20 hft/0 0:02 -csh
bhunter 16257 19584 0 Jan 21 pts/3 0:00 -sh

Note the two shells have have a PPID of 1.

Lab

Create a child shell. Run ps and find it and its parent.

ps flags

ps -ax System V, POSIX, AIX, HPUX . ...

ps -ef BSD, SunOS, ULTRIX,

These will show most of the process-table information.

Bruce Hunter 2/21/01 page 29


UNIX Part I
What is UNIX?
Filters

The UNIX analogy of pipes and streams is a hydraulic analogy. If data flows
through a stream and you can pipe it, why can’t you filter it? So UNIX has fil-
ters to alter or filter out data. Some of them are

grep

sed

sort

tr

awk

cut

You will take just a brief look at a few filters now and look at them again in
depth later.

Regular Expressions

A regular expression, or RE, is a string that represents any string that it will
match. The RE /foo/ will match foo in foobar or in foofoo. Regular
expressions can be very complex.

/^[a-z]+:[^:]+:[0-9]+:[0-9]+:[a-z ]+:[a-z/]:/bin\/
c?sh$/

Obviously this will need a lot of explanation.

Bruce Hunter 2/21/01 page 30


UNIX Part I
What is UNIX?
grep

In the primitive UNIX editor, ed, you can do a global search on a regular
expression and have it print.
g/RE/p

This was used so often that it became the parent of grep. grep will filter out
any line that contains the pattern you are looking for:

389% cat foo


1
22 grep flags
333
4444 -v "I dont want to hear
55555 about it!"
666666
7777777 -s work silent
88888888
999999999 -n give line numbers
end
390% grep end foo -c count lines
end

Note that it got the whole line, tabs and all. Now use it in a pipe.

391% ps -ef |grep sh


bhunter 12133 10852 0 Jan 28 pts/6 0:00 -sh
bhunter 12642 17498 0 Jan 21 pts/0 0:00 -sh
bhunter 14485 18836 0 Jan 25 pts/5 0:01 -sh
bhunter 15160 15961 0 Jan 21 hft/0 0:00 csh -f
/usr/local/bin/x11
bhunter 15961 1 0 Jan 20 hft/0 0:02 -csh
bhunter 16257 19584 0 Jan 21 pts/3 0:00 -sh
bhunter 17777 12911 0 Jan 21 pts/2 0:00 -sh
bhunter 22346 14485 2 10:34:09 pts/5 0:00 grep sh
392%

Bruce Hunter 2/21/01 page 31


UNIX Part I
What is UNIX?
sed

sed is an editor made from the ed editor. It is a streaming editor and it will
edit the data stream.

The df command puts out scores of lines that you don’ t want to see:

fmsu01:/28u008sv 1836252 1017586 44% - - /vproj/28u008s


fmsu01:/training/mproj 192383 168156 12% - - /mproj/training
fmsu01:/training/uproj 192383 168156 12% - - /uproj/training
fmsu01:/p42c 1312925 444590 66% - - /vproj/p42c
fmsu05:/28f008sm 917862 600054 34% - - /mproj/28f008s
fmsu05:/28f008su 917870 826071 10% - - /uproj/28f008s
fmsu01:/28f008sg 917870 826071 10% - - /gproj/28f008s
fmsu05:/f2f4gr 458671 412791 10% - - /gproj/f2f4
fmsu01:/corsairm 945902 811846 14% - - /mproj/corsair

To get rid of them use sed to delete.

396% df | sed ’/^fmsu0.*/d’


Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 924 77% 865 84% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1364 88% 346 8% /var
/dev/hd3 8192 7880 3% 32 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
397%

You will see sed again in the vi section of the course.

Bruce Hunter 2/21/01 page 32


UNIX Part I
What is UNIX?
You can use sed to substitute.

402% cat /etc/motd


******************************************************************************
* *
* *
* Welcome to IBM AIX Version 3.2! *
* *
* *
* Please see the README file in /usr/lpp/bos for information pertinent to *
* this release of the AIX Operating System. *
* *
* *
******************************************************************************

404% sed ’s/AIX/UNIX/’ motd


******************************************************************************
* *
* *
* Welcome to IBM UNIX Version 3.2! *
* *
* *
* Please see the README file in /usr/lpp/bos for information pertinent to *
* this release of the UNIX Operating System. *
* *
* *
******************************************************************************
405%

sed sub commands

s substitute

p print

g global

d delete

Flag

-n run siltent

Bruce Hunter 2/21/01 page 33


UNIX Part I
What is UNIX?
awk

awk is a pattern matching language as well as a filter. It understands


records and fields and is particularly useful for getting at a particular field.
Here awk is used in conjunction with grep and ypcat to get a particular
user’s $HOME.

407% ypcat passwd |grep bhunter


bhunter:rzojv8/Uf2q92:100:0:Bruce H Hunter:\
/usr/system/bhunter:/bin/csh
410% ypcat passwd |grep bhunter|awk -F: ’{ print $6 }’
/usr/system/bhunter
411%

awk can pattern match, then even better than grep it can perform actions.

421% ypcat passwd | awk -F: ’/hunter/ { print $6 }’


/usr/system/bhunter
422%

awk Flags

-F field delimiter

-f take commands from file

Bruce Hunter 2/21/01 page 34


UNIX Part I
What is UNIX?
awk has the concept of fields. The first field is $1, the second $2 and so on.
Take a list of names with records like:

Karen Lynn Bradford

and assign them to variables so

$1 Karen

$2 Lynn

$3 Bradford

Now change them to beaurocratic order like

Bradford, Karen Lynn

The awk command will be

awk ’{ print $3, ",’. $1, $2 }’ name_list.doc

awk can also pattern match.

awk -F: ’$3 < 99’ /etc/passwd

Or do reports like a simple payroll.

awk ’printf(%s %3.2f\n", $1, $2 * $3)’ pr.dat

Bruce Hunter 2/21/01 page 35


UNIX Part I
What is UNIX?
sort

All kinds of things need sorting. sort therefore is the sorting tool for pipe-
lines. It can be used free standing.

424% ypcat passwd | awk -F: ’{ print $1 }’ | sort


28f008s
28f016s
28u008s
87ff800
abashir
aces
achan
adhake
adm
afaizi
afarinas sort Flags
agafken
aibrahim -d dictionary order
akrahmer
. -n nummeric
.
vbui -r reverse
vdalvi
vdiscipu -f fold to lower case
vheil
viewlogic -u uniq
virsingh
vlakhani
vlaw
vlocasio
vwplot
wchang
wlchang
wmansfie
yxiaolin
zahra
zippy
425%

Bruce Hunter 2/21/01 page 36


UNIX Part I
What is UNIX?
tr

tr translates from column 1 to column2. Change the data stream’s letters


from uppercase ot lower.

tr ’A-Z ’a-z’

Its flags are

d delete

s squeeze

c compliment

Delete all numbers

tr -d ’0-9’

Change all non letters to newlines to make a word list

tr -c ’A-z’ ’\012’

Squeeze repeated numbers into a single lettter

tr -s ’0-9’ ’0-9’

Bruce Hunter 2/21/01 page 37


UNIX Part I
What is UNIX?
Quiz

What are the differences between grep, sed, and awk?

Lab

Take the file /etc/motd and make a dictionary from it. Use tr to separate
the words.

tr -cs ’A-Za-z’ ’\012’ </etc/motd

When done, it will look like this. Hint: use the -f flag in sort to get dictio-
nary order.

AIX
bos
file
for
IBM
in
information
lpp
of
Operating
pertinent
Please
README
release
see
System
the
this
to
usr \012 is a newline
Version
Welcome

Bruce Hunter 2/21/01 page 38


UNIX Part I
What is UNIX?
Putting It Together for More Productivity

Taking what you have seen so far let’s automate a task. You need to know
who is using a work server and how many active logins it is supporting. who
is a place to start but it generates data not information.

% who
gnorton pts/0 Jul 13 09:30 (10.1.16.123)
gnorton pts/2 Jul 13 09:41 (10.1.16.123)
gnorton pts/3 Jul 12 10:51 (10.1.16.123)
greddy pts/1 Jul 13 13:27 (10.1.8.106)
skhanzod pts/4 Jul 13 16:53 (10.1.7.129)
skhanzod pts/5 Jul 13 16:53 (10.1.7.129)
skhanzod pts/6 Jul 13 16:53 (10.1.7.129)
skhanzod pts/7 Jul 13 16:53 (10.1.7.129)
greddy pts/8 Jul 14 07:30 (10.1.8.106)
bhunter pts/9 Jun 30 07:43 (10.1.8.138)
jjbennet pts/10 Jul 14 10:27 (10.1.7.138)
bhunter pts/11 Jun 30 12:00 (10.1.8.138)
mmdellen pts/12 Jul 14 08:13 (10.1.12.118)
aguha1 pts/13 Jul 13 14:38 (10.1.7.115)
stbaartm pts/14 Jul 13 10:45 (10.1.7.117)
scfranks pts/15 Jul 13 10:22 (132.233.7.108)
jjbennet pts/16 Jul 14 10:33 (10.1.7.138)
scfranks pts/17 Jul 13 10:22 (132.233.7.108)
szaidi pts/18 Jul 12 11:39 (10.1.7.114)
gnorton pts/19 Jul 13 16:37 (10.1.16.123)
egayles pts/20 Jul 13 17:16 (fri2006)
mmdellen pts/21 Jul 14 08:13 (10.1.12.118)
mmdellen pts/23 Jul 14 08:13 (10.1.12.118)
gnorton pts/22 Jul 12 12:52 (10.1.16.123)
jmaddux pts/26 Jul 14 11:43 (10.1.7.118)
rannamal pts/25 Jul 10 17:34 (10.1.7.178)
szaidi pts/27 Jun 22 16:33 (10.1.7.114)
rannamal pts/24 Jul 10 17:34 (10.1.7.178)
rannamal pts/29 Jul 10 17:34 (10.1.7.178)
aguha1 pts/30 Jul 13 14:38 (10.1.7.115)
rannamal pts/31 Jul 10 17:34 (10.1.7.178)
rannamal pts/32 Jul 10 17:34 (10.1.7.178)
szaidi pts/33 Jun 22 16:33 (10.1.7.114)

Bruce Hunter 2/21/01 page 39


UNIX Part I
What is UNIX?
OK that is just part of the data. Now reduce it to something useful. First how
many logins?

% who | wc -l
102

Next. Who are they, the short form.

% who | sed ’s/ .*//’ | sort -u


aguha1
awirkus
bhunter
egayles
gnorton
greddy
gsimko
jhliang
jjbennet
jmaddux
mamun
mmdellen
mteshome
rannamal
scfranks
skhanzod
stbaartm
szaidi

Need a paper copy?

% (who | wc -l ; who | sed ’s/ .*//’ | sort -u) | lpr -Ppsf62f1

This a just a small example of what can be done.

Bruce Hunter 2/21/01 page 40


UNIX Part I
Logging In

Logging
Into
UNIX
login:

I UNIX Part I
ECT001118
section 3
Last Rev 7/12/99

Bruce Hunter 2/21/01 page 41


UNIX Part I
Logging In
Logging Onto the UNIX System

Everything starts with logging onto the system. When you sit at a UNIX sys-
tem, whether it is a workstation, time- share system, a stand-alone system,
an X terminal, or even an ordinary terminal, you need to log on.

Older-style UNIX systems have the login prompt in the left side of the
screen, usually at the bottom. Otherwise the screen will always be blank.
Graphic systems may have an elaborate login screen with a central display
requesting your logname and password.

When you enter your logname, the system will ask for your password, Your
login name was visible when you typed it, but your password will not be vis-
ible.

login: jrambo
password:

Bruce Hunter 2/21/01 page 42


UNIX Part I
Logging In
Getting A User Account

You need a user account to get on to a UNIX system and you will get it from
your local support organization, like Folsom Engineering’s FMEC CSC..
They will assign a login name that uses your first initial and no more than
seven characters of your last name. For example

Loung Nguyen lnguyen

Tom Jones tjones

Lee Yarmitzki lyarmit

an-non Chen achen

The SA will want to know what group you work with so she can assign a
group number and home directory. Your manager will also have to sign for
you to accept responsibility for your actions.

You should be given a security booklet, and you will be expected to read
and comply with it.

Bruce Hunter 2/21/01 page 43


UNIX Part I
Logging In
Set-Up Files

Your new account’s home directory will be populated with several customi-
zation files. They will be in your home directory and will all start with a dot.

50% cd
51% ls .??*
.Xdefaults .loginx10 .rhosts.O
.Xdefaults.aix .loginx11 .sh_history
.Xdefaults.aix.save .logout .twmrc
.Xdefaults.x10 .mailrc .twmrc.N
.Xdefaults.x11 .mh_profile .twmrc.NEW
.Xdefaults.x11~ .mwmrc .twmrc.ncd
.bash_history .mwmrc.aix .uwmrc
.cshrc .mwmrc.aix.save .xinitrc.aix
.cshrcx10 .netrc .xinitrc.aix.save
.exrc .newsrc .xinitrc.x10
.history .newsrc.bak .xinitrc.x11
.ispell_words .profile
.login .rhosts

Dot Files

Dot files are "invisible." They


are not shown when you do
a ls. A ls -a however will
show them.

Bruce Hunter 2/21/01 page 44


UNIX Part I
Logging In
Primary Dot Files

There are two that you cannot survive without. They are

.login
and
.cshrc

for C shell and tcsh users and

.profile

for Bourne and Korn shell users. These files set up your environment. Here
are some environment variables:

TERM=xterm
SHELL=/bin/csh
HOME=/usr/system/bhunter
LOGNAME=bhunter
USER=bhunter
Path=/usr/ucb:/usr/bin:/bin:/usr/local/bin:/usr/system/bhunter/
bin:
TZ=PST8PDT
LOCPATH=/usr/lib/nls/loc
EXINIT=map z xp
MANPATH=/usr/local/man
EDITOR=vi
XENVIRONMENT=/usr/system/bhunter/.Xdefaults.aix
XINITRC=/usr/system/bhunter/.xinitrc.aix
DISPLAY=unix:0.0

In time, you will learn how to set and customize most of these.

Bruce Hunter 2/21/01 page 45


UNIX Part I
Logging In
Your .login looks like

#!/bin/csh
set mail = /var/spool/mail/bhunter
biff n
stty erase
set noglob; eval ‘tset -Q -s‘; unset noglob
set prompt = "\!% "
set EDITOR = vi
limit coredumpsize 0
set path = ( /usr/ucb /usr/bin /bin /usr/local/bin /usr/system\
bhunter/bin .)
set MANPATH = /usr/local/man

Your .cshrc looks like

#!/bin/csh
stty -nl
alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
set history = 24
set prompt = ’\!% ’
setenv EXINIT "map z xp"
setenv FMHOME /usr/local/frame/3.0x
set path = ( $FMHOME/bin $path )
setenv MANPATH /usr/local/man
setenv EDITOR vi

Bruce Hunter 2/21/01 page 46


UNIX Part I
Logging In
System Prompt

Your shell, a command interpreter, will give you a prompt for each new com-
mand after the execution of each old command. The standard prompts are

% C shell, zsh, or tcsh

$ Korn shell,, pdksh, bash, or Bourne (sh) shell

# super user
You can change shells as you go,

Why not just one shell?

You wouldn’t want to have McDon-


nald’s as the only restaurant. In just
the same way there are different
shells to meet your needs and likes.

Bruce Hunter 2/21/01 page 47


UNIX Part I
Logging In
A Few Basic Commands to Start With

You will learn UNIX commands as you go through each section of this class.
For example you will learn file commands when you study files and file sys-
tems. For the impatient, here are a few commonly used commands.

cd Change directory, as in cd /usr/bsmith

pwd Where am I? The pathway display.

cat Display a file, as in cat foo

ls as in list. It shows the files and directories like dir in NT..

Command Syntax

UNIX commands are parsed as

command_name -flags argument argument argument ...

They must be space separated. Example:

% ls -ld /usr/system/bhunter

Bruce Hunter 2/21/01 page 48


UNIX Part I
Logging In
The flags and arguments may be optional. As an example you could use the
list command simply as

% ls

A few ls flags
or with a flag

%ls -l -a see dot files

-l long
or even two
-t by time
% ls -CF
-d list directories as directories

or three -F fold into columns

% ls -lit

and more

% ls -alit

and you can add arguments like

% ls .profile

or flags and arguments

$ ls -ld /

Bruce Hunter 2/21/01 page 49


UNIX Part I
Logging In
A Few Things Unique to UNIX

UNIX file paths are separated with the / character. It is not the \ used by
Win32 and NT.

62% pwd
/usr/system/bhunter
63%

Command flags are a -flag as in

ls -l

Note it is a -, not the / used by Win32.

There are no file name, file type, file mode trinities as in CMS or mandatory
file extensions as in other operating systems like foo.exe, foo.bat, or
foo.com.

Bruce Hunter 2/21/01 page 50


UNIX Part I
Logging In
Passwords

UNIX passwords do not exist on the machine. Your password is encrypted


by the UNIX crypt(3) command, and the results of that encryption are
stored in /etc/passwd or in YP’s passwd map.

217% ypcat passwd|grep klee


klee:hiHucUxz8gxc:340:200:Kelvin Lee:/eng2/klee:/bin/csh

You can change your password with

passwd

or

yppasswd

Make your password strong, 6 to 8 characters long, an alpha-numeric mix,


with a non-alpha non numeric as well. Do not use sequential keys as in
qwerty. It must be nothing that can be looked up in a dictionary, and use no
doggy/kitty guessable names. An example of a good password for a skier
who likes a Solomon 205 cm grand slalom ski.

Sol_205GS

As of this writing the local command to change passwords on EC systems is

chpasswd

Bruce Hunter 2/21/01 page 51


UNIX Part I
Logging In

Getting and Setting Up X Windows

All you have to do is type runx to invoke X from an Intel UNIX system.
Once in X Windows, you will see that your path is long and has several X
directories:

./usr/local/frame/3.0x/bin /usr/ucb /usr/bin /bin /usr/


local/bin ./usr/bin/X11 /usr/local/X11/bin /usr/lpp/X11/
Xamples/bin

This is not the path you have set up in your home dot files. Those paths
looked like:

39% grep path ~/.login ~/.cshrc


/usr/system/bhunter/.login:set path = ( /usr/ucb /usr/
bin /bin /usr/local/bin .)

Bruce Hunter 2/21/01 page 52


UNIX Part I
Logging In
Lab

If you have not logged on, log on now.

Quiz

Can you customize your UNIX environment? If so, where?

How do you change your password?

Who can see your password?

Who can change your password?

How do you bring up X?

Bruce Hunter 2/21/01 page 53


UNIX Part I
Files and File Systems

Files

UNIX Part I
section 4
ECT001118
Rev. 7/12/99

Bruce Hunter 2/21/01 page 54


UNIX Part I
Files and File Systems
A UNIX File

UNIX files are unique among operating systems.

They have no end-of-file character.

They are of any length.

They are a continuum of characters.

No files are special (except for special files).

They can even be devices. (That is a special file).

Special files are device entry points.

They have at least one directory entry and can have more.

A UNIX Directory

UNIX directories are unique.

They have only two pieces of data for each file, the name and I-node
number.

They are stacked as a hierarchy.

I-Nodes

I-nodes are also unique to UNIX. They contain all the file information except
for the file’s name.

NTFS (NT File System)

NT’s NTFS is POSIX (unix) complient.

Bruce Hunter 2/21/01 page 55


UNIX Part I
Files and File Systems
The File Tree

UNIX arranges its files in a tree hierarchy, like an inverted tree with its root
at the top. The base of every mounted file system is its root. File trees are
mountable.

/ (root)

/bin
/var
/usr mount
point
/spool
/include

/lp /mail
mount path

leaf
/ node

/sys

/errno.h

Bruce Hunter 2/21/01 page 56


UNIX Part I
Files and File Systems
Parents and Children

Going down the tree are child nodes; going up are parent nodes.

parent ..

current .
position

child

child
of
child

The current directory is dot (.) and the parent is dot dot (..).

Bruce Hunter 2/21/01 page 57


UNIX Part I
Files and File Systems
Traditional UNIX Directories

Some directories are traditional to all UNIX systems.

/dev /bin /tmp /var /etc /unix


link
/spool
/mail
/usr

/ucb /lib /include /src


/bin
/man
/mail
/mail /ls
/man1
ls.1
mail.1

/dev is home to the device driver entry points, /var where everything that
grows is kept, /usr is a throwback to old UNIX and is a closet for nearly
everything, /etc is the SA’s tool box, /tmp is a temporary storage place
and, /unix is the kernel.

Bruce Hunter 2/21/01 page 58


UNIX Part I
Files and File Systems
Path Names and Base Names (FQN)
Look at the file tree again to see paths.

/dev /bin /tmp /var /etc /unix


link
/spool
/mail
/usr

/ucb /lib /include /src


/bin
/man
/mail
/mail /ls
/man1
ls.1
mail.1

If you need to get to the ls command, it is

/usr/bin/ls

The /usr/bin is the path, while ls is the base name. In NT terms it is a


FQN, a fully-qualified name.

Bruce Hunter 2/21/01 page 59


UNIX Part I
Files and File Systems
Relative Paths

/dev /bin /tmp /var /etc /unix


link
/spool
/mail
/usr

/ucb /lib /include /src


/bin
/man
/mail
/mail /ls
/man1
ls.1
mail.1

You are in /usr/lib. It now is . (dot). /usr is .. . To get to man you


could

cd man

or
cd /usr/lib/man

Both are relative paths. To get to where UCB (Berkeley) mail is you can

cd ../ucb

Bruce Hunter 2/21/01 page 60


UNIX Part I
Files and File Systems
Mounts

UNIX file systems are mounted. Use the mount command to see what is
mounted.

31% /etc/mount
node mounted mounted over vfs date options
-------- --------------- --------------- ------ ------------ ---------------
/dev/hd4 / jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/hd2 /usr jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/hd9var /var jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/hd3 /tmp jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/lv00 /usr/work jfs Jan 16 07:55 rw,log=/dev/hd8
fmsu01 /misc /misc nfs Jan 16 07:56 rw,hard,bg,intr
fmsu05 /usr/fmo/ssd /usr/fmo/ssd nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /usr/spool/mail /usr/spool/mail nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /amoeba /mproj/amoeba nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /sam16u3 /uproj/sam163 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu05 /samgr /gproj/sam16 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /g16v1 /vproj/sam161 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu05 /g16v /vproj/sam16 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /sam16u2 /uproj/sam162 nfs Jan 16 07:56 rw,hard,bg,intr
.
.

or the df command

32% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 928 77% 864 84% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1444 88% 343 8% /var
/dev/hd3 8192 7880 3% 29 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 149786 81% - - /misc
fmsu05:/usr/fmo/ssd 944238 83861 91% - - /usr/fmo/ssd
fmsu01:/usr/spool/mail 408391 290661 28% - - /usr/spool/mail
fmsu01:/amoeba 408399 247000 39% - - /mproj/amoeba
fmsu01:/sam16u3 1893612 486009 74% - - /uproj/sam163
fmsu05:/samgr 917358 73783 91% - - /gproj/sam16
fmsu01:/g16v1 1836252 746162 59% - - /vproj/sam161
.
.

Bruce Hunter 2/21/01 page 61


UNIX Part I
Files and File Systems
Moving Around Directories

Use the pwd (print working directory) command to see where you are (in a
UNIX file structure).

19% pwd
/usr

Use the cd command to move:

20% cd lib
21% pwd
/usr/lib

Use ls to get a listing:

22% ls
INnet lex libodm.a nmf
Mail.rc lib.b liboldX.a nterm
X11 lib300.a libplot.a objrepos
acct lib300S.a libpp.a phones-file
aliases lib300s.a libprint.a profiled
asw lib4014.a libqb.a ps
bind lib450.a librpcsvc.a pse.exp
boot libIM.a librts.a qm.exp
bugfiler libMrm.a libs.a ras
bugformat libPW.a libsm.a refer
calprog libSIMshr.a libsnmp.a remote-file
cfgodm.ipl libUil.a libsrc.a sa
cflow1 libX11.a libsys.a security
cpp libXdmcp.a libtermcap semutil
crt0.o libXext.a libtermcap.a sendmail
dag libXfx.a libtli.a sendmail.cf
deroff libXm.a liby.a sendmail.

Bruce Hunter 2/21/01 page 62


UNIX Part I
Files and File Systems
cd

The cd command defaults to your home directory, $HOME. Let’s go back to


where you were when you did a long, long ls.

glink.o libisode.a methods unixtomh


graf libl.a mh uucp
help libld.a microcode vfontedpr
inst_updt liblvm.a mkwhatis vgrindefs
instl libm.a ms w2006
kernex.exp libmsaa.a netsvc xpass
learn libnck.a nls yaccpar
23%
23% pwd
/usr/lib
24% cd
25% pwd
/usr/system/bhunter
26% echo $HOME
/usr/system/bhunter
27%

pushd and popd

If you have to leave your current directory for a little while and then come
back ...

bash-2.00$ pwd
/usr/users/a5fs/bhunter/weeklys
bash-2.00$ pushd /fes/bhunter
/fes/bhunter ~/weeklys
bash-2.00$ pwd
/fes/bhunter
bash-2.00$ ls
C NT PERL class.foils
bash-2.00$ popd
~/weeklys

Bruce Hunter 2/21/01 page 63


UNIX Part I
Files and File Systems

Dot and Dot Dot

A cd to .. will take you to your $HOME’s parent.

27% cd ..
28% pwd
/usr/system

dot is your current directory and

dot dot is it’s parent.

Bruce Hunter 2/21/01 page 64


UNIX Part I
Files and File Systems
ls

The ls command gives a listing, but it has dozens of ways to do it. Most
UNIX commands have flags, and ls is no exception.

command_name -flag

-l is the flag for a long listing:

33% cd /usr/include
34% ls -l
total 1848
drwxr-xr-x 2 bin bin 512 Dec 14 1992 DPS
drwxr-xr-x 2 bin bin 512 Dec 14 1992 IN
lrwxrwxrwx 1 root system 24 Dec 14 1992 Mrm -> /usr/
lpp/X11/include/Mrm
-r--r--r-- 1 bin bin 630 Dec 14 1992 NLchar.h
-r--r--r-- 1 bin bin 4156 Dec 14 1992 NLctype.h
-r--r--r-- 1 bin bin 51772 Dec 14 1992 NLregexp.h
-r--r--r-- 1 bin bin 929 Dec 14 1992 NLxio.h
lrwxrwxrwx 1 root system 24 Dec 14 1992 X11 -> /usr/
lpp/X11/include/X11
lrwxrwxrwx 1 root system 23 Dec 14 1992 Xm -> /usr/
lpp/X11/include/Xm
-r--r--r-- 1 bin bin 768 Dec 14 1992 a.out.h
-r--r--r-- 1 bin bin 2212 Dec 14 1992 acl.h
-r--r--r-- 1 bin bin 439 Dec 14 1992 aio.h
.

The command equivalent in NT is DIR.

Bruce Hunter 2/21/01 page 65


UNIX Part I
Files and File Systems
That is more then you may want to see. You can do a ls -CF to fold the
output for compactness .

35% ls -F
DPS/ dbug.h isode/ nlist.h syms.h
IN/ dbxstclass.h jcode.h notice.h sys/
Mrm@ defenv.h jctype.h odmi.h sysexits.h
NLchar.h diag/ jfs/ paths.h syslog.h
NLctype.h dirent.h langinfo.h pcsim@ tar.h
NLregexp.h dumprestor.h ldfcn.h piostruct.h tcpip_audit.h
NLxio.h errno.h limits.h poll.h term.h
X11@ exceptab.h linenum.h procinfo.h termio.h
Xm@ execargs.h loader.h protocols/ termios.h
a.out.h fatal.h locale.h pwd.h time.h
acl.h fcntl.h login.h regex.h tiuser.h
aio.h filehdr.h lprio.h regexp.h ttyent.h
aixfont.h float.h lvm.h reloc.h typchk.h
aixif/ fnmatch.h lvmrec.h resolv.h uil@
aouthdr.h fp.h macros.h rpc/ uinfo.h
ar.h fp_fort_c.f malloc.h rpcsvc/ ulimit.h
arpa/ fp_fort_t.f math.h scnhdr.h unctrl.h
asl.h fptrap.h mbstr.h search.h unistd.h

Note the characters after the file names. They are

/ a directory

* an executable

@ a link

How do you know for sure?

38% file X11


X11: symbolic link to /usr/lpp/X11/include/
X11.
39% file X11 nfs
X11: symbolic link to /usr/lpp/X11/include/
X11.
nfs: directory
40%

That’s the file command. More about it later.

Bruce Hunter 2/21/01 page 66


UNIX Part I
Files and File Systems
Productivity Hints

Time can be saved by moving around the file system quickly and effort-
lessly. Relative path names are faster than absolute path names. ~ saves
time.

Which is faster?

cd /usr/users/a5fs/bhunter/perl

or

cd ~/perl

Use other directories constantly? Put them in your environment then use
them.

bash-2.00$ env | grep WORK


WORK=/fes/bhunter
bash-2.00$ pwd
/usr/users/a5fs/bhunter
bash-2.00$ cd $WORK
bash-2.00$ pwd
/fes/bhunter

Bruce Hunter 2/21/01 page 67


UNIX Part I
Files and File Systems
man

How do you find out more about ls or any command? You do a man(ual) on
it.

69% man ls
ls Command

Purpose

Displays the contents of a directory.

Syntax

To Display Contents of Directory or Name of File

ls [ -A ] [ -a ] [ -d ] [ -i ] [ -L ] [ -N ] [
R ] [-r ] [ -s ] [ -F | -p ] [ -b | -q ] [ -C |
-m | ] [ [ { -c | -u } [ -l ] [ -t ] ] | [ [ -g |
-n | -l | -o | -e ] [ -t ] ] ] [ File ... ]
[ Directory ... ]

To Display Contents of Directory

ls -f [ -d ] [ -i ] [ -s ] [ -C | -m | -x |
] [ Directory ... ]

Description

The ls command writes to standard output the contents


of each specified Directory or the name of each speci-
fied File, along with any other information you ask for
with the flags. If you do not specify a File or Direc-
tory,the ls command displays the contents of the current
directory.
.

Bruce Hunter 2/21/01 page 68


UNIX Part I
Files and File Systems
Some commands like ls have very long man pages. The first thing you
want to do is find the meanings of the flags.

The mode displayed with the -e and -l flags is interpreted


as follows:

If the first character is:

d The entry is a directory.

b The entry is a block special file.

c The entry is a character special file.

l The entry is a symbolic link, and either the -N flag


was specified or the symbolic link did not point to an
existing file.

p The entry is a first-in, first-out (FIFO) special file.

s The entry is a local socket.

- The entry is an ordinary file.

Here is some information, but keep going until you see the key word Flags.

Bruce Hunter 2/21/01 page 69


UNIX Part I
Files and File Systems
Here is the information you are looking for.

Flags

-A Lists all entries except . (dot) and .. (dot-dot).

-a Lists all entries in the directory, including the entrie


that begin with a . (dot).

-b Displays nonprintable characters in an octal (\nnn)


notation.

-c Uses the time of last modification of the i-node for


either sorting (when used with the -t flag) or for
displaying (when used with the -l flag).
This flag must be used with either the -t or -l flag,
or both.

-C Sorts output vertically in a multicolumn format.


This is the default method when output is to a terminal.

-d Displays only the information for the directory named.


Directories are treated like files, which is helpful
when using the -l flag to get the status of a directory.

The page goes on, of course. Send it to the printer if you need a paper copy
.

% man ls | lpr -P fm62f2pub1

Bruce Hunter 2/21/01 page 70


UNIX Part I
Files and File Systems
Signals

Here are some of the signals, line kills, etc taken from one system’s stty
command.

intr = ^C

quit = ^\

erase = ^H

kill = ^U

eof = ^D

eol = ^@

eol2 = ^@

start = ^Q

stop = ^S

susp = ^Z

dsusp = ^Y

reprint = ^R

discard = ^O

werase = ^W

lnext = ^V

Bruce Hunter 2/21/01 page 71


UNIX Part I
Files and File Systems
Metacharacters and Wild Cards, Globbing

UNIX uses all of the keyboard characters other than the alpha and numerics
to do special things. A ^D will end mail, a ^C will kill most programs, a ^Z will
stop a program, a newline will enter a command, a ~ will trigger mail into
acting on a command, a ! will let vi execute a shell command, a ! triggers
C shell into looking at history — the list goes on and on. Characters like this
are called metacharacters.

Wild cards are a subset of metacharacters. They expand shell’s ability to


deal with the file systems. There are three that we will deal with

* any character or characters (or none)

? one, and only one, character

[] a range of characters as in [0-9] or [A-z]

Bruce Hunter 2/21/01 page 72


UNIX Part I
Files and File Systems
Here is a very busy C source code directory. How busy is it (how many C
programs), and are there any header (.h) files?

323% cd;cd c.c


324% ls
README error.c io.c prbarry.c str.o
TD.c ex15.c jump.c prptr.c strcopy.c
a.out ex21.c jumpt.c prt1.c stringi.c
ab.c ex23.c lab.c psum.c strings.c
arg.c ex25.c letter.c ptrtostruct.c struct.c
argold.c ex27.c ln1.c recoil.c struct2.c
ascii.c f.c ln2.c reply.c structa.c
asum.c fac.c ln3.c s.c structp.c
ati.c fapp.c ln4.c s10.c subs.c
bell.c fib.c lname.c s10_10.c substring.c
bit.c fiban.c malo.c seek.c swap.c
bitA.c fibon.c max.c seekOLD.c sys.c
bit_a.c fin.c max2.c simpstruct.c tD.c
bita.c flag.c me.c srt.c testi.c
bitf.c flags.c nexts.c ss.c time.c
bitup.c for.c odd.c sscan.c timesequal.c
bufio.c fout.c onesc.c ssr.c top.c
c_l.c functproto.c orperms.c sst.c true.c
case.c get.c pay_lab.c st.c twoD.c
case_lab.c gstring.c payroll.c st2.c uf.c
cast.c h.c perms.c st_a.c ufp.c
ch5.c hello.c pl.c st_funct.c userfunct.c
char_val.c idx.c po.c statauto.c usrf_ret.c
define.c idxp.c porrage.c stcmp.c w_struct_sun.c
dowhile.c infunct.c pound.c sti.c while.c
echo.c inode.h pr.c str.c write_struct.c

325% ls *.h
inode.h
326% ls *.c|wc -l
235

Bruce Hunter 2/21/01 page 73


UNIX Part I
Files and File Systems
How many C files start with an s or a t?

327% ls [st]*.c
s.c sscan.c stcmp.c structa.c time.c
s10.c ssr.c sti.c structp.c timeseq.c
s10_10.c sst.c str.c subs.c top.c
seek.c st.c strcopy.c substring.c true.c
seekOLD.c st2.c stringi.c swap.c twoD.c
simpstruct.c st_a.c strings.c sys.c
srt.c st_funct.c struct.c tD.c
ss.c statauto.c struct2.c testi.c
328%

How many C files start with st?

328% ls st*.c
st.c st_funct.c sti.c stringi.c struct2.c
st2.c statauto.c str.c strings.c structa.c
st_a.c stcmp.c strcopy.c struct.c structp.c
329%

Do any files start with uppercase characters?

329% ls [A-Z]*
README TD.c
330%

Bruce Hunter 2/21/01 page 74


UNIX Part I
Files and File Systems
Making A Directory

The mkdir command makes a new directory:

279% cd
280% cd junk
281% mkdir snarg
282% ls -ld snarg
drwxr-xr-x 2 bhunter system 512 Feb 01 13:44 snarg
283% date
Tue Feb 1 13:44:52 PST 1994
284%

rmdir

rmdir removes a directory:

284% rmdir snarg


285% ls -ld snarg
ls: 0653-341 The file snarg does not exist.
286%

rmdir will not work on a directory with files in it.

Bruce Hunter 2/21/01 page 75


UNIX Part I
Files and File Systems
Here a directory snarg is made and an empty file put in (with the touch
command). Then an attempt is made to remove snarg with rmdir.

286% mkdir snarg


287% touch ./snarg/bar
288% ls -l snarg
total 0
-rw-r--r-- 1 bhunter system 0 Feb 01 13:49 bar
289% rmdir snarg
rm: 0653-603 Cannot remove directory snarg.
290%

Now the file in snarg is removed and rmdir is run again.

291% rm ./snarg/bar
292% rmdir snarg
293%

Note: with UNIX commands no news is good news. Errors are reported
immediately, success is taken for granted.

Bruce Hunter 2/21/01 page 76


UNIX Part I
Files and File Systems
Quiz

What’s on a directory?

What’s in a file?

What’s in an inode?

What is the name of the base of every unmounted UNIX file system?

How would you get the file size, make time, and file names of all the .c files
in /usr/system/source?

Lab

cd to /usr/include.

List all the header files starting with st. st*

List all the header files that start with an uppercase letter. [A-Z]*

List all the files with an underbar in the name. *_*

List only header files that start with the letters u through z. [u-z]*

Bruce Hunter 2/21/01 page 77


UNIX Part I
Files and File Systems
Creating Files

There must to be a hundred ways to create a file in UNIX. Files are made by

redirection ( > and >> )

touch

applications

vi, vim, xedit, pico, ed, emacs and other editors

the compilers

most logging facilities

and so on. Here is how simple it is.

452% cat>nitt
Now is the time
for all good men
to come to the aid
of their country.
^D

Did it work?

453% cat nitt


Now is the time
for all good men
to come to the aid
of their country.
454%

Bruce Hunter 2/21/01 page 78


UNIX Part I
Files and File Systems
Editors

There is no shortage of UNIX editors. Line editors:

ed

edit

red

ex

Simple editors:

pico

xedit

Streaming editors:

sed (old version)

gsed (GNU extended version)

Heavy-duty editors:

vedit (vi based)

vi

view (vi read-only)

vim (vi on steroids)

emacs

Bruce Hunter 2/21/01 page 79


UNIX Part I
Files and File Systems
vi in 30 seconds :-)

vi is the visual editor and is the de facto UNIX editor. To start vi. type in vi
followed by the file name.

% vi foo

now is the time


~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
foo [new file]

Type an a or an i to get started, then type your file. (: don’t make mistakes :)
Now type esc (escape), then :

Bruce Hunter 2/21/01 page 80


UNIX Part I
Files and File Systems
You’re on the command line.
Now is the time
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
: wq

Type in wq and you’ll write and quit the editor. You will learn (much, much)
more later.

Bruce Hunter 2/21/01 page 81


UNIX Part I
Files and File Systems
Moving and Renaming Files

If a file system is on another mounted partition, it has to be copied byte for


byte to that partition. To save a copy followed by a unlink, there is the mv
(move) command. This

% mv ~/foo /tmp

will move the file foo from your $HOME to /tmp with the same name.

~
"bar" [New file] 4 lines, 10 characters
461% mv bar /tmp
462% ls /tmp
aixfile mpeng.x11 xresources.19054.bak
bar xresources.14543.bak xresources.19129.bak
bhunter.x11 xresources.16629.bak xresources.21812.bak
err.out xresources.18053.bak
errlog xresources.18348.bak
463% rm /tmp/bar
464%

mv is also used to rename a file:

464% touch thx314


465% mv thx314 a.file
466%

466% ls
a.file bs ione rev true.f
w.3.backup
adduser.out bu nitt reverse truef
app find90 perl.f snarg vi.fmt
bhh.shell foo r t w.3

Bruce Hunter 2/21/01 page 82


UNIX Part I
Files and File Systems
Removing files: rm

The rm command removes (unlinks) a file.

294% cd snarg
295% touch foo bar snarg
296% ls
bar foo snarg
297%
297% rm snarg
298% ls
bar foo
299% rm bar
300% ls
foo
301% rm foo

rm will complain if you use it to remove a file that is not there.

302% rm foobar
rm: foobar: A file or directory in the path name does
not exist.
303%
In almost any operation system a removed file is
gone! Some allow them to be restored with spe-
cial utilities if the file structures haven been
changed (much.) UNIX would allow a file to be
reconstructed but one problem, a multi-process
system like UNIX and NT never sits still and
quickly recovers and reuses the space the file
previously occupied.

Bruce Hunter 2/21/01 page 83


UNIX Part I
Files and File Systems
rm With Wild Cards

If you were in your C source directory and did a

rm *.c

you would have no more C code. It’s a good way to get out of supporting the
code:)

Now look at a different scenario.

341% ls
bar.c error.file foo.h who.file
bar.h foo.c snarg.c

Now let’s type real fast and get rid of those C files.

343% rm * .c
rm: .c: A file or directory in the path name does not
exist.
344% ls
345%

The word oops just doesn’t seem to cover the situation!

Bruce Hunter 2/21/01 page 84


UNIX Part I
Files and File Systems
rm flags

-e displays a message after each file is deleted.

-f does not prompt before removing a write-protected file.

-i prompts you before deleting each file.

-r permits recursive removal of directories and their contents.

-- indicates that all arguments following it are to be treated


as file names. This allows you to specify file names starting
with one - (dash).

Bruce Hunter 2/21/01 page 85


UNIX Part I
Files and File Systems
rm applications

You managed to create a file called *. It’s not easy.

myprog>*

Now you want to get rid of it. How?

rm *

will wipe out everything. Now let’s take everything we know and apply it. Do
an

rm -i ?

The system will ask

remove * ?

and you say

You have a huge project directory, your project is done, and you have had
everything archived to tape. Its time to get your disk space back.

% cd projdir
% pwd
/u/hugeproj/projdir
% rm -rf mydir

When the smoke clears, mydir is just a memory, even if it had 300 meg in it
just a while back.

Bruce Hunter 2/21/01 page 86


UNIX Part I
Files and File Systems
Lab

If your are in a training login create a directory under your own name.

Move to your directory and redirect the man page for true into a file called
true.f.

Redirect the man page for test into test.f.

Make a directory called lab from the directory you just created.

cd to lab.

touch the files foo, bar and snarg.

Long list the files and the directory.

Remove snarg.

List the files again.

Remove lab.

Bruce Hunter 2/21/01 page 87


UNIX Part I
Files and File Systems
Comparing Files

Frequently you have to deal with files that may be the same, or may not.
How do you know? Your first indication is the character count from ls -l of
wc. There are two files, true.f and truef. They were named from the
true man page. Try ls.

514% ls -l true*
-rw-r--r-- 1 bhunter system 837 Dec 30 08:51 true.f
-rw-r--r-- 1 bhunter system 839 Feb 03 08:00 truef
515%

Is there a two-character difference? What does wc tell you?

515% wc true*
47 114 837 true.f
47 114 839 truef
94 228 1676total
516%

Same number of lines, same number of words, different number of charac-


ters? How do you know for sure?

Bruce Hunter 2/21/01 page 88


UNIX Part I
Files and File Systems
cmp

cmp tells you if two files are different. It’s a binary answer; they are or are not
the same. The syntax is

cmp file1 file2

519% cmp true.f truef


true.f truef differ: byte 33, line 4
520%

cmp tells you that line 4, the 33rd byte, is different. Now use vi and set
number.

1 true or false Command


2
3
4 Purpose
5
6 Returns an exit value of zero (true) or a nonzero exit value
7 (false).

1 true or false Command


2
3
4 purpose
5
6 Returns an exit value of zero (true) or a nonzero exit value
7 (false).

Bruce Hunter 2/21/01 page 89


UNIX Part I
Files and File Systems
diff
diff tells you

1) If files are different

2) Where they are different

When the diff command is run on regular files, and when comparing text
files that differ during directory comparison, the diff command tells what
lines must be changed in the files to make them agree. The syntax is

diff file1 file2

516% diff true.f truef


4c4
< Purpose
---
> purpose
17c17
< The true command returns a zero exit value. The false command
---
> The true command returns zero exit value while the false com-
mand
517%

The < tells you the line is in file1; the > is file2.

Bruce Hunter 2/21/01 page 90


UNIX Part I
Files and File Systems
links

The name and I-node number of the file are stored in the directory. That is
all the information there; there is no more. You can see a directory’s con-
tents with od.

349% pwd
/usr/system/bhunter/junk/snarg
350% od -xc .
0000000 0533 2e00 0000 0000 0000 0000 0000 0000
005 3 . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0 \0
0000020 8100 2e2e 0000 0000 0000 0000 0000 0000
\0 . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0
0000040
351% ls
352%

There is almost nothing there (there is . and ..)! Now add a new file and
look at it. The file you see is a link. Note the I-node numbers. foo’s is
0XA053A which is 656698 in base 10.

352% touch foo


353% !od
od -xc .
0000000 0533 2e00 0000 0000 0000 0000 0000 0000
005 3 . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0 \0
0000020 8100 2e2e 0000 0000 0000 0000 0000
0000
\0 . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0
0000040 053a 666f 6f00 0000 0000 0000 0000 0000
005 : f o o \0 \0 \0 \0 \0 \0 \0 \0 \0
\0 \0
0000060
354%

Bruce Hunter 2/21/01 page 91


UNIX Part I
Files and File Systems
367% ls -lid . ..
656691 drwxr-xr-x 2 bhunte system 512 Feb 02 07:35 .
426240 drwxrwxr-x 3 bhunter system 512 Feb 01 13:56 ..
368% ls -li *
656698 -rw-r--r-- 1 bhunter system 0 Feb 02 07:35 foo
369%

Bruce Hunter 2/21/01 page 92


UNIX Part I
Files and File Systems
Hard Links
A hard link is a link created by the system administrator to give another
name to a file or device. /dev has devices with multiple names. The floppy
drives are a good example.

357% ls -l fd*
brw-rw-rw- 1 root system 11, 0 Jan 24 14:10 fd0
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0.18
brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 fd0.9
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0h
brw-rw-rw- 1 root system 11, 2 Dec 14 1992 fd0l

Note the link count of 2 on fd0.18 and fd0h. Could they be the same
device? Let’s use the -i (I-node) flag of ls to find out.

358% ls -li fd*


32 brw-rw-rw- 1 root system 11, 0 Jan 24 14:10 fd0
33 brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0.18
19 brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 fd0.9
33 brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0h
34 brw-rw-rw- 1 root system 11, 2 Dec 14 1992 fd0l
359%

That’s it! fd0h (high) is fd0.18 (18 sector).

To make a hard link use the ln command.

% ln foo bar

Bruce Hunter 2/21/01 page 93


UNIX Part I
Files and File Systems
Soft or Symbolic Links

Sometimes a file or a directory isn’t where you think it is. A link is created
to make a directory entry that points to it. On many distributions perl is in /
usr/local/bin, but it appears to be in /usr/bin. Note: on EC systems it
is in /usr/intel/bin.

370% ls -l /usr/bin/perl
lrwxrwxrwx 1 root system 19 Dec 14 1992
/usr/bin/perl -> /usr/local/bin/perl
371%

What we see is a symbolic link.

Links are created with the ln command. The syntax for a soft link is

ln -s real_file link_name

A user uses netstat often. She wants to execute it without the long path
name.

ln -s /usr/etc/netstat ~/bin/netstat

Now with ~/bin in her $PATH, she need never have to type the path name
(for that command) again.

Bruce Hunter 2/21/01 page 94


UNIX Part I
Files and File Systems
What Kind Of File Is It?

Before you attempt to look at an existing file, you may want to figure out
what kind of file it is. The command is file.

49% pwd
/usr/system/bhunter/c.c
50% file README ab.c a.out
README: ascii text
ab.c: c program text
a.out: executable (RISC System/6000 V3.1
or object module not stripped
51%

51% cd
52% cd scripts
53% file *
2args: commands text
EWS: ascii text
alchk: commands text
args: English text
articl: [nt]roff, tbl, or eqn input text
base8: commands text
cleanx: commands text
deroff: ascii text
dmon: commands text
dmonb: commands text
enter: ascii text
etr: c program text
.
.
rwall: commands text
sed: directory
shf: English text
subst: commands text
t: empty
th: commands text
until: ascii text
yorn: ascii text
54%

Bruce Hunter 2/21/01 page 95


UNIX Part I
Files and File Systems
Quiz

You see a file foo. How do you test to see what kind of file it is?

You have two files that may be the same. Give three ways to test for same-
ness.

/bin/test and [ are the same file. How is that done?

You need to make the file bar in /usr/local/bin appear as if it is in your


home directory. How are you going to do it? What is the command syntax?

Bruce Hunter 2/21/01 page 96


UNIX Part I
Files and File Systems
Looking At Files
cat
The most primitive of the look at commands is cat.

259% cat foo


1
22
333
4444
55555
666666
7777777
88888888
999999999
end
270%

wc

It’s great if the file is not too long. There is always more for longer ones.
But first of all, how long is the file? wc, word count gives the length of a file in
characters, lines, and words. Here the -w (word count) flag is used.

265% wc -l PLAN
207 PLAN
266%

Bruce Hunter 2/21/01 page 97


UNIX Part I
Files and File Systems
more
Here is more turned loose on the original outline for this class.

266% more PLAN


New Intro To UNIX Plan

1) Add ed and vi class [foils done]

2) Add full networking section [illustrations done]

3) Add X windows section [take in part from X admin foils]

Class Type
Interactive with multiple labs

Outline
What Is UNIX?

A Brief Discription of the UNIX Operating System

Multi-user, multi process, general purpose, and


programmer friendly

Fully networkable

Currently favoring distributed computing and shared


resources

The History of UNIX

The Bell Labs years

The divestiture, Version 7 and System iii

UNIX at Berkeley

BSD Networking and Sun Microsystem’s Birth

--More--(26%)

Note the More line at the bottom of the screen.

Bruce Hunter 2/21/01 page 98


UNIX Part I
Files and File Systems
more options and flags

You will move forward a line if you hit enter in more. Strike the space bar
and you move a page. Enter Q and you’ll quit.

more flags

-c Keeps the screen from scrolling.

-d Prompts the user to continue, quit, or obtain help.

-f Causes more to count logical lines, rather than screen lines.

-l Does not treat the ^L (form-feed) character in a special way.

-n An integer that specifies the number of lines in the window.

-p Disables scrolling so that the screen is cleared before the


next screen of text is displayed.

-s Squeezes multiple blank lines from the output to produce only


one blank line.

-u Prevents more from underlining or from creating


stand-out mode for underlined information in a source file.

-v Prevents the display of nonprinting characters graphically.

-w Keeps the current file open, even when it reaches the EOF .

-z Displays additional control characters.

+number
Starts at the line specified by the number.

+g Displays the last screen in the file and allows you to scroll
backward through the file rather than forcing an exit.

Bruce Hunter 2/21/01 page 99


UNIX Part I
Files and File Systems

K Spacebar
Displays K more lines of text when the space bar is pressed.

Kd Displays 11 more lines (a "scroll").

Kz or z Sets a new default window size K lines long.

Ks Skips forward the number of lines specified for K and prints a


full screen of lines.

Kf Skips forward the number of screens specified for K and prints


a full screen of lines.

Kb Skips backwards K number of screens and prints a full screen


of lines.

K^B Skips backwards K number of screens and prints a full screen


of lines.

q or Q Exit from the more command.

= Displays the current line number.

v Invokes vi at the current line.

h This is a help command.

!command or %command
Invokes a shell.

K:n Skips to the Kth file following in the command line.

K:p Skips to the Kth previous file given in the command line.

:f Displays the current file name and line number.

Bruce Hunter 2/21/01 page 100


UNIX Part I
Files and File Systems
vi

The most intuitive way to read a long file is with vi. vi can be invoked read
only with

vi -R

Also look at vedit.

Bruce Hunter 2/21/01 page 101


UNIX Part I
Files and File Systems
head

head shows only the top lines of a file.

268% head PLAN

New Intro To UNIX Plan

1) Add ed and vi class [foils done]

2) Add full networking section [illustrations done]

3) Add X windows section


[take in part from X admin foils]

tail

tail shows the last lines.

269% tail PLAN

telnet and ftp

NFS internals

using NFS

YP internals

using YP cat
270%

Bruce Hunter 2/21/01 page 102


UNIX Part I
Files and File Systems
od
od stands for octal dump. You can’t read an executable assembler output
(a.out). Here it is in octal.

$ cd
$ cd c.c
$ od a.out
0000000 000737 000007 026511 065606 000000 005022 000000 000062
0000020 000110 010002 000413 000001 000000 001020 000000 000104
0000040 000000 000020 000000 000050 000000 001000 000000 000000
0000060 000000 000060 000004 000002 000004 000004 000007 000005
0000100 000002 000003 030514 000000 000000 000000 000000 000000
0000120 000000 000000 000000 000000 000000 000000 027160 060544
0000140 000000 000000 000000 000000 000000 000000 000000 000024
0000160 000000 000754 000000 000000 000000 000000 000000 000000
0000200 000000 000010 027164 062570 072000 000000 000000 001000
0000220 000000 001000 000000 001020 000000 001000 000000 004442
0000240 000000 000000 000015 000000 000000 000040 027160 060544
0000260 000000 000000 000000 000000 000000 000000 000000 000760
0000300 000000 002020 000000 000000 000000 000000 000000 000000
0000320 000000 000010 027144 060564 060400 000000 000000 000000
0000340 000000 000000 000000 000104 000000 003000 000000 004644
0000360 000000 000000 000013 000000 000000 000100 027142 071563
0000400 000000 000000 000000 000104 000000 000104 000000 000020

Bruce Hunter 2/21/01 page 103


UNIX Part I
Files and File Systems

Base 16 (hex) may be more to your liking.

$ od -x a.out
0000000 01df 0007 2d49 6b86 0000 0a12 0000 0032
0000020 0048 1002 010b 0001 0000 0210 0000 0044
0000040 0000 0010 0000 0028 0000 0200 0000 0000
0000060 0000 0030 0004 0002 0004 0004 0007 0005
0000100 0002 0003 314c 0000 0000 0000 0000 0000
0000120 0000 0000 0000 0000 0000 0000 2e70 6164
0000140 0000 0000 0000 0000 0000 0000 0000 0014
0000160 0000 01ec 0000 0000 0000 0000 0000 0000
0000200 0000 0008 2e74 6578 7400 0000 0000 0200
0000220 0000 0200 0000 0210 0000 0200 0000 0922
0000240 0000 0000 000d 0000 0000 0020 2e70 6164
0000260 0000 0000 0000 0000 0000 0000 0000 01f0
0000300 0000 0410 0000 0000 0000 0000 0000 0000
0000320 0000 0008 2e64 6174 6100 0000 0000 0000
0000340 0000 0000 0000 0044 0000 0600 0000 09a4
0000360 0000 0000 000b 0000 0000 0040 2e62 7373
0000400 0000 0000 0000 0044 0000 0044 0000 0010

How about in both character and hex?

$ od -xc a.out |head


0000000 01df 0007 2d49 6b86 0000 0a12 0000 0032
001 ß \0 007 - I k \0 \0 \n 022 \0 \0 \0 2
0000020 0048 1002 010b 0001 0000 0210 0000 0044
\0 H 020 002 001 013 \0 001 \0 \0 002 020 \0 \0 \0 D
0000040 0000 0010 0000 0028 0000 0200 0000 0000
\0 \0 \0 020 \0 \0 \0 ( \0 \0 002 \0 \0 \0 \0 \0
0000060 0000 0030 0004 0002 0004 0004 0007 0005
\0 \0 \0 0 \0 004 \0 002 \0 004 \0 004 \0 007 \0 005
0000100 0002 0003 314c 0000 0000 0000 0000 0000
\0 002 \0 003 1 L \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
$

Bruce Hunter 2/21/01 page 104


UNIX Part I
Files and File Systems
The -c flag makes more sense with characters in the file.

275% od -xc ~/foo


0000000 2020 2020 2020 2020 310a 0932 320a 0933
1\n 2 2 \n 3
0000020 3333 0a09 3434 3434 0a09 3535 3535 350a
3 3 \n 4 4 4 4 \n 5 5 5 5 5 \n
0000040 0936 3636 3636 360a 0937 3737 3737 3737
6 6 6 6 6 6 \n 7 7 7 7 7 7 7
0000060 0a09 3838 3838 3838 3838 0a09 3939 3939
\n 8 8 8 8 8 8 8 8 \n 9 9 9 9
0000100 3939 3939 390a 2020 2020 2020 2020 656e
9 9 9 9 9 \n e n
0000120 640a
d \n
0000122

Bruce Hunter 2/21/01 page 105


UNIX Part I
Files and File Systems
Do I Have Enough Space?

Don’t start a big job if you have nowhere to put the results! First see if you
have any room on the disk with df.

255% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 928 77% 864 84% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1440 88% 343 8% /var
/dev/hd3 8192 7868 3% 30 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 149781 81% - - /misc
fmsu05:/usr/fmo/ssd 944238 84130 91% - - /usr/fmo/ssd
fmsu01:/usr/spool/mail 408391 290868 28% - /usr/spool/mail
fmsu01:/amoeba 408399 247002 39% - - /mproj/amoeba
fmsu01:/sam16u3 1893612 468626 75% - - /uproj/sam163
fmsu05:/samgr 917358 44177 95% - - /gproj/sam16
fmsu01:/g16v1 1836252 746162 59% - - /vproj/sam161
fmsu05:/g16v 1278413 77031 93% - - /vproj/sam16
fmsu01:/sam16u2 1835996 572904 68% - - /uproj/sam162
fmsu01:/sam16u1 917870 76799 91% - - /uproj/sam161
fmsu05:/sam16u 1968100 185982 90% - - /uproj/sam16
fmsu05:/sam16 1278413 141670 88% - - /mproj/sam16
fmsu01:/sam16m1 917870 48603 94% - - /mproj/sam161
fmsu05:/g16 1278413 587612 54% - - /gproj/sam161
fmsu05:/samgr2 1298653 423523 67% - - /gproj/sam162
fmsu05:/e2 944238 739466 21% - - /e2
fmsu01:/28u008s 508958 93717 81% - - /gproj/28u008s
fmsu05:/usr/mfg 638430 35947 94% - - /usr/mfg

The key is to look for % used. The size is under Total KB.

Bruce Hunter 2/21/01 page 106


UNIX Part I
Files and File Systems
Who Took All The Space?

Use the du command to see where all the space has gone within a mounted
file system .

256% cd
257% du
120 ./bin
4 ./LAN
352 ./mail
18 ./p-h
16 ./AIX
10 ./ez
4 ./lpr
2 ./local/bin
4 ./local/local/bin
6 ./local/local
10 ./local
.
.
26 ./stalker
2 ./root_owned
2 ./hunter
2 ./restore
56 ./x400
1790 ./intro
36 ./weeklys/93
54 ./weeklys
8 ./travel
8 ./chandler
12 ./justification
18 ./c.class.comments
22 ./sed
41012 .
258%

The 41012 . is the home directory total with 41 K blocks — that’s 20 Meg!

Bruce Hunter 2/21/01 page 107


UNIX Part I
Files and File Systems
Quiz
What are the commands for

Looking at a non-ascii file?

Testing for disk space?

Looking at text?

Seeing the top of a file?

The bottom?

Looking at all of a big file without losing it up your screen?

Lab

Find the command true. What kind of file is it? Put it on the screen if you
can.

Find from. Are the any strings in it? What are they?

How much room is left on your current file system?

How much stuff in your login directory (in blocks or bytes)?

Bruce Hunter 2/21/01 page 108


UNIX Part I
Files and File Systems

Productivity Tricks for Files and File Systems

Keep directories small. File searching is sequential and that is inefficient in


large directories. Also it is easier to find a file in a small listing.

If you are having any problem finding a file don’t waste your time. Use the
find command and let the machine do the looking for you.

If you have to move a lot between directories

1) Use pushd and popd

2) Use multiple windows

Are two files the same or different? Use diff to find out.

Do you have repetitive file operations? Write a script to do it.

df your file system before taking on a large disk intensive task. You may not
have enough room so find out before wasting the computer’s, the network’s
and your time.

Bruce Hunter 2/21/01 page 109


UNIX Part I
The shells

The Shells

tcsh
csh
sh
ksh
zsh
bash
pdksh
UNIX Part I
Section 5
Rev 7/12/99
ECT001118

Bruce Hunter2/21/01 page 110


UNIX Part I
The shells

Shell?

The UNIX shells are both your command interpreter and a programming
language as well. They carry your environment and pass it on to other pro-
cesses. They offer:

Full cusomization by and for the user

rsh
Ease of use

User features (aliases, command line editing, history ...)

Job Control

h
cs
Command linkage

Command to file “redirection”

Automation of log-on and log-off tasks

bash
Automated start up of other automated tasks

Considerations for your choice of which shell to use at any specific time:

Speed

UNIX command compatibility


sh

Features

You have a log-in shell and can change your current shell at any time.

pdksh
h
ks
tc

zsh
sh

Bruce Hunter2/21/01 page 111


UNIX Part I
The shells

Conventions
This class will deal with three of the shells. When looking at examples, you
will be able to tell the difference by the prompts.

% C shell, tcsh

$ Bourne or Korn shell and pdksh, bash ...

The shell also has escapes. They commonly are set to:

^D Control D End of Text

^C Control C Kill program

^U Control U Line kill

^H Delete Character kill

Esc Escape vi mode change

^S /^Q stop/start xon xoff

These are not uniform from one UNIX version to another and are change-
able by the user.

What You Will Learn

In this section you will learn about the shell and shell process, its environ-
ment, and its environmental variables. You will learn how to set shell vari-
ables and what the environmental variables do for you. You will be exposed
to Bourne, Korn, and C shells. You will even get into shell programming.
Here more than any one place are the keys to productivity.

Bruce Hunter2/21/01 page 112


UNIX Part I
The shells

There Is More Than One Shell

The shell is a very special process. It is your command interpreter, it is a


programming language, and it is parent of every process you will deal with.
Shells come in flavors. There are a few:

sh The Bourne shell, the original shell. Used by SAs.

csh The C Shell from BSD, a favorite.

ksh Korn shell like sh but C shell-like features.

pdksh Public-domain Korn shell.

rsh Restricted shell (AT&T.) Archaic.

bash Bourne-again shell, a variation of sh with csh features,


file-name completion, history, integer math, job control,
command line editing ....

zsh resembles ksh and includes: comand-line editing,


built-in spelling correction, command completion,
shell functions, a history mechanism, ....

tcsh an enhanced version of C shell. including a


command-line editor programmable word completion,
and spelling correction

Bruce Hunter2/21/01 page 113


UNIX Part I
The shells

Globbing

Globbing is a funny-sounding name for file-name expansion. The shells live


in a world of files and directories, and that is their orientation. The shells
have a set of wild card metacharacters that are used for file-name expan-
sion.

* Any characters or no character ( 0 to infinity).

? Just one character.

[] Encloses a range of characters.

Here are a few usage examples. First the * as a filler for several characters.

162% ls b*.c
bell.c bit.c bitA.c bit_a.c bita.c bitf.c
bitup.c bufio.c
163%

Now a search of all three-letter prefixes.

163% ls ???.c
arg.c c_l.c fib.c get.c ln1.c ln4.c s10.c sst.c str.c
ati.c ch5.c fin.c idx.c ln2.c max.c srt.c st2.c sys.c
bit.c fac.c for.c lab.c ln3.c odd.c ssr.c sti.c top.c
164%

A search for just header files.

164% ls *.h
inode.h

Look for inode programs and header files.

168% !165
ls inode.[ch]
inode.c inode.h
169%

Bruce Hunter2/21/01 page 114


UNIX Part I
The shells

Now a hard one, all the dot files in $HOME but not the directories . and ..

170% ls .??*
.Xdefaults .loginx11 .profile
.Xdefaults.aix .logout .rhosts
.Xdefaults.aix.save .mailrc .rhosts.O
.Xdefaults.x10 .mh_profile .sh_history
.Xdefaults.x11 .mwmrc .twmrc
.Xdefaults.x11~ .mwmrc.aix .twmrc.N
.bash_history .mwmrc.aix.save .twmrc.NEW
.cshrc .netrc .twmrc.ncd
.cshrcx10 .newsrc .uwmrc
.exrc .newsrc.bak .xinitrc.aix
.history .nfs4D2 .xinitrc.aix.save
.ispell_words .nfs5D2 .xinitrc.x10
.login .nfs6D2 .xinitrc.x11
.loginx10 .nfs7D2

Only the .X files.

171% ls .X*
.Xdefaults .Xdefaults.aix.save .Xdefaults.x11
.Xdefaults.aix .Xdefaults.x10 .Xdefaults.x11~
172%

All X files.

172% ls .[Xx]*
.Xdefaults .Xdefaults.x11 .xinitrc.x10
.Xdefaults.aix .Xdefaults.x11~ .xinitrc.x11
.Xdefaults.aix.save .xinitrc.aix
.Xdefaults.x10 .xinitrc.aix.save

Bruce Hunter2/21/01 page 115


UNIX Part I
The shells

Now for ranges. Get files with names starting with the first half of the lower-
case and all the uppercase files as well.

174% ls [A-Za-l]*
README bitf.c error.c flag.c inode.h
TD.c bitup.c ex15.c flags.c io.c
ab.c bufio.c ex21.c for.c jump.c
arg.c c_l.c ex23.c fout.c jumpt.c
argold.c case.c ex25.c functproto.c lab.c
ascii.c case_lab.c ex27.c get.c letter.c
asum.c cast.c f.c gstring.c ln1.c
ati.c ch5.c fac.c h.c ln2.c
bell.c char_val.c fapp.c hello.c ln3.c
bit.c define.c fib.c idx.c ln4.c
bitA.c dowhile.c fiban.c idxp.c lname.c
bit_a.c echo.c fibon.c infunct.c
bita.c envp.c fin.c inode.c
175%

And now the other half of the lower-case alphabet plus the numbers.

176% ls [m-z0-9]*
020494 porrage.c seekOLD.c str.c testi.c
malo.c pound.c simpstruct.c str.o time.c
max.c pr.c srt.c strcopy.c timesequal.c
max2.c prbarry.c ss.c stringi.c top.c
me.c prptr.c sscan.c strings.c true.c
nexts.c prt1.c ssr.c struct.c twoD.c
odd.c psum.c sst.c struct2.c uf.c
onesc.c ptrtostruct.c st.c structa.c ufp.c
orperms.c recoil.c st2.c structp.c userfunct.c
pay_lab.c reply.c st_a.c subs.c usrf_ret.c
payroll.c s.c st_funct.c substring.c w_struct_sun.c
perms.c s10.c statauto.c swap.c while.c
pl.c s10_10.c stcmp.c sys.c write_struct.c
po.c seek.c sti.c tD.c

Bruce Hunter2/21/01 page 116


UNIX Part I
The shells

Quiz

What is a good choice for:

a fast shell?

a fully featured shell?

features and speed?

command compatibility?

Give the full command line to see only perl code (.pl) in /usr/local/
src/perl

The line

$ ls [aieou]*.c

what will happen?

You just went from csh to sh and did some work. Now you want to go back.
How?

Bruce Hunter2/21/01 page 117


UNIX Part I
The shells

The Shell’s Environment


Each user’s shell memorizes its environment. Here’s one displayed with the
env command.

434% env
TERM=xterm
SHELL=/bin/csh
HOME=/usr/system/bhunter
LOGNAME=bhunter
USER=bhunter
PATH=/usr/ucb:/usr/bin:/bin:/usr/local/bin
TZ=PST8PDT
LOCPATH=/usr/lib/nls/loc
LANG=En_US
EXINIT=map z xp
FMHOME=/usr/local/frame/3.0x
MANPATH=/usr/local/man
EDITOR=vi
XENVIRONMENT=/usr/system/bhunter/.Xdefaults.aix
XINITRC=/usr/system/bhunter/.xinitrc.aix
DISPLAY=unix:0.0
WINDOWID=12582925
TERMCAP=co#80:li#24:

You can set your own environmental variables to help speed up your work.

FMHOME=/usr/system/sframe; PATH=:.:$FMHOME/bin:$PATH;
export FMHOME PATH
WORK=/fes/bhunter
export WORK

These environmental variables are the only variables that the shell automat-
ically passes to each program that it starts.

Bruce Hunter2/21/01 page 118


UNIX Part I
The shells

Environmental Variables

TERM The terminal type

SHELL The shell of choice

HOME The user’s home directory

LOGNAME The user’s name

USER Also the user’s log name

PATH The shell’s search path

PROMPT Your system prompt

TZ The time zone

LANG The spoken language of choice

EXINIT The editor environmental variable

MANPATH The location of the unformatted man pages

EDITOR The user’s editor of choice

DISPLAY The display location for X

TERMCAP The termcap entry for the user’s terminal

UID The user’s ID number

GID The user’s group ID number

For any one user or environment, there may be more, or fewer, but these
are typical.

Bruce Hunter2/21/01 page 119


UNIX Part I
The shells

/etc/passwd

The shell gets its first bits of information (environmental variables) from the
file /etc/passwd (or the YP version or part of the login process.) Here is
one passwd record:

bhunter:rzojv8/Uf2q92:100:0:Bruce H Hunter:
/usr/system/bhunter:/bin/csh

Here are the environmental variables picked up.

LOGNAME bhunter

UID 100

GID 0

HOME /usr/system/bhunter

SHELL /bin/csh

The command to change your pass-


word is /bin/passwd.

Bruce Hunter2/21/01 page 120


UNIX Part I
The shells

Environmental Variables And UNIX Programs

An environmental variable is one that is known to programs that are exe-


cuted from the shell. Here, in C, is how the shell knows about them.

main(argc, argv, envp)


int argc;
char *argv, *envp[];
{
int i = 0;

while ( envp[i][0] )
puts (envp[i++]);
}

Every compiled program that starts from C code ( or equivalent) automati-


cally receives two pointers to vectors (arrays of arrays of character or arrays
of strings). One of them , envp, is a pointer to all the user’s variables. This C
program prints each variable to the screen one at a time.

Preset Variables

All you have to do to invoke a preset


variable is mention its name. nogob is
an example of a preset. say

noglob

and your shell will not expand variables

Bruce Hunter2/21/01 page 121


UNIX Part I
The shells

Here is the output of the compiled C program. It looks like the output of the
env command, doesn’t it?

485% a.out
TERM=xterm
SHELL=/bin/csh
HOME=/usr/system/bhunter
LOGNAME=bhunter
USER=bhunter
PATH=/usr/local/frame/3.0x/bin:/usr/local/frame/3.0x/
bin:/usr/
local/frame/3.0x/bin:/usr/ucb:/usr/bin:/bin:
/usr/local/bin:/usr/system/bhunter
/bin:/usr/bin/X11:/usr/local/X11/bin:/usr/lpp/X11/bin:
/usr/lpp/X11/Xamples/bin:.
TZ=PST8PDT
LOCPATH=/usr/lib/nls/loc
NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/prime/
%N
ODMDIR=/etc/objrepos
LANG=En_US
EXINIT=map z xp
FMHOME=/usr/local/frame/3.0x
MANPATH=/usr/local/man
EDITOR=vi
XENVIRONMENT=/usr/system/bhunter/.Xdefaults.aix
XINITRC=/usr/system/bhunter/.xinitrc.aix
DISPLAY=unix:0.0
WINDOWID=12582925
TERMCAP=co#80:li#24:

cc, the C compilor, automatically names its output


program a.out.

Bruce Hunter2/21/01 page 122


UNIX Part I
The shells

Ordinary Shell Variables

You can create ordinary (non-environmental) variables on the fly. These are
not part of the shell’s ENV.

440% set hi = Hello


441% set there = ’ world’
442% echo $hi $there
Hello world
443%

Use set to see what variables are set (in C shell).

443% set
argv ()
cwd /usr/system/bhunter/intro
hi Hello
history 24
home /usr/system/bhunter
path /bin /usr/ucb /usr/bin /bin /usr/local/bin
prompt !%
shell /bin/csh
status 0
term xterm
there world
user bhunter

Use unset to unset a variable

444% unset hi there

Bruce Hunter2/21/01 page 123


UNIX Part I
The shells

Setting Variables

Variables are set differently in ksh and sh than they are in csh

Variables in sh, bash, pdksh, zsh, and ksh


Setting a variable in sh looks like a simple assignment

$ a=1234
$ b=5678
$ c=90
$ echo $a$b$c
1234567890
$ echo $a
1234
$

Note the is no white space around the assignment operator.

Variables in csh and tcsh


C shell needs a little more hand holding, via the set command.

467% set a = A
468% echo $a
A

Note the white space on either side of the =.

Bruce Hunter2/21/01 page 124


UNIX Part I
The shells

The Shell Remembering An Environmental Variable

As you have already seen, a variable can be set and kept in the shell’s envi-
ronment. We say that they are exported.

export

In sh you export a variable so all of its children will receive it as part of the
ENV.

TERM=VT100
export TERM

setenv

In csh you do the same thing for environmental variables with setenv.

setenv EXINIT "map z xp"


setenv FMHOME /usr/local/frame/3.0x
setenv MANPATH /usr/local/man
setenv EDITOR vi

Bruce Hunter2/21/01 page 125


UNIX Part I
The shells

Quiz

What is an environmental variable?

What is a preset variable?

Where is $HOME’s first set?

What kind of variabes are these:

HOME

PATH

path

noglob

TERM

Lab

1) Set a, b, c, and d to 1 - 4.

2) Display their values.

3) Unset the variables.

Bruce Hunter2/21/01 page 126


UNIX Part I
The shells

Which Shell Is Best?

No one shell is “better” than another. High points of Bourne shell are

It is fast (7 to 10 times faster than csh).

It agrees with all UNIX utilities.

It works well in scripts.

It is the system shell.

On the other hand, C Shell is feature rich with

A history mechanism

Refined job control

Aliases

Command line editing (limited)

All the new shells have C shell’s features plus much more.

The Newer Shells

Korn, pdksh, zsh, and bash have all the features of sh and csh with no
disadvantages., They are fast, and have fantastic command line editing,
know ~, file name and command completion, integer manipulation, as well

tcsh is an enhanced version of the C shell. It is both a command language


interpreter and interactive login shell It includes a command-line editor, pro-
grammable word completion and spelling correction.

Bruce Hunter2/21/01 page 127


UNIX Part I
The shells

Typical features of bash,zsh,pdksh,ksh, and tcsh

File, directory, and command name completion

For example type in just enough of the name to show it is unique and the
shell will complete it. To go to the directory weeklies type

cd ~/we->

where -> is a tab and it takes you to $HOME/weeklies.

History

If you were in vi editing a file myprog.pl a few commands back just type

!v

and it will execute

vi myprog.pl

The up and down arrow keys will move forward and back through history.

Command-line editing

To edit the command line use the right and left arrow keys to walk through
the line, delete will remove a character, and typing a character will insert it.

Full job control

In all these shells ^Z will suspend a job, fg will foreground it and bg will
background it. For example if you had just suspended a foreground job, and
it was the only one under job control,

bg %1

will restart it detached from the terminal.

Bruce Hunter2/21/01 page 128


UNIX Part I
The shells

Choosing a Shell For Productivity

sh
Old, command compatible, fast, feature poor

csh
Old, slow, fewer features that the newer shells, UNIX command returns
incompatible with csh.

ksh
Fast, more features. Portability issues with free UNIX.

bash, zsh, pdksh


Fast, the most features, the most compatibility with both sh and csh syntax
and features. Advantages:

command completion

aliases

command-line editing

expansive use of history

typeset for integers and arrays

more built-ins for speed

Bruce Hunter2/21/01 page 129


UNIX Part I
The shells

Switching Shells

All you have to do to change shells is invoke one from the other.

486% sh
$
$ ^D 487%

To make a permanent change of shells, have your SA change your shell in


the passwd file.

Killing a shell

To kill the new shell, hit it with a end-of text signal (^D) or try logout or
exit.

Lab
cd to your home directory. Change to Bourne shell. cd to /tmp. Kill your
Bourne shell (^D) and run pwd. Where are you?

Bruce Hunter2/21/01 page 130


UNIX Part I
The shells

Customizing Your Shell Environment — Dot Files

Where and when are your shell variables set? A few are set by the login pro-
cess and passed to your login shell by init. Most are set in your start-up
files like

.profile sh ksh

.bashrc bash

.login csh

.cshrc csh

.x* X

.*rc almost anything

Bruce Hunter2/21/01 page 131


UNIX Part I
The shells

How many dot files do you have?

487% cd
488% ls .??*
.Xdefaults .loginx11 .profile
.Xdefaults.aix .logout .rhosts
.Xdefaults.aix.save .mailrc .rhosts.O
.Xdefaults.x10 .mh_profile .sh_history
.Xdefaults.x11 .mwmrc .twmrc
.Xdefaults.x11~ .mwmrc.aix .twmrc.N
.bash_history .mwmrc.aix.save .twmrc.NEW
.cshrc .netrc .twmrc.ncd
.cshrcx10 .newsrc .uwmrc
.exrc .newsrc.bak .xinitrc.aix
.history .nfs3B2
.ispell_words .nfs79 .xinitrc.x10
.login .nfs89 .xinitrc.x11
.loginx10 .nfs99

.nn:
LAST NEXTG select select.bak
489% ls .??*|wc -l
47
490%

Bruce Hunter2/21/01 page 132


UNIX Part I
The shells

.profile

The Bourne, bash, pdksh and Korn shells get their environments from
.profile when you log in. Here is a typical .profile.

PATH=/usr/ucb:/bin:/usr/bin:/usr/local/bin:/usr/intel/
bin
MAIL=/usr/spool/mail/$USER
export TERM MAIL PATH
biff n
# FMHOME line added by the FrameMaker setup program
FMHOME=/usr/system/sframe
PATH=:.:$FMHOME/bin:$PATH
export FMHOME PATH

PATH is your shell’s search path. MAIL is where your mail is stored, and
biff gives mail notification. tset sets the terminal characteristics.

Bruce Hunter2/21/01 page 133


UNIX Part I
The shells

sh files

/usr/bin/sh The sh command executable.

.profile Is the sh and ksh set-up file.

/etc/passwd Contains the source of home directories, UDI, GID


and the shell of choice.

Bruce Hunter2/21/01 page 134


UNIX Part I
The shells

.login

C shell executes .login just once, when you log on. This script is more or
less typical.

#!/bin/csh
set mail = /var/spool/mail/bhunter
biff n
stty erase ^H
set prompt = "\!% "
set EDITOR = vi
limit coredumpsize 0
set path = ( /usr/ucb /usr/bin /bin /usr/local/bin
/usr/system/bhunter/bin .)
set MANPATH = /usr/local/man

Bruce Hunter2/21/01 page 135


UNIX Part I
The shells

.cshrc

The file .cshrc is read (and reread) every time you fork a new shell, which
is just about every time you hit a carriage return from the terminal’s com-
mand line.

#!/bin/csh
alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
set history = 24
set prompt = ’\!% ’
setenv EXINIT "map z xp"
setenv FMHOME /usr/local/frame/3.0x
set path = ( $FMHOME/bin $path )
setenv MANPATH /usr/local/man
setenv EDITOR vi

Bruce Hunter2/21/01 page 136


UNIX Part I
The shells

C Shell Files

$HOME/.cshrc is read at the beginning of execution by each shell.


The .cshrc file is user-defined.

$HOME/.login is read by the login shell before the .cshrc file at login.

$HOME/.logout is read by the login shell at logoff.

/usr/bin/csh is the executable file.

/tmp/sh* contains the temporary file for here documents (<<).

/etc/passwd contains the source for home directories, UDI, GID, and
the shell for the file parameter.

Bruce Hunter2/21/01 page 137


UNIX Part I
The shells

ksh Files

$HOME/.kshrcIs used to control history.

/usr/bin/ksh Is the executable.

/tmp/sh* Contains temporary files that are created when


a shell is opened.

.profileThe set-up file for sh and ksh .

Bruce Hunter2/21/01 page 138


UNIX Part I
The shells

bash Files

/bin/bash

~/.bashrc

/etc/profile

~/.profile

When an interactive shell that is not a login shell is started, bash reads and
executes commands from ~/.bashrc, if that file exists. When invoked as a
login shell, bash first attempts to read and execute commands from /etc/
profile and then ~/.profile then reads and executes commands from
~/.bashrc.

zsh files

$ZDOTDIR/.zshrc
$ZDOTDIR/.zlogin
$ZDOTDIR/.zlogout
${TMPPREFIX}* (default is /tmp/zsh*)
/etc/zshenv
/etc/zprofile
/etc/zshrc
/etc/zlogin
/etc/zlogout

Bruce Hunter2/21/01 page 139


UNIX Part I
The shells

Quiz

Why don’t you want to load up .cshrc?

Where do you set history in .profile?

How do you get mail notification?

Does the parent shell know about its child’s new variables?

Where do you get your path to search?

What is a typical path?

Bruce Hunter2/21/01 page 140


UNIX Part I
The shells

Shell Command Substitution, Quoting


The single quotes (’...’) are used to preserve white space and prevent
sh from substituting its IFS (internal-field separator) or tokenizing a string.

grep ’now is’ foo.txt

Character strings in double quotes will have shell variables expanded.

echo "The path is $path"

When the Bourne shell reads command lines from a string between two
grave accents (‘‘), the standard output from these commands may be used
to set a variable. For example

nusers=‘who|wc -l‘

No interpretation is done on the string before the string is read, except to


remove backslashes. Backslashes may be used to escape a grave accent
(‘) or another backslash. If the command substitution lies within a pair of
double quotes (" ...‘ ...‘ ... "), a backslash used to escape a
double quote (") will be removed; otherwise, it will be left intact.

The \ character is also used for quoting. It escapes any character it pro-
ceeds. Some times it changes the meaning. For example

\*

tells the shell that the star is literal, not a file expansion metacharacters.
Commands like echo use

\n

to signify a newline character. For example

150% echo don\’t\n


don’t

Bruce Hunter2/21/01 page 141


UNIX Part I
The shells

Shell Quotes in a Nutshell

’ ’ preserve white space, prevent metacharacter and


variable interpretation.

"" preserve white space, interpret metacharacters and


variables

‘‘ run the enclosed command, return results

\ preserve the meaning of the next character or


give it a new one.

Examples:

% grep ’now is the time’ text.doc

sh "cp $f ${f}.bak"

n=‘who | wc -l‘

echo "Name? : \c"

Bruce Hunter2/21/01 page 142


UNIX Part I
The shells

Quiz

What will be the result of the following lines?:

% grep now is the time foo.txt

$ a=1 ; echo a${a}

$ a=don\t ; echo $a

n=‘ls .??* | wc -l‘


echo ’There are $n dot files’

grep ’From ’ $MAIL

Bruce Hunter2/21/01 page 143


UNIX Part I
The shells

sh Parameter Substitution
Shell variables are dereferenced, that is, the variable is read from its place
in storage when proceeded by a $.

echo $HOME

There are two types of parameters, positional and keyword. If parameter is


a digit, it is a positional parameter. For example

echo $1

will print the first command-line argument passed to a shell. They are used
in scripts. Positional parameters may be assigned values by set in csh or
simply with a = in sh. Keyword parameters are assigned values:

name=value

It can get more complicated:

${parameter}

The value, if any, of the parameter is substituted. The braces are required
only when parameter is followed by a letter, digit, or underscore that is not to
be interpreted as part of its name. It is used to clarify a variable or prevent
possible confusion. Here the character b, not the variable b, is concate-
nated to the variable a and stored in str.

a=1
b=2
str=${a}b

Bruce Hunter2/21/01 page 144


UNIX Part I
The shells

sh special characters, metacharacters

The following parameters are automatically set by sh:

# The number of positional parameters in decimal.

? The decimal value returned by the last


(synchronously executed) foreground command.

$ The process number of this shell.

! The process number of the last background command invoked.

$0 The program name

Here is $? in use. ls returns a 0 to the shell. $? displays it.

$ ls
PLAN mail.doc shell.doc.lck
$ echo $?
0
$

Here is a script that demonstrates the use of $0 and $$.

:
(
date
whoami
tty
hostname
)>$0.$$

Bruce Hunter2/21/01 page 145


UNIX Part I
The shells

The run of the script:

297% chmod +x sh.param


298% sh.param
299% top
total 194
-rw-r--r-- 1 bhunter system 55 Dec 20 13:11 sh.param.25332
-rwxr-xr-x 1 bhunter system 38 Dec 20 13:11 sh.param
-rw-r--r-- 1 bhunter system 4592 Dec 20 09:37 method

Bruce Hunter2/21/01 page 146


UNIX Part I
The shells

Here is a demonstration of $$, $#, and the positional parameters $1 - $9.

62% cat args


#!/bin/sh
echo -n "PID = "
echo $$
echo "there are $# arguments"
while [ $# != 0 ]
do
echo "$1"
shift
done
exit 0

The execution

63% args one two three


PID = 19994
there are 3 arguments
one
two
three
64%

Shell Scripts

The program on the last


page and the one on this
page are called “shell
scripts.” They are written in
an editor and executed by
simply giving their name to
the shell.

Bruce Hunter2/21/01 page 147


UNIX Part I
The shells

More sh Environmental Variables

The following parameters are used by the sh shell and passed to pro-
grams.

HOME The home directory used by the cd command.

PATH The search path for commands.

CDPATH The search path for the cd command.

MAIL Your mail file

PS1 Primary prompt string, by default $.

PS2 Secondary prompt string, by default >

IFS Internal Field Separator, normally SPACEs, TABs,


and/or NEWLINE.

Bruce Hunter2/21/01 page 148


UNIX Part I
The shells

sh Blank Interpretation

After parameter and command substitution, the results of substitution are


scanned for internal field separator characters and split into distinct argu-
ments. String literals enclose in quotes are taken as one string. This is
tokenizing.

The line

grep now is the time foo.file

is read and interpreted as a command (grep) followed by 5 arguments.


grep will see it a now as the RE and the rest as file names. The line

grep "now is the time" foo.file

is read as a command followed by just two arguments. In use grep will take
now is the time and pass it to grep as an RE so grep will scan the file
foo.file for it.

Bruce Hunter2/21/01 page 149


UNIX Part I
The shells

The Data Streams

UNIX can redirect the data stream. Every UNIX process has three open
stream minimally:

STDIN the input stream stream 0

STDOUT the output stream stream 1

STDERR the error stream stream 2

The numbers are file descriptors and are used in conjunction with the redi-
rection metacharacters.

Bruce Hunter2/21/01 page 150


UNIX Part I
The shells

sh redirection

All the shells recognize

> redirect to

>>redirect to with append

< redirect from

These direct the data stream to or from files (or devices). sh also has these

<&digit

Use the file associated with file descriptor digit as standard input. Similarly,
it’s used for the standard output using >&digit.

<&- The standard input is closed. Also the other way using >&-.

If any of these is preceded by a digit as well, the file descriptor which will be
associated with the file is that of the digit (instead of the default 0 or 1).
For example

... 2>&1

associates file descriptor 2 with the file currently associated with file descrip-
tor the STDERR joins STDOUT so

testprog 2> errfile

sends the error stream to errfile while

testprog 2>&1

combines the error stream into STDOUT.

Bruce Hunter2/21/01 page 151


UNIX Part I
The shells

The order in which redirections are specified is significant. The shell evalu-
ates redirections left-to-right. For example

... 1>bar 2>&1

first associates file descriptor 1 with file bar. It associates file descriptor 2
with the file associated with file descriptor 1 (namely, file bar). If the order of
redirections are reversed, file descriptor 2 is associated with the terminal
(assuming file descriptor 1 had been) and file descriptor 1 is associated with
file bar.

157% sh
$ cat foo bar 1>good+bad 2>&1

Note there is no visible output.

$ cat good+bad

Wed Nov 10 14:55:06 PST 1993


1
22
333
4444
55555
666666
7777777
88888888
999999999
end
cat: 0652-050 Cannot open bar.
$

Bruce Hunter2/21/01 page 152


UNIX Part I
The shells

C Shell STDERR Redirection

In C shell you can combine STDOUT and STDERR and send both to one file a
little more easily. Again, you have a file foo, but no bar, so an error will
generate.

558% cat foo bar >& error


559% cat error

Wed Nov 10 14:55:06 PST 1993


1
22
333
4444
55555
666666
7777777
88888888
999999999
end
cat: 0652-050 Cannot open bar.

Bruce Hunter2/21/01 page 153


UNIX Part I
The shells

Redirection Examples

Here is redirection at its simplest:

550% echo date>date.f

Now redirection with append to the same file:

551% date>>date.f

The results:

304% cat date.f


Date
Tue Dec 20 14:01:20 PST 1994

Now let’s redirect in the other direction:

554% sed ’s/Date/date:/’<date.f


date
Tue Dec 20 14:01:20 PST 1994

A classic:

if grep ’true’ txt.file>/dev/null

Bruce Hunter2/21/01 page 154


UNIX Part I
The shells

Here the STDERR is captured into an error file, while the STDOUT is dis-
played.

$ cat foo bar 2>error.f

Wed Nov 10 14:55:06 PST 1993


1
22
333
4444
55555
666666
7777777
88888888
999999999
end

$ cat error.f
cat: 0652-050 Cannot open bar.

Bruce Hunter2/21/01 page 155


UNIX Part I
The shells

sh Special Commands / Metacharacters

: Has no effect, the command does nothing, but a zero exit


code is returned. This is sometimes used to start Bourne
shell scripts.

. filename Read and execute commands from filename and


executes in the current shell. It is like source in csh.

Bruce Hunter2/21/01 page 156


UNIX Part I
The shells

csh set

syntax:
set [ -aefhkntuvx- [ argument ... ] ]

Here are a few set flags;

-e Exit immediately if a command exits with a nonzero exit status.

-n Read commands but do not execute them.

-t Exit after reading and executing one command.

-u Treat unset variables as an error when substituting.

-v Print shell input lines as they are read.

-x Print commands and their arguments as they are executed.

As an example

set -x

at the top of a script will turn on debugging,

Bruce Hunter2/21/01 page 157


UNIX Part I
The shells

Here is a shell script with the x flag set.

#!/bin/sh -x
echo "Name : "
read name
dir=`pwd`
echo "$name, you are in $dir"
exit 0

Now it runs with debugging on.

27% !$
myscript
+ echo Name :
Name :
+ read name
Arnold
+ pwd
dir=/usr/system/bhunter/junk
+ echo Arnold, you are in /usr/system/bhunter/junk
Arnold, you are in /usr/system/bhunter/junk
+ exit 0
28%

Now with no x flag.

30% myscript
Name :
Rambo
Rambo, you are in /usr/system/bhunter/junk
31%

Bruce Hunter2/21/01 page 158


UNIX Part I
The shells

csh, bash, tcsh, zsh, ksh, psksh Job Control

You can start a job in background with & in any shell.

$ du / > /tmp/hogs &

Everything except sh however allows more control. A job running in the


foreground can be suspended with a ^Z.1

118% du />/tmp/du.udg
^Z
Suspended

Use the command jobs to get the job number and status.

119% jobs
[1] + Suspended du / > /tmp/du.udg
120%

It now can be restarted in background with bg.

120% bg %1
[1] du / > /tmp/du.udg &
121% jobs
[1] Running du / > /tmp/du.udg

Use fg If you want to bring it back into foreground.

122% fg %1

1. SIGSTOP

Bruce Hunter2/21/01 page 159


UNIX Part I
The shells

Example, Job Control in zsh


The program:

#!/usr/intel/bin/zsh
# name: long_job
n=0
while [ $n -ne 10 ]
do
echo $n
(( n += 1))
sleep 10
done

Execution in zsh with job control.

% long_job
0
1
2
^Z
zsh: done long_job
% jobs
[1] + done long_job
% fg %1
[1] + continued long_job
3
4
5
6
7
8
9
%

Bruce Hunter2/21/01 page 160


UNIX Part I
The shells

You will probably want to protect a job in background from being accidently
killed by a signal 1, the hangup or SIGHUP signal.

% nohup du / > /tmp/du.hogs &


[1] 1135

Now if it has to be killed, it will take a little more work and the command
kill. Use the PID number

% kill 1135

or the csh job number.

% kill %1

Bruce Hunter2/21/01 page 161


UNIX Part I
The shells

Quiz

You just changed your .profile. You do a

$ . .profile

Will it do you any good?

You just changed your .login. You do a

% . .login

Will it do you any good?

You do a

% du ..

from your home directory. It’s taking time and you need your screen. What
are your options?

Bruce Hunter2/21/01 page 162


UNIX Part I
The shells

csh Built-In Commands


All built-in commands are executed within the C shell. If a built-in command
occurs as any component of a pipeline except the last, it is executed in a
subshell. Most of these you have seen before.

alias name def


Assigns def to the alias name. Examples are:

alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"

bg [%jobno] ...
Runs the current or specified jobs in the background.

bg %1

eval argument ...


Reads the arguments as input to the shell, and executes the
resulting command(s). This is usually used to execute
commands generated as the result of command or variable
substitution, since parsing occurs before these substitutions.

set c = ’ls -l ~’ ; eval $c

Bruce Hunter2/21/01 page 163


UNIX Part I
The shells

history [ -hr ] [ n ]
Displays the history list. If n is given, it displays only the n
most recent events.

-r Reverse the order of printout to be most recent first rather than


oldest first.

-h Display the history list without leading numbers. This is used


to produce files suitable for sourcing using the -h option to
source.

jobs
Lists the active jobs under job control. (not in sh)

-l
Lists process IDs, in addition to the normal information.

kill [ -sig ] [ pid ] [ %jobno ] ...

Sends a signal (default signal 15 to the job identified by


the PID of job number.

-l List the signal names that can be sent.

nice [ +n|-n ] command


Sets a jobs priority by incrementing the process priority value for
the shell or for command by n. The higher the priority value, the
lower the priority of a process, and the slower it runs. If no
increment is specified, nice sets the nice value to 4.
The range of nice values is from -20 through 19.

nohup command
Runs command with the hangup signal ignored.

Bruce Hunter2/21/01 page 164


UNIX Part I
The shells

set var = value


With no arguments, set displays the values of all shell variables.
With the var argument alone, set assigns an empty (null) value to
the variable var. With arguments of the form var= value, set assigns
value to var, where value is one of the following:

word A single word (or quoted string).

(wordlist) A space-separated list of words enclosed in parentheses

setenv VAR word


With no arguments, setenv displays all environment variables.
With the VAR argument it sets the environment variable VAR to
have an empty (null) value. With both VAR and word arguments,
setenv sets the environment variable NAME to the value word,
which must be either a single word or a quoted string.

setenv TERM vt100

source name
Reads commands from the file name and executes it in the
current shell.

source .login

unset pattern
removes variables whose names match pattern.

Source
The sh equivalent to source is dot. Just place a dot in front of
your command and it executes in the current (Bourne) shell.

$ . .profile

Bruce Hunter2/21/01 page 165


UNIX Part I
The shells

Preset Variables
Some shell variables have predefined meanings although they are not part
of the ENV:

argv Is the argument list. It contains the list of command line


arguments supplied to the shell as $1, $2, and so on.

cdpath Contains a list of directories to be searched by the cd command.

cwd Is the full path name of the current directory.

echo Echos commands (after shell substitutions).

home Is the user’s home directory.

ignoreeof
When set ignores EOF from terminals. This protects against
accidentally killing a C shell by typing a ^D.

noclobber
Restricts output redirection so that existing files are not
destroyed by accident. > redirections can only be made to
new files.

noglob Stops filename metacharacter substitution.

notify If set notifies you immediately as jobs are completed, rather than
waiting until just before issuing a prompt.

path Is the list of directories in which to search for commands.


path is initialized from the environment variable PATH, which the
C shell updates whenever path changes. The default is typically:
( /usr/ucb/usr/bin).

prompt Your prompt.

Bruce Hunter2/21/01 page 166


UNIX Part I
The shells

Setting Your Prompt

Traditional UNIX prompts are:

$ Bourne shell (ksh, bash, pdksh) family primary

> Bourne shell family secondary

% C family shell and zsh

# superuser

You can reset your prompt:

prompt = "‘date‘ $ " ; export prompt

which will give you

Mon Dec 16 09:44:45 PST 1996 $

or

setenv prompt "\! % "

Which will give you history.

533%

It is inadvisable to set your prompt to any prompt that is in use by another


utility such as & which is used by mail or < or > which are used by several
other utilities.

Bruce Hunter2/21/01 page 167


UNIX Part I
The shells

Using the Secondary Prompt

Many useful commands and constructs cannot complete on one line. As


such C shell will reject them. The other shells however can handle them just
fine and, in fact, you can even program form the command line, The follow-
ing fragment is done in zsh.

% cd perl
% for f in ‘ls‘
for> do
for> ls -ld $f
for> file $f
for> done
-rw-r----- 1 bhunter users 97 Jul 14 09:56
:shell script
-rwxr-x--- 1 bhunter system 310 Nov 11 1998 class.pl
class.pl: shell script
-rwxr-x--- 1 bhunter users 465 Jul 09 15:42 concreate
...

Bruce Hunter2/21/01 page 168


UNIX Part I
The shells

Which Shell Am I In?

If you cannot remember which shell you are in any particular window check
the window for its terminal name first

% tty
/dev/pts/62

The run ps and grep on sh.

$ ps -ef|grep sh | grep -v grep | grep bhunter


bhunter 13580 140800 0 Jun 30 pts/62 0:00 -csh
bhunter 32550 123686 1 Jul 09 pts/11 0:01 bash
bhunter 40506 13580 0 15:23:39 pts/62 0:00 zsh
bhunter 60758 62472 0 Jul 08 pts/51 0:01 bash
bhunter 62472 80380 0 Jun 30 pts/51 0:00 -csh
bhunter 72206 92678 0 Jun 30 pts/9 0:00 bash
bhunter 92678 85498 0 Jun 30 pts/9 0:00 -csh
bhunter 123686 18980 0 Jun 30 pts/11 0:00 -sh

It is also helpful to look at $0.

$ echo $0
bash

Bruce Hunter2/21/01 page 169


UNIX Part I
The shells

History

history allows you to use words from previous command lines in the com-
mand line you are currently typing. This can be used for spelling corrections
and typos and the repetition of complicated commands or arguments. Com-
mand lines are saved in a history list. The size of the history list is set by the
history variable. The most recent command is retained in any case. A
history substitution begins with a !

544% history
529 man ksh
530 pwd
531 vi sh.man
532 top
533 pwd
534 cd intro
535 top
536 vi sh.man
537 vi csh.man
538 sh
539 vi ~/.cshrc
540 echo *
541 glob *
542 ls
543 grep prompt ~/.??*
544 history
545%

Bruce Hunter2/21/01 page 170


UNIX Part I
The shells

Productivity Hint

Here is a C programmer’s use of history with the command results left out.

% vi myprog.c
% lint !$
% cc !$
% a.out
% !v
% !l
% !c
% !a

It saves a lot of typing and can be done from any shell other than sh!

Bruce Hunter2/21/01 page 171


UNIX Part I
The shells

Shell History-Event Designators

An event designator is a reference to a command-line entry in the history


list.

! Starts a history substitution, as in !456.

!! Repeats the previous command.

!n Repeats command-line n.

!-n Refers to the current command-line minus n.

!str Refers to the most recent command starting with str.


For example !vi brings back the most recent vi session.

Bruce Hunter2/21/01 page 172


UNIX Part I
The shells

Word Designators
You can retrieve parts of the last command line with word designators. Here
the user lints a program io.c and then repeats the argument using the
!* designator.

67% lint io.c


"io.c", line 14: warning: main() returns random value to
invocation environment
"stdio.h", line 204 ("llib-lc"): warning: function gets
return value is always ignored
"stdio.h", line 205 ("llib-lc"): warning: function puts
return value is always ignored
68% cc !$
cc io.c

Other word designators are

!! Repeat the last command

!# The entire command line typed so far.

!0 The first input word (command).

!n The nth argument as in !4

!$ The last argument.

!* All the arguments, or a null value if there is just one word in


the event.

Bruce Hunter2/21/01 page 173


UNIX Part I
The shells

csh, tcsh Command Line Printing and Editing

You can harmlessly print an old command line to the screen without it exe-
cuting with

!hist_number:p

as in

71% history
59 args
60 chmod +x args
61 args
62 cat args
63 args one two three
64 cd
65 cd c.c
66 ls
67 lint io.c
68 cc io.c
69 rm .aout
70 rm a.out
71 h
72% !60:p
chmod +x args
73%

You can edit the last command line with

^string^replacement

Taking the ability to print, you can combine it and

79% !60:p
chmod +x args
80% ^args^hosts
chmod +x hosts
81%

Bruce Hunter2/21/01 page 174


UNIX Part I
The shells

bash and zsh history and command line editing

bash runs history and editing very much like Windows32 and NT.

up arrow move back in history

down arrow move forward in history

right arrow moves right on the command line

left arrow moves left on the command line

delete removes the character to the left of the cursor

any character entered is inserted (to the left)

To replace a character place the cursor to the right of it, delete it, and type in
the new character.

ksh, pdksh command line editing

HIt escape and then use your vi editing commands to manipulate the cur-
sor and make changes, additions, and deletions.

Bruce Hunter2/21/01 page 175


UNIX Part I
The shells

Aliases
Shells maintains a list of aliases that you can create, display, and modify
using the alias and unalias commands. You already saw alias back a
few pages. Here it is again in these lines from .cshrc

alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
set history = 24
set prompt = ’\!% ’

Aliases can be removed with unalias. Aliases are a help in cutting down
on typing repetitive commands, but they can be overdone. With too many
aliases your csh will get slower than it already is. Minimize aliases. Take
advantage of having your own ~/bin and put small scripts there to do your
everyday commands. Aliases work in all shells except sh.

sh, zsh, ksh, pdksh, bash Alias, the Named Function

The Bourne shell named function was created to give sh aliases. Note the
syntax.

dir() {
ls -al | more
}

Another example

top() {
ls -lt | sed ’10, $d’
}

Bruce Hunter2/21/01 page 176


UNIX Part I
The shells

Passing Arguments to alias

Standard history argument designators exist for history arguments:

!^ first word

!$ last word

!2 word 2

!* all words

as in:

alias ri "rm -i \!*"

or:

alias flop "tar -\!^vf /dev/fd0c \!$ "

Note that the bang must be escaped.

Productivity Hints With Aliases

Get rid of aliases you don’t use. They slow down the shell and just about kill
csh.

Crate aliases to shorten long frequently used command names.

Use aliases as macros for frequently used commands and pipelines, e.g

alias top "ls -lt | sed -n ’1,10p’"

Bruce Hunter2/21/01 page 177


UNIX Part I
The shells

Quiz

Commands like cd, test, and ls are built-in to the shell for faster execu-
tion. Do you think they execute in the current shell when used in a pipeline?

Pipelines can only exist in the same shell or


between shell execed processes that are
descendents of a common parent that initi-
ated the pipeline.

Bruce Hunter2/21/01 page 178


UNIX Part I
The shells

ksh Features Different from csh


ksh History
ksh also has history. To get history, set HISTSIZE in.profile

HISTSIZE=10
export HISTSIZE

To set the ksh prompt to your current history number assign it to the
prompt variable.. It will behave very much like C shell.

set prompt="[!] "

and you prompt will look like

[53]

The history command is history. To reinvoke command 48

!48

To repeat the old argument that started with x

!x

Take all the arguments from that command 14

!14:*

Bruce Hunter2/21/01 page 179


UNIX Part I
The shells

The Number-One Productivity Tool, Scripts

Nothing boosts user productivity more then task automation through the use
of scripts. The shells are not alone as scripting languages. There is also
PERL and Tcl but shell scripts are faster an most often easier to write.
Advantages of shell scripts:

Truly portable and do not have to be compiled or recompiled

Can be created in any editor

Quick to write, much quicker than PERL or Tcl

Much easier to learn then PERL or Tcl

The advantages of PERL over shell scripts

More robust

Better for long programs

Easier to debug

More versatility

Bruce Hunter2/21/01 page 180


UNIX Part I
The shells

The Shells As Programming Languages

Any commands that you type at the terminal can be put into a text file and
executed. These are called scripts. Scripts are used to shorten repetitive
tasks. They can be very simple.

104% cat rmail


#!/bin/sh
rm /var/spool/mail/bhunter

They can be a little more complex with flow control and other programming
constructs.

#!/bin/sh
for dir in ‘ypcat passwd|awk -F: ’$3>99 { print $6 }’‘
do
echo $dir
du $dir|sed -n ’$p’
done
exit
# udu
# shows each users total disk usage

Here is a short but nasty script used, once, a long time ago.

97% cat cleanx


#!/bin/csh
while (sleep 10)
rm /tmp/x[0-9]*.c
rm /usr/tmp/x[0-9]*.c
end
# cleanx
# removes x-number-char-dot C files from temp areas
# kludge for the Morris worm
# removes worm source files
# bhh 11/3/88

Bruce Hunter2/21/01 page 181


UNIX Part I
The shells

Shell Scalar Variables


Ordinary variables, as opposd to presets or environmenatl variables, are
created on the fly.

#!/bin/sh
a=3
b=4
c=‘expr $a + $b‘
echo "$a + $b = $c"

The variable is proceeded by a $ to dereference it.

Shell Arrays

ksh, bash, etc. have arrays. Typeset the array with the -u flag.

#!/bin/ksh
typeset -i i=0
typeset -i len
typeset -u names

for name in ‘who | sed ’s/ .*//’‘


do
names[$i]=$name
((i = $i + 1))
done
len=$i
((i = 0))

while [ $i -lt $len ]


do
echo ${names[$i]}
((i = $i + 1))
done

Note the syntax for the dereference, ${names[$i]}

Bruce Hunter2/21/01 page 182


UNIX Part I
The shells

Get Input From the STDIN

To get input from the keyboard use the read statement.

echo "name: \c"


read name

csh use set var = $<

Send Output To the STDOUT

Use echo to write to the STDOUT. To suppress the newline use echo -n
(old) or \c.

echo "name: \c"


read name
echo "Hi $name"

Lab

Write a script in sh to ask for the user’s name.

name? : McLane
hi McLane

Don’t forget to make it executable.

Bruce Hunter2/21/01 page 183


UNIX Part I
The shells

Arguments a.k.a. Positional Parameters

The shells have built-in command line arguments called positional parame-
ters. Let’s write a script called foo.

#!/bin/sh
echo $1

and then execute it

%myprog bar
bar

The positional parameter $1 is the first argument, and here it is echoed. A


variable $# stores how many arguments there are. It is like argc in C.

If you write

#!/bin/sh
echo "you have $# arguments"

you get the results

194% argc 1
you have 1 arguments

195% sh argc 1 2 3 4 5 6 7 8 9 0
you have 10 arguments

Bruce Hunter2/21/01 page 184


UNIX Part I
The shells

Lab

Write a script to take command line arguments of the user’s first and last
name and then output them as Last, First.

bash-2.00$ name_lab John Rambo


Name: Rambo, John

Bruce Hunter2/21/01 page 185


UNIX Part I
The shells

A Lab Answer

#!/bin/sh
echo "Name: $2, $1"

Bruce Hunter2/21/01 page 186


UNIX Part I
The shells

Comments

Comments start with a #. The rest of the line is ignored. The exception is
the first line.

#!/bin/csh
# does nothing
cd ; ls # added after command(s)

Permissions for Scripts

Scripts are not automatically runnable. They must have theri permissions
changes to make them run:

% chmod +x myprog

or

% chmod 755 myprog

Bruce Hunter2/21/01 page 187


UNIX Part I
The shells

exit

Shells automatically do an exit when they parse the last line. You can and
should use a deliberate exit if you exit anywhere else.

#!/bin/csh
exit 0
# comments

The return code is traditionally

• 0 OK

• 1 through N traceable error

• -1 really bad error

Bruce Hunter2/21/01 page 188


UNIX Part I
The shells

Decisions, the if

Programs branch on decisions.

echo "exit y/n? : "


read a
if [ "$a" = "y" ]
then
exit 0;
else
continue
fi

The if is looking for a true/false answer. 0 is true. if can be used to test the
exit code of a command.

if grep ’locked’ ./lockfile>/dev/null


then
continue
else
echo "File locked"
exit -1
fi

It can also be used with the results of the test command.

if test -f lock.file
then
echo "File locked"
exit -1
else
continue
fi

Bruce Hunter2/21/01 page 189


UNIX Part I
The shells

The if can test on the results of an expression based on a 0 / non-0 return.

#!/bin/ksh
typeset -i i=0
typeset -i len
typeset -u names

for name in ‘who | sed ’s/ .*//’‘


do
names[$i]=$name
((i = $i + 1))
done
len=$i
((i = 0))

while [ $i -lt $len ]


do
if (($i % 4))
then
printf "%-12.8s" ${names[$i]}
else
echo "${names[$i]}"
fi
((i = $i + 1))
done

Note: This script also runs in bash if you comment out the typeset -u.

Bruce Hunter2/21/01 page 190


UNIX Part I
The shells

test

The if and while most frequently use the test command to supply the
boolean true or false needed. Here are most of its flags.

-b FileName Returns True if FileName exists and is a block special file.

-c FileName True exit value if FileName exists and is a char’ special file.

-d FileName True exit value if FileName exists and is a directory.

-e FileName True if the specified FileName exists.

-f FileName True if the FileName exists and is a regular file.

-g FileName True value if FileName has Set Group ID bit set.

-h FileName True if the FileName exists and is a symbolic link.

-k FileName True if the FileName exists and has sticky bit is set.

-L FileName True if the specified FileName exists and is a link

-n String1 True if the length of the String1 is nonzero.

-p FileName Trueif the specified FileName exists and is a named pipe

-r FileName True if the specified FileName is readable

-s FileName True if the specified FileNam has a size greater than 0.

-u FileName True exit FileName has Set User ID bit is set.

-w FileName True if FileName exists and the write flag is on.


It will not be writable in a read-only file systam.

-x FileName True if FileName the execute flag is on. If it is a


directory you have permission to search in the directory.

Bruce Hunter2/21/01 page 191


UNIX Part I
The shells

-z String1 True if the length of the String1 variable is 0.

String1= String2 True if the String1 and String2 are identical.

String1!=String2 True if the String1 is not the same as String2

String True e if the String1 variable is not a null string.

Integer1 -eq Integer2 True if the Integer1 and integer2 are equal.
-ne, -gt, -ge, -lt, and -le can be used in place of -eq.

These can be combined with the following operators:

! Unary negation operator

-a Binary AND operator

-o Binary OR operator

Bruce Hunter2/21/01 page 192


UNIX Part I
The shells

Lab

Take the last lab and add a test to reject the commands use if two argments
are not pased.

Bruce Hunter2/21/01 page 193


UNIX Part I
The shells

A Lab Answer

#!/bin/sh
if [ $# -ne 2 ]
then
echo "use name_lab first_name last_name"
exit 1
fi
echo "Name: $2, $1"

Bruce Hunter2/21/01 page 194


UNIX Part I
The shells

Multi-Way Branch, case

The case is a multi-way branch like a long drawn out if, elsif,...
elsif, else. The variable after the keyword case is the switch. The
word switch is not used but is pattern matched on the argument(s) closed on
the right by a single parentheses. The switch *) is the default.

#!/bin/sh

n=‘who | sort -u | wc -l‘


echo "$n users, \c"
case n in
1|2|3|4|5)
echo light load
;;
6|7|8|9)
echo mid load
;;
*)
if [ $n -gt 9 ]
then
echo heavy load
else
echo no load
fi
;;
esac

Bruce Hunter2/21/01 page 195


UNIX Part I
The shells

Looping, The while

The while will loop on a true condition,

while /bin/true
do
echo -n "$n "
n=‘expr $n + 1‘
done

or a command results

while sleep 60
do
ps | grep -v grep | grep -s myprog
if $?
then

or a test

n = 1
while [ $n -ne 11 ]
do
echo -n "$n "
n=‘expr $n + 1‘
done

Bruce Hunter2/21/01 page 196


UNIX Part I
The shells

Lab

Write a script to do a countdown

bash-2.00$ !cd.lab
10
9
8
7
6
5
4
3
2
1

Bruce Hunter2/21/01 page 197


UNIX Part I
The shells

Lab Answers

#!/bin/sh
i=10
while [ $i -ne 0 ]
do
echo $i
i=‘expr $i - 1‘
done
echo bang!

#!/usr/intel/bin/zsh
typeset -i i=10
while [ $i -ne 0 ]
do
echo $i
((i = $i - 1))
done
echo bang!

Bruce Hunter2/21/01 page 198


UNIX Part I
The shells

Looping, the for

The shell for is a listing for, as opposed to an iterative for.

for n in 1 3 5 7 9
do
echo $n
done

It is common practice to reap the results of a pileline in a for.

for name in ‘who | sed ’s/ .*//’‘


do
echo $name
done

Bruce Hunter2/21/01 page 199


UNIX Part I
The shells

break and continue


To give you more control in a loop or case break breaks you out of the loop
while continue goes back to the top.

#!/bin/sh

while ‘/bin/true‘
do
echo "continue y/n: \c"
read a
case $a in
y)
continue
;;
n)
break
;;
*)
echo "can’t type?, try again"
continue
;;
esac
done
exit 0

Bruce Hunter2/21/01 page 200


UNIX Part I
The shells

Lab

Create a file foo with the contents

1
22
333
4444
55555
666666
7777777
8888888
999999999
00000000000

Write a script to read the file one line at a time and print the lines as long as
they have no zeros.

Bruce Hunter2/21/01 page 201


UNIX Part I
The shells

A Lab Answer

#!/bin/sh
i=1
for line in ‘cat foo‘
do
i=‘expr $i + 1‘
# 1234567890
if [ "$line" = "0000000000" ]
then
break
fi
echo "$i $line"
done

Bruce Hunter2/21/01 page 202


UNIX Part I
The shells

Integer Math in ksh, bash, zsh

Math in shell in not a lot of fun with constructs like

i=‘expr $i + 1‘

but in ksh and pdksh you can “declare” integers and do math operations as
long as the are in double sets of parentheses.

#!/usr/intel/bin/zsh
integer i=1
integer len=0
integer w=0
integer h=0
integer volume=0
integer sum=0

while [ $i -lt 3 ]
do
echo "length in feet: \c"
read len
echo "width in inches: \c"
read w
echo "heigth in inches: \c"
read h
echo "$w' by $h' by $len’"
((volume=$len*$h*$w*12))
((volume=$volume/1728))
print "$volume cu ft"
((i=$i+1))
((sum=$sum+$volume))
done
((sum=$sum+$sum))
echo "concrete required $sum cubic feet"

Bruce Hunter2/21/01 page 203


UNIX Part I
The shells

zsh Math

zsh has bash’s math features but is even more relaxed in it’s notation not
requiring type definitions.

#!/usr/intel/bin/zsh

echo "repeat test"


n=10
m=1
repeat $n
do
echo $m number :: $n
((m += 1))
done

Bruce Hunter2/21/01 page 204


UNIX Part I
The shells

csh Programming Issues

C shell is a resource pig.

C shell is fragile

C shell fails on constructs that the Bourne shell family scripts thrive on

C shell constructs cannot span multiple lines, sh family can.

The return from a successful command is seen as false by csh.

C shell behaves poorly with sed and major UNIX fitters.

Bruce Hunter2/21/01 page 205


UNIX Part I
The shells

Command Grouping

Each command executes in its own (sub)shell. This can lead to ugly, time-
consuming scripts.

#!/bin/sh
echo date > f.file
date >> f.file
echo users >> f.file
who>> f.file

By using parentheses to group commands you can get them all to execute
in one shell.

#!/bin/sh
(
echo date
date
echo users
who
)> f.file

C shell is limited in that it must do it all on one line.

#!/bin/csh
( echo dated; date ; echo users ; who )> f.file

This will give problems as the line gets longer.

Bruce Hunter2/21/01 page 206


UNIX Part I
The shells

Here Documents

Here documents allow a user to run an interactive process (or application)


from a shell script and feed it arguments, commands and so on, as if they
were typed in from the keyboard. The operator used is <<. It is followed by a
string that will terminate the input.

#!/bin/sh
ed profile<<END
/biff y/s//biff n/
w
q
END

It works in ksh and csh.

#!/bin/csh
ed profile<<END
/biff y/s//biff n/
w
q
END

Bruce Hunter2/21/01 page 207


UNIX Part I
The shells

sh Named Functions
Here is a part of a .profile with ll set as a named function.

:
ll(){
ls -l $1
}

Bourne shell has named functions. They can be used to make aliases or as
true functions.

#!/bin/sh
geta () {
while [ 1 ]
do
echo -n "enter yes or no : "
read ans
answ=‘echo $ans|cut -c 1‘
case $answ in
y|Y)
return
;;
n|N)
echo "exiting"
exit 1
;;
*)
echo "enter y or n"
;;
esac
done
}
echo -n "do you want to continue : "
geta
echo "script body"
exit 0

As you can see from this foil, there is a lot more to shell programming than
what you have covered so far.

Bruce Hunter2/21/01 page 208


UNIX Part I
The shells

Good Books

UNIX Shell Programming, Arthur & Burns

UNIX C Shell Field Guide, Anderson & Anderson

The Korn Shell, Bolsky & Korn

Bruce Hunter2/21/01 page 209


The vi Editor

vi
With a little sed and vim

Editing and
Regular Expressions

UNIX Part 5
Rev 6/3/99
ECT001118

Bruce Hunter 2/21/01 page 210


The vi Editor

Agenda

First we will do a crash course on vi to get the look and feel of it. You will
then study the ed editor since its commands are the basis for vi. Then you
will go through the vi command set. There will be labs at reasonable inter-
vals to reinforce what you have learned and to test yourself. A few advance
techniques will complete the course.

Bruce Hunter 2/21/01 page 211


The vi Editor
The UNIX vi Editor

Goals:
Learning to use the UNIX editors for

viewing text

inserting text

appending text

deleting text

moving text

copying text

going to the shell from within vi

The use of regular expressions

Advanced Techniques

formatting text in place

putting command results in text

inverting letters and words

cut and paste

Bruce Hunter 2/21/01 page 212


The vi Editor
vi and Productivity

Working in an editor and repeatedly typing the same mnemonic of repeat-


edly hitting the same F- key or arrow key is a waste of your time. Retyping
the same or similar text is a waste of time. Visually searching text is a waste
of time. Visually searching and then correcting is a waste of time. Even hav-
ing to manually insert a newline at the ned of each line is a waste of time.

Skillful and knowledgeable use of vi will save you from all of these
repeated operations. The advantages of vi over a simple editor like xedit
or pico:

Instant searching in either direction with wraparound

Rapid movement of text through the window or of the cursor through the text

One or two key mnemonics for: deletions, changes, insertions, and appends

Global search and replace

A fully developed regular expression engine

Built-in shell interaction

Ease of reading or writing full or partial files from within the text

Use of the mouse as an editing tool (vim)

Same instruction set as used in: sed, grep, egrep, ex. ed. etc.

Multiple buffers and full buffer access

Fully customizable

Full featured

Universal

Bruce Hunter 2/21/01 page 213


The vi Editor
A vi Crash Course

Before examining the intricacies of vi and its parent editor ed, you are
going to take a brief crash course in vi. This part of the class will have you
doing the exercises as the instructor lectures. It is a continuous lecture/lab..

There are many other editors on


UNIX. From X Windows you can
use

xedit

and in or out of X

ed

ex

vi and vim

emacs

pico

Bruce Hunter 2/21/01 page 214


The vi Editor
Preparing to use vi

The UNIX default editor is vi. It is easy to use and does a lot. It runs in three
modes. When you are entering text, it is in append mode. To leave append
mode, you type an escape. The editor then will be in vi mode where the
majority of letter keys have a special meaning. For example, the x removes
a letter, while the a puts you back in append mode. The third mode is ex or
ed mode, named after ed, the primitive editor. By entering a slash (/), you
go to the bottom line to execute a search. By entering a colon, you go to the
bottom line to enter a command. Typical commands are w for write, q for
quit, or wq for both.

You will use the most frequently used vi commands in the next set of exer-
cises.

Modes:

vi mode

input mode

ex mode

To display the mode :set showmode

Bruce Hunter 2/21/01 page 215


The vi Editor
Lab / Exercise

Try each command in vi as you go through the foil.

Enter
% vi exercise_1

Type a to add text. It stands for append. Type

I will learn to love the vi editor.

Enter a return. Then type

I’m learning now.

Hit return twice then type

This is a mistoook.

Enter a return again. Now hit <escape>. You are in command mode. Enter a
/. You’ll be on the bottom line, the “twenty-fifth line.” Type in ooo. You’ll be
at the ooo in mistoook. Type in x and one o will be removed. Type in cw to
change the rest of the word. Now type in ake. Then hit escape.

Type in b (back) once or twice to get back to the word a. Type i (insert) and
then type in not followed by a space. Now hit escape again. Type in o.
You’ll be down one line and in insert mode. Type a return. Now type

This is the last line.

And hit escape. Next we’ll move through the text.


Your file should look like

I will learn to love the vi editor.


I’m learning now.

This is not a mistake.


This is the last line.

Bruce Hunter 2/21/01 page 216


The vi Editor
Go to the command line to search with /. Type

/ing

And enter. You’ll be here.

I will learn to love the vi editor.


I’m learning now.

This is not a mistake.


This is the last line.

Type k to move up a line. Type j to move back. Type jjj and you will be
on the bottom. Hit / again then enter. You are back at ing. Type $ and you
are at the end of the line. Now type 0, and you are at the beginning.

Type w and move forward a word. Type 2w and move forward two. Now go
to the command line again, type :and hit return. Enter w and save the file.
Hit / and enter again and you are back at ing. Type h and move left one
character at a time. Type l and move right one character at a time.

Now type : from vi mode and go to the bottom command line and type

:wq

What have you learned so far?

Bruce Hunter 2/21/01 page 217


The vi Editor
A Short vi Summary

a append

i insert

<escape> go to command mode

j move down a line

k move up a line

l move right

h move left

x delete a character

cw change a word

r replace a character

0 move to the beginning of a line

$ move to the end of the line

/ initiate a search from the command line

? initiate a backwards search from the command line

: go to the command line to type a command

:w write the file

:q quit

Bruce Hunter 2/21/01 page 218


The vi Editor
Bring up the first exercise_1, file in vi.

I will learn to like the vi editor.


I’m learning now.

This is not a mistake.


This is the last line.

If the cursor is not at the beginning of the first line, put it there with an H
command. Let’s put the system date on the first line. Put a blank line above
the top line. Type in O in vi command mode. Now hit escape. Go to the
command line. Type

:r !date

Fri Nov 18 14:23:57 PST 1994


I will learn to like the vi editor.
I’m learning now.

This is not a mistake.


This is the last line.

Go to the blank line with:

/^$

What was that?? The ^ is the beginning of the line; the $ the end. It is a line
that begins with its own end — a blank line. Now remove it with a dd. Go to
the end of the file with a

:$

Add

This text appended


for exercise 3.

Bruce Hunter 2/21/01 page 219


The vi Editor
What line are you on? In command mode type a control G. You’ll see

~
~
~
"ex.1" line 6 of 6 --100%--

Put a tab in front of each line.

:1,$s/^/ /

There is a tab on the rignt side of the expression. This says "From line 1 to
the end of file ($) put a tab at the beginning of the line." Now get all the line
numbers with this command-line expression.

:set number

1 Mon Nov 21 09:52:17 PST 1994


2 I will learn to like the vi editor.
3 I’m learning now.
4 This is not a mistake.
5 This is the last line.
6 This text appended
7 for exercise 3.

Move to line 6.

:6

Type a J. The line is appended with the line below it.

1 Mon Nov 21 09:52:17 PST 1994


2 I will learn to like the vi editor.
3 I’m learning now.
4 This is not a mistake.
5 This is the last line.
6 This text appended for exercise 3.

Bruce Hunter 2/21/01 page 220


The vi Editor
Now take the numbers away.

:set nonumber

You can abbreviate it like this.

:se nonu

Look at the text again.

Fri Nov 18 14:23:57 PST 1994


I will learn to like the vi editor.
I’m learning now.
This is not a mistake.
This is the last line.
This text appended from exercise 3.

Move to the learn line.

/learn

Move the cursor to the v in vi. Type 2fv to find the second v.

Find the lines that end with a dot (period).

/\.$

This regular expression says "Look for a dot as the last character on the
line." Note the backslash escape for the dot. Now find the lines that don’t
end with a dot.

/[^.]$

This says “Look for a not-dot character at the end of the line.”

Bruce Hunter 2/21/01 page 221


The vi Editor
A Summary of Basic vi Commands

In vi mode:

a append

A append to end of the line

i insert

I insert at the begining of the line

j move down a line

k move up a line
H J K L
h move left

l move right

0 move to the begining of a line

$ move to the end of the line

H/M/L home/middle/last the cursor position on the screen

^D move the screen down a half page


Control
^U move the screen up a half page Characters

^F move the text forward a full screen

^B move the text back a full screen

x delete a character

cw change a word

r replace a character

Bruce Hunter 2/21/01 page 222


The vi Editor
R overwrite

J append the line below

O add a line above

o add a line below

u undo the last change

^G current line number

fa find a

:w write the file

:q quit

<escape> go to command mode

In ex command mode:

/ initiate a search from the command line

? search backwards

A few set commands:

number get line numbers (abbreviate nu)

wm= (wrapmargin) insert an automatic \n within N


characters of the end of line

flash set a visible bell (no beep)

hardtabs= set tabs to

showmode show when in input mode

Bruce Hunter 2/21/01 page 223


The vi Editor

Learn ed?

UNIX system administators should know ed. If the system won’t come up
into multi-user mode all you have is ed!

ed is the basis of most of the text-oriented commands. Filters like grep,


egrep, sed, awk. and the editors, ex and vi are based on ed. vi in its
command mode is in ex, therefore ed mode. It is important that you know
ed for its own sake and as parent to all the other commands.

ed

grep
ex sed
perl
egrep awk
vi

vim

Bruce Hunter 2/21/01 page 224


The vi Editor
What is vi?
From ed Berkeley created the ex editor. Both ed and ex are line editors;
you cannot see the document as you work on it. Just about everything you
need to know about ed applies to ex and vi.

The Creation of vi
BSD architect Bill Joy1 created vi, when he was at Berkeley. He knew that
a visual editor was necessary for UNIX’s success. The start was the cre-
ation of the ex editor. The next major goal was to create a visual editor, one
that you could see what it is that you are working on. Up until this time a
visual editor could only be on fixed proprietary systems where the terminals
were all of one kind, such as DEC VT-100 terminals. UNIX is created to use
any kind of a ASCII terminal.

/etc/termcap

To create a visual editor, first a system had to be created where each user
session could place the terminal characteristics of the users’ terminals
within each user’s environment. To do this, Bill Joy created termcap. Here
is a brief section of a termcap file showing the VT 100 entry.

dp|vt100-np|vt100 with no padding (for psl games):\\p


:cl=\E[H\E[2J:sr=\EM:cm=\E[%i%d;%dH:nd=\E[C:up=\E[A:\\p
:ce=\E[K:cd=\E[J:so=\E[7m:se=\E[m:us=\E[4m:ue=\E[m:\\p
:md=\E[1m:mr=\E[7m:mb=\E[5m:me=\E[m:tc=vt100:
d1|vt100-nam|vt100nam|vt100 w/no am:\\p
:am@:xn@:\\p
:is=\E>\E[?3l\E[?4l\E[?5l\E[?7l\E[?8h:
ks=\E[?1h\E=:ke=\E[?1l\E>:\\p :tc=vt100-am:

1. vice president of R&D Sun

Bruce Hunter 2/21/01 page 225


The vi Editor

Pattern Matching
Pattern matching is used to locate lines in text and set up operations like the
search and replace and the global search and replace. The ed editor sup-
ports pattern-matching characters that you can use as regular expressions
(REs) to construct pattern strings. You can use these patterns in addresses
to specify lines. For example do this to search for the pattern teh.

/teh

Search for the pattern teh and replace it (only one occurrence).

s/teh/the/

Search for the pattern teh and replace it with the globally

1,$s/teh/the/g

You can combine REs into patterns that match strings containing that same
sequence of characters. For example,

AB\*CD

matches the string

AB*CD

and

[A-Za-z]*[0-9]*

matches any string that contains any combination of alphabetic characters


(including none), followed by any combination of numerals (including none).

Bruce Hunter 2/21/01 page 226


The vi Editor
No one single skill, in UNIX, will be as important to you as the skillful use of
regular expressions. It is the keystone for productivity.

ns
Re

sio
gu
ns
Re

es
lar
sio
gu

pr
Ex
es

Ex
lar

pr
pr

lar
Ex

es
Ex

gu
pr

sio
lar

es

Re

ns
gu

sio
Re

ns

REs
ns
Re

sio
gu

ns
Re
es
lar

sio
gu
pr
Ex

es
lar
Ex
pr

pr
Ex
lar

es

Ex
pr
gu

sio

lar

es
Re

ns

gu

sio
Re

ns

Bruce Hunter 2/21/01 page 227


The vi Editor
Look at a few simple REs:

/^[Cc]at$/
is the word

Cat

or

cat

all by itself on a line. ^ is the beginning of the line, $ the end, and [Cc] is c
in either case. The (2) characters a and t stand for themselves.

/^[Cc]at[a-z][a-z]*$/
is a longer word starting Cat or cat but with more letters. The * means
0-infinity. A word like caterpillar would satesify the expression.

/^[^ ]/
Will look for a line that does not start with a blank.

The expressions above are pattern matches and can be used for searches
or addresses. The next is a substitution.

s/\(bomb\) \(smart\)/\2 \1/


takes the expression bomb smart and returns smart bomb. The \(\) are
a tag.

Bruce Hunter 2/21/01 page 228


The vi Editor
Characters and Metacharacters

Characters 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
(there are128) !@#$%^&*()_+|~[]{};:’"<>,./?
\n\r\t\v\f\a
[ ] [ ]

Not a1 character [^:] [^0-9] [^A-z] [^A-zO-9]

All characters match themselves but \{\} \[\]\(\) \& \1


some have to be escaped for quantifiers,
or tags and brackets need to be escaped
to be brackets.

The wild card .

Character ranges [A-z] [a-z] [A-Z] [0-9]

Character groups [aeiou] [13579] [Aa]

Quantity modifiers A* [A-z0-9]* [a-z][a-z]*


[^:]\{13\} 0\{1,10\}

Tag ^[a-z][a-z]*:\([^:][^:]*\):[0-9][0-9]

1. specific

Bruce Hunter 2/21/01 page 229


The vi Editor
Tagged Regular Expressions

Expressions can be tagged with escaped parentheses

\(foo\).*\(bar\)

This tags the expressions foo and bar. The character sequence \(Pat-
tern\) marks a subpattern that matches the same string it would match if it
were not enclosed.

Tag Numbers
Tagged expressions have tag numbers. In the example above foo is \1
and bar is \2. The characters \number match the same string of charac-
ters that a subpattern matched earlier in the pattern. number is a digit. The
pattern \number (for example \2) matches the string matched by the
occurrence of the subpattern specified by number, counting from left to right.

The expression

/\(foo\).*\(bar\)/\1\2/

replaces foo bar or fooeybar with foobar.

tags

/\(foo\).*\(bar\)/\1\2/
tag numbers

Bruce Hunter 2/21/01 page 230


The vi Editor
Addressing Lines
There are three types of addresses in the ed editor.

Line Number Addresses


Line number addresses are addresses relative to the current line and pat-
tern addresses. The current line (usually the last line affected by a subcom-
mand) is the point of reference in the buffer. The following are guidelines for
constructing addresses.

The Dot Character

A . (period) addresses the current line. This is the default for most ed sub-
commands and does not need to be specified.

Numbered Addresses

Number addresses reach the specified line number of (or in) the buffer.

Marked Addresses
‘x addresses the line marked with a lowercase ASCII letter, represented by
x, by the k subcommand (ed mode) or m subcommand in vi mode.

Bruce Hunter 2/21/01 page 231


The vi Editor
Relative Line Numbers

An address that begins with + or - specifies a line relative to the current line.
For example, -5 is the equivalent of .-5 (five lines above the current line).

Now let’s get tricky.

61% ed foo
69
1,$p
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
/33
333
+,$-1p
4444
55555
666666
7777777

Bruce Hunter 2/21/01 page 232


The vi Editor
There are a lot of ways to address a line. Look at the following text.

16 Description
17 The true command returns a zero exit value. The false command
18 returns a nonzero exit value. These commands are most often
19 used as part of a shell script.
20
21 Examples
22
23 To construct a loop that displays the date and time once each
24 minute, use the following code in a shell script:
25
26 while true
27 do
28 date
29 sleep 60
30 done
31
32 Implementation Specifics
...
36 Related Information
37
38 How to Create and Run a Shell Script in AIX Version 3.2 System
39 User's Guide: Base and Devices provides information on creating
40 and executing shell procedures.

17,19

is an acceptable adress range but so is

.,.+2

or

/^ The true/, /script\.?/

Bruce Hunter 2/21/01 page 233


The vi Editor
Practical Address Examples

Back up the entire file:

1,$w mytxt.bak

Remove from here to the end of file:

.,$d

Copy the next few lines to the end of the file:

.,.+4 co $

Delete (mail) from From to the first blank line:

/^From /, /^$/d

Read in from file mytxt to this line

.r mytxt

Delete the last few lines:

$-5,$d

Move the first paragraph to the end of the file:

1,/^$/mo $

Bruce Hunter 2/21/01 page 234


The vi Editor
Quiz

Explain:

1,$s/teh/the/g

s/^\([A-Z][a-z]+\) *\([A-Z][a-z]+\).*/\2, \1/

s/^[ ][ ]*//

s/ *$//

s/ */ /g

/^ *Implementation Spe.*/,/^ *These comm.*/d

/^ *\([A-Z][a-z]*\)/\1/

Bruce Hunter 2/21/01 page 235


The vi Editor
Here is a little PERL program to do comma-after-three-zero notation. See
how simple the REs are!

#!/usr/local/bin/perl5
while(<DATA>) {
s/0000$/0,000/;
while(/0000/) {
s/0000,/0,000,/
}
print
}
__END__
30000000
100000000000
100000000000000000

The results

1009% big_no.pl
30,000,000
100,000,000,000
100,000,000,000,000,000

Note that the line has been parsed from right to left.

Bruce Hunter 2/21/01 page 236


The vi Editor
Lab

Make a file copy of the true man page

man true >true.f

By using global commands with REs from the command line :

1) Change every occurrence of true to TRUE

2) Change every occurrence of false to FALSE

3) Change every occurrence of shell to Bourne shell

Look at the file first in vi. Titles may look like this

A^H F^HFI^HIL^HLE^HE

You are looking at character backspace (^H) character. To clean it up is a


lab in itself. On HP systems go to the vi command line and

:1,$s/\b.//g

On old BSD systems

:1,$s/^H.//g

To type in the backspace type \ first then hit Backspace.

Bruce Hunter 2/21/01 page 237


The vi Editor
Review; Modes in Vi

Like ed, vi runs in command mode and in append or insert mode, but it has
two command modes. One is the ex mode issuing commands from the
“25th line,” the command line at the bottom of the screen. The other is vi
mode done by issuing short mnemonic commands (subcommands) from the
screen, the text itself.

An example of the first, or ex mode, is writing the file. In command mode


you strike the colon (:) character and go to the bottom line. There you enter
the ed command w.

:w

The bottom line will then tell you what you wrote.

vi Mnemonics
vi has a huge set of mnemonics it uses in vi (command) mode. The mne-
monics use most of the characters on the ASCII keyboard. It uses them
plain, shifted, or as control characters. The numbers are used as repeat
characters.

Using vi For Reading Files


Editors are regularly used to read files. While you can use cat, type, or
more to see a file, only the editor will move you by lines, do complete jumps
in either direction and search for specific strings or expressions. Use
vi -R to invoke a read-only editor.

Moving Through Text


Once you have read the text initially on the screen you will want to move
through the text. To move a full screen down use ^F (forward). To move up
or back a full screen use ^B (back). You can move a half screen down with
^D (down) and back a half screen (^U) up. You can also search rapidly with
the ed / command from the bottom (25th) line. such as /vt100

Bruce Hunter 2/21/01 page 238


The vi Editor
More Movements
H Moves the cursor to the top line on the screen.

L Moves the cursor to the last line on the screen.

M Moves the cursor to the middle line on the screen.

Moving the Cursor Throught the Text


To really understand cursor movment commands, you have to look at the
qwerty keyboard, particularly the right hand side of the middle row of keys.

q w e r t y u i o p [ ]
a s d f g h j k l ; ’
z x c v b n m , . /
Your right fingers should rest on the characters jkl; and the h is there with
a small movement of the index finger. The leftmost key, h, moves the the
cursor down, its left neighbor, k, moves the cursor up.

h j k l

That it! Almost all the other keys have mnemonic values like u up, d down,
etc. For trickier movements use:

Bruce Hunter 2/21/01 page 239


The vi Editor

0 moves the cursor to the beginning of the line.

$ moves the cursor to the end of the line.

fx moves the cursor to the next x character.

Repeats
Let’s say your cursor is at the top of the screen, you want it near the bottom.
Use a number before the j, such as 25j, and you are at the bottom. A num-
ber before a mnemonic is a repeat.

Deletions
Oops!, you made a mistake! We all do, and that is why vi has so many
commands for removing and changing text and characters.

x Removes the character the cursor is sitting on. 2x will take 2,


3x 3, and so on.

dw Removes a word.

D Removes everything from the cursor to the end of the line.

dd Removes the line.

Changes
Sometimes you delete messes; other times you change them.

cw Changes the word from the cursor position to the last character in
the word. White space is seen as word separators.

r Allows the character that the cursor is on to be changed.

R Puts vi in strike-over mode until escape is hit again.

Bruce Hunter 2/21/01 page 240


The vi Editor
Adding a Line
To add a line below the cursor, type o; to add one above, type (uppercase)
O. The cursor will be positioned at the beginning of the new line, and you are
already in insert mode.

Append to the End of the Current Line


You are at the beginning or the middle of a line, and you want to add to the
end of it. Type A (not a) and you are at the end and in append mode.

Going to the End of the Line


You are at the beginning or the middle of a line, and you want to position the
cursor at the last character on the line. Type $.

Bruce Hunter 2/21/01 page 241


The vi Editor
Going to the Beginning of the Line
You are at the end or the middle of a line, and you want to position the cur-
sor at the first character on the line. Type 0.

Undoing a Command
A U, as an ex command, will undo the last edits and restore what was there
first.

Repeating a Command
The last command will be repeated if you type a dot (This will happen if you
want it or not because once in a while you will hit dot accidently.)

Change case

Move the cursor over the character and type ~.

Yanks and Puts


vi buffers its moves. When you delete a character, word, words, or a line
that deletion is buffered. You can get it back with a u but you can do a put,
too. A put is where you take what had been deleted and put it somewhere
else.

Say that you misstype the and get teh. Now put the cursor over the e and
hit x, and the e is gone. Without moving the cursor type in p, and the e is
back but now after the h .

teh

to

the

with just two keystrokes. Later you’ll see how to do it with one by mapping a
key (like z) to the x and p commands.

Bruce Hunter 2/21/01 page 242


The vi Editor
Try to swap lines. Do a dd on a line and follow it with a p. Start with

1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end

With the cursor on 999999999, strike dd, then p.

1
22
333
4444
55555
666666
7777777
88888888
0000000000
999999999
end

Now go back to 0000000000 and repeat your performance.

1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000

Bruce Hunter 2/21/01 page 243


The vi Editor
y is for yank
You don’t have to delete to put words or a line in the buffer, you can yank.

yw yank a word

yy yank a line

ayy yank the line into buffer a

Once yanked into the buffer, the buffer can be put with the p subcommand
(for after the cursor) and a P subcommand (for before the cursor.) Want to
repeat a line? Put the cursor at the end of the foo file and do a yy followed
by a p.

1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
end

Here’s one more exercise with named buffers. Put the cursor on one of the
ends and yank it to buffer with

"ayy

Now go to the top of the text and put the buffer in front of line 1.

"aP

Bruce Hunter 2/21/01 page 244


The vi Editor
Joining Lines
Frequently you need to join lines. Put the cursor anywhere on the line to be
joined and hit J. Try it with foo on the next to last end.

end
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end end

Accidently Joining Lines


You reach out for the a key and touch caps lock. Oops! Now you try to move
your cursor down rapidly and think you’re typing jjjjjj... but you’re typ-
ing JJJJJJJJJJJ. There is no eraser for this one. You have fused a dozen
lines into one.

Finding a Letter on a Line


The f (find) mnemonic followed by a letter will move the cursor to that letter.
for example:

fM will move the cursor to the first letter M on the current line

Fx Moves the cursor from the end of the line, to the last x character while

2fc will move the cursor to the c in character in the same line.

Bruce Hunter 2/21/01 page 245


The vi Editor
Lab

1)Bring up truef in vi.

2) Search for Des and get the line number.

3) Line number the file.

4) Unset the numbers.

Bruce Hunter 2/21/01 page 246


The vi Editor
Those are the vi commands. Now the ex commands.

ex and ed Editor Commands in vi


In vi ed and ex command are typed from the bottom or “25th line.” Many
ex commands are like ed commands.

Deleting a block of lines


.,.d

as in

:1,15d

The : is the prompt for the bottom (ex) command line. This last example
Removes lines 1 through 15.

Moving Text
.,.m N

as in

1,15m $

Let’s start to repair our mutilated foo file. You type in 1m $ to move the first
line to the last on the ex command line and get

end
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end end

Bruce Hunter 2/21/01 page 247


The vi Editor

Now one final deletion

:$-1d

and

1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end

Bruce Hunter 2/21/01 page 248


The vi Editor
Copying Text

Now the tree is OK; let’s mess it up again and copy all of it to its end.

:1,$copy $
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000

You could have done the same thing with

1,$co $

or

1,$t $

Bruce Hunter 2/21/01 page 249


The vi Editor
Addresses and Pattern Matches
Addresses don’t have to be numbers, they can be patterns. If you are any-
where but the first line in the foo file, you can go to the command line and
type

:/^1$/

which is a pattern that becomes the address. Now let’s add begin to the top
of the file.

begin
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end

and write it to a file by pattern matching the addresses.

:/begin/,/end/w snarg

Bruce Hunter 2/21/01 page 250


The vi Editor
Shell Escapes from the vi Command Line

You can fork a shell1 from vi and execute about anything you like. The sim-
plest way is to create a shell and do whatever you have to do outside of vi.

:!sh

When you have done your task, strike ^D and your back in your vi session.

You can execute a command from shell and put its results in you’re vi file.
For example this will do a listing of the current directory and read it in at the
current cursor position.

:r !ls

Writing a Partial File From vi


If you want to write a partial file from the file you currently have in vi, type

:$-20,$w temp

which writes the file temp and puts the last 20 lines of your vi buffer in it.

Reading a File Into Your Text


Anything readable can be read into you current file. For example

:r foo

reads the foo file at the current cursor position.

1. a default shell can be specifeid in .exrc

Bruce Hunter 2/21/01 page 251


The vi Editor
Saving a File

The compound command

:wq

will write the file and exit vi. The commands

:x

or from vi mode

ZZ

does the same thing.

You can do a u from visual mode


or a U from ex mode.

U/u

Bruce Hunter 2/21/01 page 252


The vi Editor

Quitting vi
The command

:q

will quite vi unless you have made changes. If so, and you are serious
about quitting without saving, do this:

:q!

Getting the Current Line Number


To get the number of the line you are on, do a

:^G

and the bottom line will look a little like this.

~
~
~
“bar” The cursor is at line 1 of 12 --8%-- .

Bruce Hunter 2/21/01 page 253


The vi Editor

Customizing vi

Like all things UNIX, you can customize vi to your preferences. You can
set characteristics

: set redraw term=xterm wrapmargin=10

To see all the settings use set all

:set all
noautoindent nomodeline noslowopen
autoprint nonumber nosourceany
noautowrite nonovice tabstop=8
nobeautify nooptimize taglength=0
closepunct=’".,;)]} tags=tags /usr/lib/tags
directory=/var/tmp partialcharacter=- term=xterm
noedcompatible prompt noterse
noerrorbells noreadonly timeout
noexrc redraw ttytype=xterm
flash remap warn
hardtabs=8 report=5 window=40
noignorecase scroll=20 wrapscan
linelimit=1048560 sections=NHSHH HUuhsh+c wrapmargin=0
nolisp shell=/bin/csh nowriteany
nolist shiftwidth=8 wraptype=word
magic noshowmatch
mesg showmode

You can set editor characteristics in your .cshrc, .login, or .pro-


file. Execute the env command to see them.

Bruce Hunter 2/21/01 page 254


The vi Editor
Mapping
You can map your keys to do special vi functions. The key z or Z isn’t used
for much so, let’s use that one. This will have the z key become a mnemonic
to reorder reversed characters.

map z xp

Entering Shell Commands

The following subcommands allow you to run a UNIX command within the
vi editor. Enter these subcommands in command mode.

:sh
Enters the shell to allow you to run more than one UNIX command.
You can return to the editor by pressing the ctrl-D key sequence.

:!command
Runs the specified UNIX command and then returns to the editor.

Bruce Hunter 2/21/01 page 255


The vi Editor

Special Tricks

Seeing the invisible — set list


Is that space a series of space characters, or is it a tab? To see the unsee-
able use set list.

Here is text as you normally see it.

1
22
333
4444
55555
666666
7777777
88888888
999999999
end

Now see it with set list.

^I1$
^I22$
^I333$
^I4444$
^I55555$
^I666666$
^I7777777$
^I88888888$

Bruce Hunter 2/21/01 page 256


The vi Editor
Buffers

The last 9 deletions are stored in numbered buffers. To retrieve any of these
buffers use

"Np

where N is the buffer number as in

"4p

If you take our foo file and delete the lines 1 through 9, starting at line 1,
and then execute "9p, then "8p, .... and you get

end
999999999
88888888
7777777
666666
55555
4444
333
22
1

You can see that buffer 1 was the first, buffer, 2 the second and so. Now
buffer 1 holds the oldest deletion, and buffer 9 the latest.

Bruce Hunter 2/21/01 page 257


The vi Editor
Saves and Retreives

To simply do a save ( if you expect a power glitch, the server, or the net to
burp) do a

:w

If you do get hit with a crash a copy of your file is saved by UNIX and can be
retrieved by invoking vi with the r flag and file name.

% vi -r filename

Bruce Hunter 2/21/01 page 258


The vi Editor
Formatting Text in Place

It’s a good idea to enter only one sentence to the line. Place a newline after
every period. It makes way for further editing.

It is line oriented and will not display your text


unless you specifically request to display (print) it.
ed is the father of all UNIX editors up to vi.

What is vi?

From ed Berkeley created the ex editor.


Both ed and ex are line editors, you can not see the
document as you
work on it.
Just about everything you need to know about ed applies
to ex and vi.
To invoke ed you type in ed followed by the file name.

$ed foo

Bruce Hunter 2/21/01 page 259


The vi Editor
This allows massive editing like moving text. It is not ready for final presen-
tation, however. Originally this sort of text was formatted in nroff and/or
troff. Today you use marker-language formatters like FrameMaker and
import the files. You can still, however, use nroff to format the document
and do it right in vi.

Place the nroff command

.ad l

at the top of your document.

.ad l

It is line oriented and will not display your text


unless you
specifically request to display (print) it.
ed is the father of all UNIX editors up to vi.

What is vi?

In vi (not ex) command mode, at the top line, enter !G.


It will take you down to the bottom line.

~
~
~
~
~
!nroff

Bruce Hunter 2/21/01 page 260


The vi Editor

Now see your text

It is line oriented and will not display your text


unless you specifically request to display (print) it.
ed is the father of all UNIX editors up to vi.

What is vi?

From ed Berkeley created the ex editor. Both ed and ex


are line editors, you can not see the document as you
work on it.

Just about everything you need to know about ed appiles


to ex and vi. To invoke ed you type in ed followed by the
file name.

$ed foo

It is right justified and “filled.” The !G had us escape to shell to execute


nroff but the G let you do it to all (Global) the text — in place.

Bruce Hunter 2/21/01 page 261


The vi Editor
Setting Tabs

You can reset tab stops with the tabstop attribute.

: set tabstop=4

With this setting, the tabs will be at modulo 6 rather then the default of 8.

Abbreviations

Do you type the same large word over and over? If you do, you can get vi
to type it for you by setting the ab attribute.

: ab SA system administration

Now when ever you type SA, vi will expand it to system administra-
tion.

Bruce Hunter 2/21/01 page 262


The vi Editor
Customization

EXINIT

You can semi permanently set vi with the EXINIT environmental variable
in your .cshrc, .login, or .profile. Then you will have the vi settings
you like or need every time you bring up vi.

83% grep EXINIT .??*


.cshrc:setenv EXINIT "map z xp"
.loginx11:setenv EXINIT "set ai aw sw=4 redraw map z xp"
84%

.exrc

The .exrc file is read just before initiating a vi session.

43% pwd
/usr/system/bhunter
44% cat .exrc
set wrapmargin=10
set tabstop=6
map z xp
ab SA System Administration
45%

Bruce Hunter 2/21/01 page 263


The vi Editor
vim, Improved vi

vim has lots of enhancements over vi like multi level undo, multi windows
and buffers, command line editing, filename completion, on-line help, visual
selection, automatic man pages for UNIX commands, and so on.

What more could vi do? How about if you could use the mouse to help you
edit? In vim you can. Start with set.

:set mouse=a

Now the mouse works with the editor. Move it and you won’t need the h, j,
k, and l keys to move the mouse. Mark text and the following functions
work:

c change the marked text

x delete the marked text

y yank the marked text

Bruce Hunter 2/21/01 page 264


The vi Editor
sed

By learning vi you already know about half of what you need to run sed.
sed is the streaming editor. Remember the vi line for a global replace of
teh to the?

:1,$s/teh/the/g

Let’s run a document through sed and make the same corrections.

sed ’s/teh/the/g’ mytext.doc

That’s it? Yes it’s that easy. Now in vi remove the first line of text.

:1d

Now from sed

sed ’1d’ mytext.doc

What can sed do that vi can’t or would have a hard time with? How about
search for and then remove everything on one line.

sed ’/AMD/d’ venddor_list.dat

Or make additions automatically

sed ’/Intel/ \a
vendor of choice’ venddor_list.dat

There is a whole lot more but that will have to wait for UNIX Part II.

Bruce Hunter 2/21/01 page 265


The vi Editor
Good Books

Learning the vi Editor by Linda Lamb (O’Reilly & Associates., Inc)

A Guide To vi by Dan Sonnenschein (Prentice-Hall)

The UNIX Programming Environment, Kernighan and Pike


(Prentice-Hall1)

Related Productivity Tools

spell

look

ispell

vim

nroff / troff

1. a really good section on ed

Bruce Hunter 2/21/01 page 266


UNIX part I
UNIX Devices: Terminals, Printers,...

Devices

UNIX Part I, section 7


Rev 7/13/99
ECT001118

Bruce Hunter 2/21/01 page 267


UNIX part I
UNIX Devices: Terminals, Printers,...

UNIX reaches all of its physical devices through device drivers built into the
kernel. The entry point to devices is found in the directory /dev. Let’s look at
a terminal.

216% ls -l /dev/tty*
crw-rw-rw- 1 root system 1, 0 Feb 18 14:24 /dev/tty
crw-rw-rw- 1 root system 16, 0 Oct 21 13:42 /dev/tty0

This doesn’t look like any files or directories you have seen so far. That’s
because it’s not a regular file; it’s a special file.

character special file

link count

crw-rw-rw 1 root system 1, 0

major device number

minor device number

A disk would look a little different.

brw------- 1 root system 15 0 Dec 14 1992 /dev/hdisk0


brw------- 1 root system 15,1 Dec 14 1992 /dev/hdisk1

These are block special files. More about them later.

Bruce Hunter 2/21/01 page 268


UNIX part I
UNIX Devices: Terminals, Printers,...

Look at these floppy disk listings on a Linux system:

315% ls -l fd*
lrwxrwxrwx 1 root root 15 Jan 19 19:08 fd ->
../proc/self/fd
brw------- 1 root root 2, 0 May 5 1998 fd0
brw------- 1 root root 2, 12 May 5 1998 fd0D360
brw------- 1 root root 2, 16 May 5 1998 fd0D720
brw------- 1 root root 2, 28 May 5 1998 fd0H1440
brw------- 1 root root 2, 12 May 5 1998 fd0H360
brw------- 1 root root 2, 16 May 5 1998 fd0H720
brw------- 1 root root 2, 4 May 5 1998 fd0d360
brw------- 1 root root 2, 8 May 5 1998 fd0h1200
brw------- 1 root root 2, 20 May 5 1998 fd0h360
brw------- 1 root root 2, 24 May 5 1998 fd0h720
brw------- 1 root root 2, 1 May 5 1998 fd1
brw------- 1 root root 2, 13 May 5 1998 fd1D360
brw------- 1 root root 2, 17 May 5 1998 fd1D720
brw------- 1 root root 2, 29 May 5 1998 fd1H1440
brw------- 1 root root 2, 13 May 5 1998 fd1H360
brw------- 1 root root 2, 17 May 5 1998 fd1H720
brw------- 1 root root 2, 5 May 5 1998 fd1d360
brw------- 1 root root 2, 9 May 5 1998 fd1h1200
brw------- 1 root root 2, 21 May 5 1998 fd1h360
brw------- 1 root root 2, 25 May 5 1998 fd1h720

Note the major number 2. They all have the same device driver. The minor
numbers and names reflect three different disk densities.

Bruce Hunter 2/21/01 page 269


UNIX part I
UNIX Devices: Terminals, Printers,...

Your Terminal

If you start at a bare screen, you are on a tty. If you are in a window you
are on a pseudo terminal.

218% ls -l /dev/*tty*
crw-rw-rw- 1 root system 1, 0 Feb 18 14:24 /dev/tty
crw-rw-rw- 1 root system 16, 0 Oct 21 13:42 /dev/tty0
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp0 -> /dev/pts/0
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp1 -> /dev/pts/1
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp2 -> /dev/pts/2
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp3 -> /dev/pts/3
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp4 -> /dev/pts/4
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp5 -> /dev/pts/5
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp6 -> /dev/pts/6
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp7 -> /dev/pts/7
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp8 -> /dev/pts/8
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp9 -> /dev/pts/9
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypa -> /dev/pts/10
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypb -> /dev/pts/11
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypc -> /dev/pts/12
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypd -> /dev/pts/13
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttype -> /dev/pts/14
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypf -> /dev/pts/15
219%

It’s not real; it’s virtual. Its driver is a pseudo driver, which does all its work in
software.

What termianl are you on? Try tty.

% tty
/dev/pts/9

Bruce Hunter 2/21/01 page 270


UNIX part I
UNIX Devices: Terminals, Printers,...

stty

Terminal’s hardware and termio characteristics are set with stty. To see
your terminal’s setting use

223% stty -a
speed 9600 baud; 47 rows; 80 columns;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D;
eol = ^@eol2 = ^@; start = ^Q; stop = ^S; susp = ^Z;
dsusp = ^Y reprint = ^R discard = ^O; werase = ^W; lnext
= ^V -parenb -parodd cs8 -cstopb hupcl cread -clocal -
parext -ignbrk -brkint -ignpar -parmrk -inpck -istrip -
inlcr -igncr icrnl -iuclc -ixon -ixany -ixoff -imaxbel
isig icanon -xcase echo echoe echok -echonl -noflsh
-tostop echoctl -echoprt echoke -flusho -pending iexten
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel
224%

Old UNIX used the @ as an erase character. Let’s do it again.

88% stty erase @

Now if I make a mistake

%pe@wd

will be accepted as

%pwd

To set it back to ^H, type it as

% stty erase ^H

Bruce Hunter 2/21/01 page 271


UNIX part I
UNIX Devices: Terminals, Printers,...

Terminal Type, TERM, termcap and tset

Your UNIX environment knows its terminal type because you set it when you
logged on. It should have been in one of your dot files.

tset -n -I
export TERM

The tset command first determines the type of terminal used, then per-
forms necessary initialization and mode settings. The flags used here

-n

on systems with the Berkeley 4.3 tty driver, specify that the new tty driver
modes should be initialized for this terminal while

-I

suppresses transmission of terminal initialization strings. Your environmen-


tal variable TERM may later be reset.

229% env
TERM=xterm
SHELL=/bin/csh

with lines like

setenv TERM xterm

or

set noglob; eval ‘tset -s -I -Q $TERM‘; unset noglob

from a .loginx11 script.

Bruce Hunter 2/21/01 page 272


UNIX part I
UNIX Devices: Terminals, Printers,...

How do you know what terminal you’re on? Ask the machine with tty.

232% tty
/dev/pts/4
233%

Your terminal knows about how to handle its cursor from termcap.

vs|xterm|aixterm|vs100|xterm terminal emulator :\


:cr=^M:do=^J:nl=^J:bl=^G:le=^H:ho=\E[H:\
:co#80:li#65:cl=\E[H\E[2J:bs:am:cm=\E[%i%d;%dH:nd=\E[C:up=\E[A:\
:ce=\E[K:cd=\E[J:so=\E[7m:se=\E[m:us=\E[4m:ue=\E[m:\
:md=\E[1m:mr=\E[7m:me=\E[m:\
:ku=\EOA:kd=\EOB:kr=\EOC:kl=\EOD:kb=^H:\
:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:ta=^I:pt:sf=\n:sr=\EM:\
:al=\E[L:dl=\E[M:ic=\E[@:dc=\E[P:\
:MT:ks=\E[?1h\E=:ke=\E[?1l\E>:\
:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;3;4;6l:\
:rs=\E[r\E<\E[m\E[2J\E[H\E[?7h\E[?1;3;4;6l:xn:\
:AL=\E[%dL:DL=\E[%dM:IC=\E[%d@:DC=\E[%dP:\
:ti=\E7\E[?47h:te=\E[2J\E[?47l\E8:\
:hs:ts=\E[?E\E[?%i%dT:fs=\E[?F:es:ds=\E[?E:

It may look like modem line noise to you, but to your system it tells every-
thing it needs to know about your screen. Only with this will vi, more, ...
work.

Bruce Hunter 2/21/01 page 273


UNIX part I
UNIX Devices: Terminals, Printers,...

Quiz

You just got an old VT-100. How do you set up your environment for it?

You need to put a Wyse 50 on as a terminal. How do you find out if it can be
accepted by more or vi?

Lab

Set your line erase (kill) character to @.

Try it.

Echo ’#’

Set kill back to ^U.

Find out what your TERM variable is set to.

Bruce Hunter 2/21/01 page 274


UNIX part I
UNIX Devices: Terminals, Printers,...

Printers

We may be heading for a paperless society, but for now a lot of trees are
becoming paper for printers. To send someting to a printer, you must first
spool it with a spooling system, and there are no less than three printer
queuing (spooling) systems used in UNIX today.

lp AT&T UNIX spool system


fm32f2pub1

lpr BSD spool system

qprt AIX spool system

What’s in a name?

f2: pole F2

2: second floor

3: fm3

fm: Folsom

pub1 the first (public) system there

Bruce Hunter 2/21/01 page 275


UNIX part I
UNIX Devices: Terminals, Printers,...

BSD’s lpr Spooler System

lpr is the BSD printer command. If you have a printer called lw1, you send
a file to it with

% lpr -P lw1 /etc/motd

The -P flag sets the destination (printer name.)

Did your job get to the printer queue? You can test the queue with lpq

244% lpq
Queue Dev Status Job Files User PP % Blks Cp Rnk
------- ----- --------- --- ------- - ---- -- ----- -- ---
lps20 dplps READY
lps20: no entries
245%

If you mess up and send a binary file to the printer, you can get rid of it with
lprm. There may be a problem, however, if your job went to a print server.

Bruce Hunter 2/21/01 page 276


UNIX part I
UNIX Devices: Terminals, Printers,...

What’s a Spooler?

With multiple users sending print requests to multiple printers, a spooler


system is needed to hold the printer requests and files so they can be sent
to printers one at a time.

request

spooler

printer

Bruce Hunter 2/21/01 page 277


UNIX part I
UNIX Devices: Terminals, Printers,...

The UNIX System V Print Spooler

The UNIX System V print spooler has its own set of commands. The print
command is lp and the device (printer) destination flag is -d, so the com-
mand line

$ lp -depson /etc/motd

will get the message of the day to the dot-matrix printer.

The spooler must be initialized by the printer administrator before it works.


The spooler must be started

lpsched

Then the the printer must be

enabled

and

accepted

by the print administrator before you can use it. How do you know if it has?
Use the command lpstat with the -p flag and the printer name.

$ lpstat -p epson
printer epson is idle. enabled since \
Fri Feb 18 19:17:11 1994. available.

This message means that the printer epson is ready to print, and there are
no jobs ahead of yours.

Bruce Hunter 2/21/01 page 278


UNIX part I
UNIX Devices: Terminals, Printers,...

lp

enable
disable

spooler (lpsched)

accept
reject

Switchies on the LP spooler

Bruce Hunter 2/21/01 page 279


UNIX part I
UNIX Devices: Terminals, Printers,...

Which Spooler Do You Have?

How do you know which print spooler your system has? Look in the area
/var/spool or /usr/spool for the printer area. Do a listing.

ls -CF /usr/spool

If you see a directory lp and it has contents like

SCHEDLOCK default logs system


admins fifos model temp
bin forms.readme requests

you definitely are going to have to use lp. The command lpr is usually
aliased to lp but is not the BSD lpr command.

Bruce Hunter 2/21/01 page 280


UNIX part I
UNIX Devices: Terminals, Printers,...

Another way of testing is to look at the lp* executables.

ls -l /usr/bin/lp*

---x--s--x 2 bin lp 58338 Mar 15 1993 /usr/bin/lp


-r-xr-xr-x 1 bin sys 7183 Mar 11 1993 /usr/bin/lpinstall
---x--s--x 2 bin lp 58338 Mar 15 1993 /usr/bin/lpr
-rws--s--x 1 lp lp 9910 Mar 15 1993 /usr/bin/lprint
-rwx--x--x 1 bin bin 70736 Jan 19 1993 /usr/bin/lprof
---x--s--x 1 bin lp 93030 Mar 15 1993 /usr/bin/lpstat

You can see that lpr is really lp, and the lpstat command is there.

Bruce Hunter 2/21/01 page 281


UNIX part I
UNIX Devices: Terminals, Printers,...

Killing A Request On the Same System Doing the


Printing

If you send a binary file to any printer, or a postscript file to a non-postscript


printer, you will need to cancel the job. The command is cancel which
cancels requests to line printer. Its syntax is

cancel [request_ids] [printers]

cancel cancels printer requests made by lp. The command line argu-
ments may be either request ids, as returned by lp, or printer names. For a
complete list, use lpstat. Specifying a printer cancels the request that is
currently printing on that printer. .

Note that you get a message when you send a print request to the printer

$ lpr -depson lp.foil


requestid is epson-3 (1 file)

It is that request id that you need to cancel the print request.

$ cancel epson-3

On networked systems with shared printers (which is most systems), the


print request will probably reach the printer server before you get a chance
to kill it. The most positive move then is to cancel the spool request at the
print server. Note that the print server is most likely to be running under the
lpr spooler system.

Bruce Hunter 2/21/01 page 282


UNIX part I
UNIX Devices: Terminals, Printers,...

Quiz
What are the printing, queue-observation, and cancel commands for

1) System V

2) BSD

3) AIX

Lab
Find out which spooler system you have on your system.

Send /etc/motd to the printer.

Check the queue.

Bruce Hunter 2/21/01 page 283


UNIX part I
UNIX Devices: Terminals, Printers,...

Floppy Drives

Nearly all workstations and UNIX PCs nearly all have floppy drives. The
5 1/4 inch format used to be a standard but is now largely replaced by the
smaller 3 1/2. A floppy drive with diskette can be mounted as a UNIX file
system, but is used most often as an archive media like a tape or CD.

UNIX has multiple commands to write to any disk, or diskette, or tape. A few
are

tar

cpio

cp (if it’s a mounted file system only)

dump (for backup)

The syntax to get to a floppy with tar is

tar cf /dev/diskette_name file file ...

To retrieve a file

tar xvf /dev/diskette_name [file, file, ...]

To look at the contents of a diskette

tar tvf /dev/diskette_name

Bruce Hunter 2/21/01 page 284


UNIX part I
UNIX Devices: Terminals, Printers,...

tar example

255% tar cvf /dev/fd0 sh.man


a sh.man 83 blocks.
256%

256% tar tvf /dev/fd0


-rw-r--r-- 100 0 42139 Feb 03 09:47:46 1994 sh.man
257%

Bruce Hunter 2/21/01 page 285


UNIX part I
UNIX Devices: Terminals, Printers,...

What Is The Drive’s Name?

One quick way to get the name of your floppy device is with a listing of
/dev. Here is a look at AIX.

42% ls -l /dev/fd*
brw-rw-rw- 1 root system 11, 0 Feb 15 07:52 /dev/fd0
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/fd0.18
brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 /dev/fd0.9
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/fd0h
brw-rw-rw- 1 root system 11, 2 Dec 14 1992 /dev/fd0l

Note the link counts and minor numbers. fd0h is, really fd0.18 while fd0
is really fd0.9. Are these block devices only?

43% ls -l /dev/rfd*
crw-rw-rw- 1 root system 11, 0 Dec 14 1992 /dev/rfd0
crw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/rfd0.18
crw-rw-rw- 1 root system 11, 2 Jan 16 07:54 /dev/rfd0.9
crw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/rfd0h
crw-rw-rw- 1 root system 11, 2 Dec 14 1992 /dev/rfd0l

The rfd’s are raw devices, used for formatting. Another source is looking at
the man page for diskette formatting. AIX is being currently phased out.
Here is the top of a Linux workserver’s floppy disk lisitng.

315% ls -l fd*
brw------- 1 root root 2, 0 May 5 1998 fd0
brw------- 1 root root 2, 12 May 5 1998 fd0D360
brw------- 1 root root 2, 16 May 5 1998 fd0D720
brw------- 1 root root 2, 28 May 5 1998 fd0H1440

Note the densities as part of the name.

Bruce Hunter 2/21/01 page 286


UNIX part I
UNIX Devices: Terminals, Printers,...

Diskette Formats For Sun, SCO, and IBM

It is convenient to transfer system files by floppy diskette. This system of file


transfer is sometimes called sneakernet. Before you can use a diskette it
must be formatted. There is a problem, however, between UNIX systems.
The problem is a lack of consistency in device naming and in format com-
mands. However, there is consistency in formats, both low and high density.
For example, Sun uses fdformat, SCO uses format, and IBM uses both
but with different syntax.

The most common, and therefore portable, format is “high” density 135
tracks per inch (80 tracks) with 18 sectors for a capacity of 1.4M.

OEM device syntax

IBM fd0h fdformat /dev/fd0h

Sun fd0c fdformat -h /dev/fd0c

SV fd1135ds18 format /dev/fd1135ds18

tar is the most convenient for small transfers but IBM’s version insists on
using a hyphen

tar -xvf /dev/fd0h

Whereas Sun keeps with the old traditional Version 7

tar xvf /dev/fd0c

For convenience, you can use low density on IBM 6000s

fdformat

Bruce Hunter 2/21/01 page 287


UNIX part I
UNIX Devices: Terminals, Printers,...

Man Page Example, Linux Folppy Format

FDFORMAT(8) Linux Programmer's Manual FDFORMAT(8)

NAME
fdformat - Low-level formats a floppy disk

SYNOPSIS
fdformat [ -n ] device

DESCRIPTION
fdformat does a low level format on a floppy disk. device is usually one
of the fol-
lowing (for floppy devices, the major = 2, and the minor is shown for
informational
purposes only):

/dev/fd0d360 (minor = 4)
/dev/fd0h1200 (minor = 8)
/dev/fd0D360 (minor = 12)
/dev/fd0H360 (minor = 12)
/dev/fd0D720 (minor = 16)
/dev/fd0H720 (minor = 16)
/dev/fd0h360 (minor = 20)
/dev/fd0h720 (minor = 24)
/dev/fd0H1440 (minor = 28)

/dev/fd1d360 (minor = 5)
/dev/fd1h1200 (minor = 9)
/dev/fd1D360 (minor = 13)
/dev/fd1H360 (minor = 13)
/dev/fd1D720 (minor = 17)
/dev/fd1H720 (minor = 17)
/dev/fd1h360 (minor = 21)
/dev/fd1h720 (minor = 25)
/dev/fd1H1440 (minor = 29)

Obviously a lot more needs to be explained so look for another man page.

Bruce Hunter 2/21/01 page 288


UNIX part I
UNIX Devices: Terminals, Printers,...

The related command is fd. It goes on to explain the formats the fd drivers
can do.

Name Capac. Cyl. Sect. Heads Base minor #


----------------------------------------------------------
fdnd360 360K 40 9 2 4

5.25 inch high density device files:

Name Capac. Cyl. Sect. Heads Base minor #


----------------------------------------------------------
fdnh360 360K 40 9 2 20
fdnh410 410K 41 10 2 48
fdnh420 420K 42 10 2 64
fdnh720 720K 80 9 2 24
fdnh880 880K 80 11 2 80
fdnh1200 1200K 80 15 2 8
fdnh1440 1440K 80 18 2 40
fdnh1476 1476K 82 18 2 56
fdnh1494 1494K 83 18 2 72
fdnh1600 1600K 80 20 2 92

3.5 inch double density device files:

Name Capac. Cyl. Sect. Heads Base minor #


----------------------------------------------------------
fdnD360 360K 80 9 1 12
fdnD720 720K 80 9 2 16
fdnD800 800K 80 10 2 120
fdnD1040 1040K 80 13 2 84
fdnD1120 1120K 80 14 2 88

3.5 inch high density device files:

Name Capac. Cyl. Sect. Heads Base minor #


----------------------------------------------------------
fdnH360 360K 40 9 2 12
fdnH720 720K 80 9 2 16
fdnH820 820K 82 10 2 52
fdnH830 830K 83 10 2 68

Bruce Hunter 2/21/01 page 289


UNIX part I
UNIX Devices: Terminals, Printers,...

fdnH1440 1440K 80 18 2 28
fdnH1600 1600K 80 20 2 124
fdnH1680 1680K 80 21 2 44
fdnH1722 1722K 82 21 2 60
fdnH1743 1743K 83 21 2 76
fdnH1760 1760K 80 22 2 96
fdnH1840 1840K 80 23 2 116
fdnH1920 1920K 80 24 2 100

3.5 inch extra density device files:

Name Capac. Cyl. Sect. Heads Base minor #


----------------------------------------------------------
fdnE2880 2880K 80 36 2 32
fdnCompaQ 2880K 80 36 2 36
fdnE3200 3200K 80 40 2 104
fdnE3520 3520K 80 44 2 108
fdnE3840 3840K 80 48 2 112

The points to consider here are about man page lookup. First point: There
are 9 separate manuals 1 - 8 and n. Not everyting is in man 1. Second look
at the bottom of each man page for See Also. It is a pointer to the next man
page. To get a special man page like fd from section 4 use this format:

% man 4 fd

Bruce Hunter 2/21/01 page 290


UNIX Part I
mail

Mail

UNIX Part I
Section 8
Rev ed 7/13/99
ECT001118

Bruce Hunter 2/21/01 page 291


UNIX Part I
mail
Using Mail Productivity

Many employees spend up to an hour or more a day sending and receiving


mail. Understanding how mail works and taking advantage of all parts of the
mail system can save a lot of time and increase efficiency and accuracy.
“Tricks” you will learn about using mail:

Checking for messages without bringing up mail.

Editing your document within mail.

Manipulating your mail file directly from within the editor.

Creating a document outside of mail and redirecting it into mail.

Rapidly reading and deleting unimportant mail (with two keystrokes.)

Getting mail notification without disturbing whatever else you are doing.

Customizing mail for automatic expansion of abbreviations.

Customizing (mapping) keys to shortcuts in mail.

Aliasing mail addresses to save typing time.

Bruce Hunter 2/21/01 page 292


UNIX Part I
mail
What is mail?

Mail is a loose term email, even looser. In the computer sense mail and
email are utilities by which we can send and receive mail. Traditional-UNIX
users use the command mail, but even that is an over simplification. UNIX
mail is done with the mail command. UNIX “mail” is set up to send mail

on the local machine

over UUCP

over the Internet

It can receive mail from all three as well.

In reality, there is a lot more than the mail command. There is a three-level
hierarchy of agents.

user agents

routing agent

delivery agents

Bruce Hunter 2/21/01 page 293


UNIX Part I
mail

/ucb/mail /bin/mail mh elm USER AGENTS

sendmail ROUTING AGENT

local (/bin/mail) TCP/IP UUCP DELIVERY AGENTS

Agents Used In Mail Deleivery

Bruce Hunter 2/21/01 page 294


UNIX Part I
mail
The user agents are the commands typed to send or receive mail like mail,
elm, or mh. mail can be one of two versions

/bin/mail AT&T mail user agent

/usr/ucb/mail BSD mail agent

All mail processed by these user agents are processed by the routing agent,
usually sendmail. Final delivery is managed by the delivery agent, which is
/bin/sh on the local machine, uux for UUCP, or the Ethenet for Internet
delivery.

To make understanding mail easer, we will deal deal with the user agent
/usr/ucb/mail for a while.

Bruce Hunter 2/21/01 page 295


UNIX Part I
mail
Who Can You Send Mail To?
Who Can You Receive Mail From?

Mail can be delivered to or received from

Anyone on your local system, usually limited to you,

Anyone in your mail “domain.”

Anyone on the Internet (therefore the world).

Deliver, however, may be restricted by your organization and by the Intel


gateway.

Bruce Hunter 2/21/01 page 296


UNIX Part I
mail
What’s A Mail Domain?

Medium sized to large organizations like to make the whole organization a


single mail domain. Larger organizations subdivide into mail domains. Fre-
quently these domains correspond to internal organizations or divisions, but
they don’t have to.

For example the domain

fm.intel.com

is the home of the domain residing on a system by thename of pcocd2

Bruce Hunter 2/21/01 page 297


UNIX Part I
mail
UNIX Mail Addressing

Surface mail requires an address; so does mail. If you put a note in an


envelope and put just the recipients name on it, it will be delivered only if
you put it on her desk. Similarly, if you mail to just a name, as in

mail deastman

Mr. Eastman will get it only if he’s on the local machine on in the local
domain. Beyond that you will need more than the person’s log-on name.
Here is an internet-style address.

mail chad@linus.mitre.org

What’s it all about?

The recipient’s log-on name

The name of the recipient’s system

chad@linus.mitre.org

The name of the organization

The major domain name

This is the “canonical” form. Note that the user’s name is separated from the
system name with an @. The system from the organization and the organiza-
tion from the major domain is separated with a dot.

Bruce Hunter 2/21/01 page 298


UNIX Part I
mail
Messages and Envelopes

Mail, as a whole, consists of a message and an envelope. With mail, the


message is what you type or include, and the header is the separate infor-
mation needed to send the mail. Here is a header.

From INTEL.ENTERPRI@intel.com Wed Feb 16 15:45:29 1994


Return-Path: <intel.ENTERPRI@fm1n.intel.com>
Received: from trngtrng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-
4.1)
id AA23663; Wed, 16 Feb 94 15:45:25 PST
Received: from INSIDE.intel.COM (fm1n.intel.com) by
trng.intel.com with SMTP id A
A07390
(5.65c/IDA-1.4.4); Wed, 16 Feb 1994 15:45:23 -0800
Received: by INSIDE.intel.COM
(Soft*Switch Central V4L380P5) id 412341150094047FCCFM;
16 Feb 1994 15:35:15 GMT
Message-Id: <CCFM.ENTERPRI.412341150094047FCCFM@INSIDE.intel.COM>
Date: 16 Feb 1994 15:35:15 GMT
From: "Enterprise Change R" <intel.ENTERPRI@fm1n.intel.com>
Subject: WW08 ENCO Change Summary Pt2
To: "BOB BAUER" <intel.BBAUER@fm1n.intel.com>,
.
.
"JOHN REECE" <jreece@sousa.intel.com>,
"RODNEY RUBERT" <intel.RRUBERT@fm1n.intel.com>,
"AJAY SEVAK" <intel.ASEVAK@fm1n.intel.com>
(4 entries truncated from copylist)
Comment: MEMO 1994/02/16 15:39

Bruce Hunter 2/21/01 page 299


UNIX Part I
mail
Let’s send a simple local mail message and look at its header.

46%
46% mail bhunter
Subject: greetings
Good morning!
.
Cc:
47% hostname
jaeger
48%

From bhunter@trng.intel.com Thu Feb 17 08:08:26 1994


Return-Path: <bhunter@trng.intel.com>
Received: from trng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-4.1)
id AA19947; Thu, 17 Feb 94 08:08:25 PST
Received: from jaeger ([132.233.71.32]) by trng.intel.com with
SMTP id AA17748
(5.65c/IDA-1.4.4 for <bhunter@trng.intel.com>); Thu, 17 Feb
1994 08:08:23 -0800
From: Bruce H Hunter <bhunter@trng.intel.com>
Received: by jeager (AIX 3.2/UCB 5.64/trng-SMTP-version-5-3-93)
id AA22340; Thu, 17 Feb 1994 08:08:23 -0800
Date: Thu, 17 Feb 1994 08:08:23 -0800
Message-Id: <9402171608.AA22340@jeager>
To: bhunter@trng.intel.com
Subject: greetings

Good morning!

49%

Bruce Hunter 2/21/01 page 300


UNIX Part I
mail
Note the key lines in the header

Received: from trng.intel.com (fmsu04-2) by fmsu01

From: Bruce H Hunter <bhunter@trng.intel.com>

Received: by jaeger (AIX 3.2/UCB 5.64/trng-SMTP-version-5-3-93)


id AA22340; Thu, 17 Feb 1994 08:08:23 -0800

Note that although the mail was sent “locally,” actually it has traveled
through a mail master.

From bhunter@trng.intel.com Thu Feb 17 08:08:26 1994


Return-Path: <bhunter@trng.intel.com>
Received: from trng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-4.1)
id AA19947; Thu, 17 Feb 94 08:08:25 PST

That is because /var/spool/mail is mounted from the mail master.

52% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 900 78% 871 85% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 748 93% 364 8% /var
/dev/hd3 8192 7868 3% 36 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 153712 81% - - /misc
fmsu01:/usr/spool/mail 408391 288293 29% - - /usr/spool/mail

Bruce Hunter 2/21/01 page 301


UNIX Part I
mail
Mail is stored in /var/spool/mqueue just before delivery. There are at
least two files for each message.

fmsu01# ls -l
total 2
-rw------- 1 root 28 Feb 17 07:50 dfAA18373
-rw------- 1 root 0 Feb 17 07:50 lfAA18373
-rw------- 1 root 682 Feb 17 07:50 qfAA18373
-rw-r--r-- 1 root 0 Feb 17 07:50 xfAA18373
fmsu01# pwd
/var/spool/mqueue

The qf (or queue) files are the header files. The df files are the data. These
files stay just momentarily and are them combined and sent to your own
mail file in/var/spool/mail. Here is a qf file. It will be translated into a
mail header.

fmsu01# cat qf*


P1525
T761500592
DdfAA18892
S<root@trng.intel.com>
H?P?return-path: <root@trng.intel.com>
Hreceived: from trng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-4.1)
id AA18892; Thu, 17 Feb 94 07:56:32 PST
H?x?full-name:
Hreceived: from intruder (intruder.intel.com) by trng.intel.com
with SMTP id AA16536
(5.65c/IDA-1.4.4 for <dhudock@trng.intel.com>); Thu, 17 Feb 1994
07:56:31 -0800
HFrom: Operator <root@trng.intel.com>
Hreceived: by intruder (4.1/trng-SMTP-version-5-3-93)
id AA11703; Thu, 17 Feb 94 07:56:59 PST
Hdate: Thu, 17 Feb 94 07:56:59 PST
Hmessage-id: <9402171556.AA11703@intruder>
HTo: dhudock@trng.intel.com
Hsubject: in.tftpd--eng050-224a.fm.intel.com-132.233.50.240
R<dhudock>

Note the sender and recipient addresses.

Bruce Hunter 2/21/01 page 302


UNIX Part I
mail

Your mail file in /var/spool/mail belongs to you. You are the owner.

28% cd ../mail
29% pwd
/var/spool/mail
30% ls -l bhunter
-rw------- 1 bhunter 36922 Feb 16 17:02 bhunter
31% whoami
bhunter
32%

Bruce Hunter 2/21/01 page 303


UNIX Part I
mail

Quiz
Where is your mail stored?

Who owns the file?

Lab

Send yourself a message.

Receive it.

What is its queue number?

What systems has it passed through?

What was the Internet address of the host that sent it?

Bruce Hunter 2/21/01 page 304


UNIX Part I
mail
Do I Have Mail?

There are several ways to get mail delivery notification.

biff Mail notification, a beep as mail arrives.

xbiff X biff version, a mailbox icon. Flag up means mail.

from A command shows who the message is from.

mail The mail command itself.

The from command will show who mail is from

42% from
From: "Enterprise Change R" <intel.ENTERPRI@fm1n.intel.com> Wed
Feb 16 15:45:29 1994
43%

biff must be built into one of your dot files. xbiff must be part of your X
dot files.

Bruce Hunter 2/21/01 page 305


UNIX Part I
mail
Sending Mail

Sending mail is simple. Let’s go back to the greeting message. The user is
on the local machine or in the local domain. Type in mail username

46% mail bhunter

You will be asked for a subject

Subject: greetings

Type it in and then send your message. Terminate it with a dot on a line all
by itself.

Good morning!
.
Cc:

The Cc line can be ignored if you don’t want courtesy copies. You can also
send a prepared mail (file) with redirection to the mail command.

mail deastman<ww7

Bruce Hunter 2/21/01 page 306


UNIX Part I
mail

No News Is Good News

You will receive no notification if the mail message you send is delivered
successfully. If not it will come back, you will be notified, and the message
will be in $HOME/deadletter or back in your mail.

Bruce Hunter 2/21/01 page 307


UNIX Part I
mail
Receiving (Reading) Mail

Use the mail command to receive mail.

57% mail
Mail [5.2 UCB] [IBM AIX 3.2] Type ? for help.
"/usr/spool/mail/bhunter": 6 messages 6 new
>N 1 intel.ENTERPRI@fm1n.intel.com Wed Feb 16 15:45 267/12292
"WW08 ENCO Chang
e Summary Pt2"
N 2 intel.ENTERPRI@fm1n.intel.com Wed Feb 16 15:47 322/15027
"WW08 ENCO Chang
e Summary (TCR"
N 3 intel.BADMINIS@fm1n.intel.com Wed Feb 16 16:51 60/2088
"FOLSOM (FWR) FACI
LITIES WORK "
N 4 intel.BADMINIS@fm1n.intel.com Wed Feb 16 16:52 122/3287 "CA
Materials Tec
hnology Analy"
N 5 intel.BADMINIS@fm1n.intel.com Wed Feb 16 17:02 86/4228
"Hotel Rooms are S
carce in Ari"
N 6 bhunter@trng.intel.com Thu Feb 17 08:08 17/646 "greetings"
&

The & is mail’s prompt. Type in its message number at the prompt to look
at a message.

& 6
Message 6:
From bhunter@trng.intel.com Thu Feb 17 08:08:26 1994
From: Bruce H Hunter <bhunter@trng.intel.com>
Date: Thu, 17 Feb 1994 08:08:23 -0800
To: bhunter@trng.intel.com
Subject: greetings

Good morning!

&

Bruce Hunter 2/21/01 page 308


UNIX Part I
mail
Deleting Mail

Type d at the prompt to delete the message.

&d

The next message will then appear.

& d
Message 5:
From intel.BADMINIS@fm1n.intel.com Wed Feb 16 17:02:25 1994
Date: 16 Feb 1994 16:52:16 GMT
From: "Bulletin Administra" <intel.BADMINIS@fm1n.intel.com>
Subject: Hotel Rooms are Scarce in Arizona
Comment: MEMO 1994/02/16 17:00
Apparently-To: <bhunter@trng.intel.com>

PLEASE READ & FORWARD TO YOUR TRAVELERS. THANK YOU.

Quitting Mail

At the & prompt type q, and mail is terminated.

&
At EOF
& q
Saved 1 message in mbox
Held 4 messages in /usr/spool/mail/bhunter
58%

Bruce Hunter 2/21/01 page 309


UNIX Part I
mail

If you quit before reading all your mail, the last message will be saved in
your $HOME/mbox. It is a good idea to clean it out from time to time.

58% cd
59% ls -l mbox
-rw------- 1 bhunter system 24158 Feb 17 08:48 mbox
62% cat /dev/null>mbox
63%

It’s even easier in sh.

336% ls -l mbox
-rw------- 1 bhunter system 6357 Dec 9 07:37 mbox
337% sh
$ >mbox
$ ls -l mbox
-rw------- 1 bhunter system 0 Dec 22 08:25 mbox
$

Bruce Hunter 2/21/01 page 310


UNIX Part I
mail
Message Status

The mail command will display messages with a status for each message.
The status can be any of the following:

> The current message.

M A message that will be stored in your personal mailbox.

N A new message.

P A message to be preserved in your system mailbox.

R Indicates that you have read the message.

U An unread message. An unread message is a message that was


listed in the mailbox last time you invoked mail, but you did not read.

22% mail
Mail [5.2 UCB] [IBM AIX 3.2] Type ? for help.
"/usr/spool/mail/bhunter": 4 messages 4 unread
>U 1 intel.ENTERPRI@fm1n.intel.com Wed Feb 16 15:45 268/12302
"WW08 ENCO Change Summary Pt2"
U 2 intel.ENTERPRI@fm1n.intel.com Wed Feb 16 15:47 323/15037
"WW08 ENCO Change Summary (TCR"
U 3 intel.BADMINIS@fm1n.intel.com Wed Feb 16 16:51 61/2098
"FOLSOM (FWR) FACILITIES WORK "
U 4 intel.BADMINIS@fm1n.intel.com Wed Feb 16 16:52 123/3297 "CA
Materials Technology Analy"
&

Bruce Hunter 2/21/01 page 311


UNIX Part I
mail
mail Subcommands

= Echoes the number of the current message.

# Comment character for writing comments in mail script files.

- Displays the previous message.

? Displays a brief summary of mailbox subcommands. It is the same as


the help subcommand.

!command
Executes the workstation shell command specified by command.

d Deletes the current message

dp Deletes the current message and displays the next message.


If there is no next message, mail displays EOF.

e [messagelist] Starts the alternate editor using the messagelist


as input files. To define you alternate editor, use set EDITOR=
or place an entry in your $HOME/.mailrc file.

x Leaves mail and returns to the operating system without


changing the original contents of your mailbox. The mailbox returns to
the condition that it was when mail was started. Messages marked
to be deleted are not deleted.

Some special conventions are recognized for the Name:

# Refers to the previous file.

% Refers to the system mailbox (/var/spool/mail/logname).

& Refers to your personal mailbox ($HOME/mbox).

+Name
Refers to a file in your folder directory.

Bruce Hunter 2/21/01 page 312


UNIX Part I
mail

folders
Lists the names of the folders in your folder directory.

f [messagelist] displays the headings of messages in


messagelist.

h lists the headings in the current group of messages (each group


of messages contains 20 messages by default.
Change this with the set screen= statement).

help displays a brief summary of mailbox subcommands. It is identical


to the ? subcommand.

hold [messagelist] Marks each message in messagelist to be


saved in your system mailbox (/var/spool/mail/logname)
instead of in your $HOME/mbox file. It does not override the
delete subcommand.

l Displays a list of mail subcommands with no explanation of


what they do.

mbox [messagelist] Indicates that the messages in messagelist are


to be sent to your personal mailbox ($HOME/mbox) when you quit
the mail. This operation is the default action for messages that you
have read if you are looking at your system mailbox
(/var/spool/mail/logname and the hold option is not set.

m [messagelist] Displays the messages in messagelist using


a defined pager program like more or pg to control display to the
screen.

new [messagelist] Marks each message in messagelist as not having


been read.

Bruce Hunter 2/21/01 page 313


UNIX Part I
mail
n Makes the next message in the mailbox the
current message and displays that message. With an argument
list, it displays the next matching message.

pa Displays the messages in messagelist using


the defined pager (more, pg) program to control display to the screen.

p Dsplays the text of a specific message.

q Leaves mail and returns to the operating system. All messages


read, but, not deleted or saved, are stored in your personal mailbox
($HOME/mbox). All messages you have marked to be deleted are
removed from the mailbox and cannot be recovered. All messages
marked with the hold or preserve option and messages you have
not viewed are saved in the system mailbox
/var/spool/mail/logname.

reply, respond [message]


Allows you to reply to the sender of a message and to all others who
receive copies of a message.

s file Saves a messagelist including heading


information to a file or folder.

size Displays the sizes in lines/characters of the


messages in messagelist.

top [messagelist]
Displays the top few lines of the messages specified by messagelist.

u Removes the messages in messagelist from


the list of messages to be deleted when you quit mail.

unread Marks each message in messagelist as not having been read.

Bruce Hunter 2/21/01 page 314


UNIX Part I
mail
The ~ Escape

The mail program treats lines beginning with the ~ (tilde) character as
subcommands. The following describes the subcommands used while
in the mail editor. The editor recognizes subcommands only if you enter
them at the beginning of a new line.

˜? Displays a summary of the mail subcommands.

˜! command
Executes the shell command and returns to the message.

˜c addresslist
Adds names in addresslist to the list of people to receive
copies of the message. The ˜c subcommand can only be used to add
to, not change or delete, the contents of the Cc: list.

˜d Appends the contents of the dead.letter file to the end of the


message.

˜e Starts the alternate editor (like vi) using the message text as the
input file. This editor can be defined with the set EDITOR= statement.
When you exit the editor, you return to the mail editor, where you may
add text, or send the message by exiting mail.

˜f [messagelist]
Includes a messagelist in the current message to
forward the message to another user. This subcommand reads each
message in the messagelist and appends it to the current end of the
message, but does not indent the appended message.

˜h Enables you to add or change information in all of the heading fields.


The system displays each of the four heading fields, one at a time.
You can view the contents of each field and delete or add information
to that field. Press Enter to save any changes to the field and to
display the next field and its contents.

Bruce Hunter 2/21/01 page 315


UNIX Part I
mail
˜m [messagelist]
Includes a messagelist in the current message.

˜q Quits the editor without sending the message. Saves the mesage in
the dead.letter file in your home directory, unless the nosave
option is set.

˜r file
Reads the contents of a file into the current message.

˜s string
Changes the subject field to the phrase specified in string.

˜t addresslist
Adds the addresses in addresslist to the To: field of the message.

˜v Starts the vi editor using the message text as the input file. This editor
can be defined with the set VISUAL= statement. When you exit that
editor, you return to the mail editor, where you may add text to the
message or send the message by exiting mail.

˜w file
Writes the message to file.

˜: subcommand
Executes subcommand and returns to the mail.

˜˜ Allows you to use the ˜ (tilde) character in a message without it being


interpreted as a command prefix. A ˜˜ key sequence results in only
one ˜ character being included in the message.

Bruce Hunter 2/21/01 page 316


UNIX Part I
mail
Quiz

What are the mail subcommands to

1) Look at a message?

2) Delete a message?

3) Get into vi at mid message?

4) Quit mail as it you were never there?

5) Save a message?

You just spelled and formatted a document. How do you send it to user
cbarret?

Bruce Hunter 2/21/01 page 317


UNIX Part I
mail
Customizing mail
Your .mailrc script customizes your mail environment.

29% cat .mailrc


set append dot autoprint
set folder=/usr/system/bhunter/trng.mail
alias george gsimko@folsm1
alias dave dhudock@folsm1
alias gene gsady@fmwu39
alias chad chad@linus.mitre.ORG
30%

A line that begins with a # (pound sign) followed by a space is treated as a


comment. The following key words are used.

alias newalias { address... |previousalias... }


Defines an alias or distribution list. The alias can be defined as an actual
mail address, or as another alias that is defined in a previous entry in the
.mailrc file. To define a group, enter multiple addresses or previous
aliases separated by spaces.

alias group dave gene george shilo russ fes_group

ignore fieldlist
Adds the header fields in fieldlist to the list of fields to be ignored. Ignored
fields are not displayed when you look at a message with the type or print
subcommand. Use this subcommand to suppress machine-generated
header fields.

set [optionlist | Option=Value...]


Sets an option. The argument following the set option can be either an
optionlist giving the name of a binary option (an option that is either set or
unset) or an option=value entry used to assign a value to an option.

unset optionlist
Disables the values of the options specified in optionlist. This action is the
inverse of the set optionlist entry.

Bruce Hunter 2/21/01 page 318


UNIX Part I
mail
Binary Options For set and unset
append
Adds messages saved in your mailbox to the end rather than to the begin-
ning of the $HOME/mbox file.

ask
Prompts for the subject of each message sent. If you do not wish to create
a subject field, press Enter at the prompt.

askcc
Prompts for the addresses of people who should receive copies of the mes-
sage. If you do not wish to send copies, press Enter at the prompt.

autoprint
Sets the delete subcommand to delete the current message and display
the next message.

debug
Displays debugging information. Messages are not sent while in debug
mode. This is the same as specifying the -d flag on the command line.

dot
Interprets a period entered on a line by itself as the end of a message you
are sending.

hold
Holds messages the you have read but have not deleted or saved in the
system mailbox instead of in your personal mailbox. This option has no
effect on deleted messages.

ignore
Ignores interrupt messages from your terminal and echoes them as @ (at
sign) characters.

ignoreeof
Sets mail to refuse to quit at the ^D key sequence as the end of a mes-
sage.

Bruce Hunter 2/21/01 page 319


UNIX Part I
mail
keepsave
Prevents mail from deleting messages that you have saved with the s or w
subcommand. Normally, messages are deleted automatically when you exit
the mail command. Use keepsave and hold options to hold messages in
your system mailbox. Otherwise, the messages are placed in
$HOME/mbox.

metoo
Includes the sender in the alias expansion. By default, expanding the alias
removes the sender. When this option is set in your .mailrc file, sending
a message using an alias that includes your name sends a copy of the mes-
sage to your mailbox.

noheader
Suppresses the list of messages in your mailbox when you start mail.
Instead, only the mailbox prompt (&) is displayed. To get a list of messages,
use the h subcommand.

nosave
Prevents retention of interrupted letters in t $HOME/dead.letter.

quiet
Suppresses the printing of the banner when mail starts. The banner is the
line that shows the name of the mail program.

verbose
Displays the actual delivery of messages on the terminal.

Bruce Hunter 2/21/01 page 320


UNIX Part I
mail
Value Options for the set Entry

You can use a set entry to assign values to the following options. For
example, enter

set screen=20

to limit headers to 20 lines per screen.

crt=lines
Defines the number of lines of a mail message mail displays before paus-
ing for input (this option starts pg to control the scrolling).

EDITOR=editor
Gives the full path name of the editor to be started with the e mailbox sub-
command or the ˜e mail editor subcommand.

escape=C
Changes the escape character C used for mail editor subcommands. The
default character is ˜ (tilde).

folder=pathname
Gives the path name of a directory in which to store mail folders. Once the
directory is defined, you can use the + (plus sign) notation to refer to it
when using the file name ++ parameter with mailbox subcommands.

record=filename
Defines a file in which to record outgoing mail. The path name must be
given relative to the user’s home directory.

screen=lines
Defines the number of lines of message headers displayed.

toplines=lines
Defines the number of lines displayed by the top subcommand.

VISUAL=editor
Gives the full path name of the editor to be started with the v subcommand
or the ˜v mail editor subcommand. The default editor is vi.

Bruce Hunter 2/21/01 page 321


UNIX Part I
mail

Setting Up Folders

You must set the folders option in $HOME/.mailrc to set up mail fold-
ers.

folder=/usr/system/bhunter/folders

You must have an empy file in your mail folder directory for each folder.

% cd
% mkdir folders
% cd folders
% touch junk meetings schedues project

Bruce Hunter 2/21/01 page 322


UNIX Part I
mail
Quiz

Explain these entries in .mailrc

set folder=’Mail’
set append dot autoprint
alias mark russel@ssw.intel.com
alias mustang-list mustangs@cup.hkp.com

Bruce Hunter 2/21/01 page 323


UNIX Part I
mail
Sendmail

The sendmail program is what makes mail work on a networked system.


It is the routing agent for mail. You can see it running as a daemon on your
system.

24% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 23:34 /etc/init
root 644 095 0 Jan 16 - 0:00 sendmail -bd -q30m

The primary job for sendmail is address translation. Mail yourself a mes-
sage.

mail bhunter

and then look at the header From address

From bhunter@trng.intel.com Thu Feb 17 15:45:07 199

and the Received line

Received: from jaeger ([132.233.71.32])\


by trng.intel.com

or even the Return-Path

Return-Path: <bhunter@trng.intel.com>

You didn’t even type in an address, and now it has a address and it isn’t
even your machine. sendmail has added an address, not any old address
but a fully canonical address in Internet format. The received line shows it
knows that the mail came from jaeger, but the machine of record is trng,
which is also the name of the mail domain. sendmail uses a set of pre-
defined rules for the address translation.

Bruce Hunter 2/21/01 page 324


UNIX Part I
mail
/etc/aliases and Mail Delivery

You send mail to George Canepa as

mail gcanepa

and he gets it. This isn’t unusual until you realize that he has no UNIX
account, he’s on soft switch. How did it work? In the file /etc/aliases or
its YP equivalent the is a list of aliases that mail uses. Here is just a small
price of it.

gcanepa:george_r_canepa@ccm.fm.intel.com
gchin:gchin@fmsu01
glenmcph:glenmcph@fmsu01
gmiller:gmiller@fmsu01
grathbun:grathbun@fmsu01
grc:george_r_canepa@ccm.fm.intel.com
gsady:gsady@fmsu01
gsiemers:gsiemers@fmsu01
gsimko:george_simko@ccm.fm.intel.com

Notice the first intial and last names. They are on soft switch. The aliases
file substitutes the adddress to get it to sendmail. sendmail will do the
rest of the adress translation to soft switch. Here is rule one of sendmail
for the address translation.

S1
#
# rules added for softswitch addressing
#
R$-@$j$* $1@$R.$D make FROM field mail host
R$- $1@$R.$D add domain to single addresses
R$+@$-@$+ $1@$2.$D ssw reply resolution
#

Bruce Hunter 2/21/01 page 325


UNIX Part I
mail
.forward

What if you are temporarily transferred to another site for a few days? How
will you get your mail?

Create a file called .forward in your $HOME. In it put your logname and
new, temporary address.

%cd
% cat .forward
jsmith@ws15.chandler.intel.com

An extemely useful use of .forward is to send you UNIX mail to your Win-
dows account.

bhunter@intel.com

Bruce Hunter 2/21/01 page 326


UNIX Part I
Security

Security

UNIX Part I
section 9
Rev 1/13/99
ECT001118

Bruce Hunter 2/21/01 page 327


UNIX Part I
Security

“There is no security.” Spoken by the character Wolfgar in the film Night


Hawks.

These words are frightening, but with a little work and some knowledge
there is security. In the film, the antagonist, Wolfgar, was eventually killed
and then there was security — for a while. That brings about another point.
Security, like freedom, must be guarded and worked on constantly.

Bruce Hunter 2/21/01 page 328


UNIX Part I
Security
Security

What is security? You leave a dollar and a half in your desk drawer and the
next morning it’s gone. You have been violated. There is a lack of security.
What if you had locked the desk? That’s security.

Would you leave your pay, in cash, laying on your desktop day after day?
Then why do you leave the company’s intellectual and trade secrets open
on your desk day after day? Why do you leave your computer terminal open
for anyone who comes along to access it, day after day?

Computer security is often treated as an afterthought, while in reality it has


the greatest potential for theft or abuse of anything that you are apt to come
across at work on any day.

Bruce Hunter 2/21/01 page 329


UNIX Part I
Security
UNIX Security

UNIX has many ways to protect the user’s work from access, theft, or tam-
pering. UNIX can be configured to an orange book C2 security level. Part of
its security is up to the system administrators, but most of it is up to you.
Areas that can be secured are

Logon access

Password protection

File protection

Directory protection

Network access protection

Keyboard/screen access protection

YP permissions set up in login-dot files (like .rhosts)

Your search path, $path

Bruce Hunter 2/21/01 page 330


UNIX Part I
Security
Personal Security

You can do more to secure the system from access than all the other protec-
tion features. What can you do?

Pick an unguessable password. Not a doggie/kitty name, not an


anagram of your name or any part of your life that is obvious.
It should be character rich (upper/lower/numeric mix with at least
one non character non number) and nothing
that can be looked up in any dictionary, English or foreign.

Memorize your password. Don’t write it down.

Don’t type your password when someone is watching you.


Would you key in your ATM password with someone watching
every keystroke?

Don’t walk away and leave your screen unattended. Lock it.

Protect your file systems and files from casual access with reasonable
permissions.

Change your password often. Don’t reuse old passwords.


Don’t write your password down; memorize it.

Don’t give your password to anyone. If they need access to your


files, give it to them with group permissions.

Bruce Hunter 2/21/01 page 331


UNIX Part I
Security
Logons and Passwords

A password was assigned when your account was created. That password
is your key to the system and should be protected at all times. Your logon
prompt looks like

login:

You type in your log-on name and then you are asked for the password.

password:

Echo is turned off while you type it, so it can’t be seen. If your screen seems
“funny”, key in a dummy name and password. You may be the victim of a
Trojan horse.

If at any time you feel that your password has been compromised, change it.

On EC systems the command to change your password is

chpasswd

Bruce Hunter 2/21/01 page 332


UNIX Part I
Security
Changing Your Password

The first step is to pick a good password, one that is a character mix and
something that even your best friend wouldn’t know. How about that pair of
skis that you sold two years ago? They were Solomon GSs, 205 cm long.

sol*205GS

You won’t forget it, and no one will guess it. Any character is good except
the newline. Even control characters work, and they are hard to see. The
command you need to use for a system that is not on NIS (YP) is passwd.
Type

% passwd

and it will ask you for the new password. Enter it, and you will be asked to
type it again to be sure that you didn’t make a typo.

You need to use the yppasswd command on systems with NIS (YP). It
works the same as /bin/passwd.

Bruce Hunter 2/21/01 page 333


UNIX Part I
Security
Where Is Your Password Stored?

Your password is nowhere in the system. You have an entry in


/etc/passwd or its YP equivalent. To see your password entry

% grep yourname /etc/passwd

or

86% ypcat passwd |grep bhunter


bhunter:rzojv8/Uf2q92:100:0:Bruce H Hunter:\
/usr/system/bhunter:/bin/csh
87%

The garbage you see in the second field is the result of your password being
encrypted. On newer systems, your password encryption is in a shadow
file.

Bruce Hunter 2/21/01 page 334


UNIX Part I
Security
Quiz

Describe a good password.

Where is your password stored?

Who can see it?

How can you see it?

How do you change it? On YP?

Bruce Hunter 2/21/01 page 335


UNIX Part I
Security
File Permissions

UNIX has a unique file permissions protection system. Do a long listing of


any file system.

94% ls -al
total 4
drwxr-xr-x 2 bhunter system 512 Feb 02 07:35 .
drwxrwxr-x 3 bhunter system 512 Feb 16 10:33 ..
-rw-r--r-- 1 bhunter system 0 Feb 02 07:35 foo
95%

What is that stuff in the first field?

regular file
read
write
execute
no permission

-rwxr-xr-x
drwxr-xr-x

other (everyone)
group
user

directory

Bruce Hunter 2/21/01 page 336


UNIX Part I
Security
What Does rwx Mean?
Permissions are set in binary bits, and expressed in octal.

read write execute


R W X

100 010 001 base 2

4 2 1 octal

Sum = 7 = 0111 2

Now let’s go back.

94% ls -al
total 4
drwxr-xr-x 2 bhunter system 512 Feb 02 07:35 .
drwxrwxr-x 3 bhunter system 512 Feb 16 10:33 ..
-rw-r--r-- 1 bhunter system 0 Feb 02 07:35 foo
95%

The dot directory is readable, writable, and “executable” by bhunter, read-


able and “executable” by the group system and everyone else. Executable
for a file makes sense but for a directory? For a directory, x means it can be
traversed. Turn it off, and no one can get to the directory.

102% cd junk
103% mkdir noenter
104% chmod 666 noenter
105% ls -ld noenter
drw-rw-rw 2 bhunter system 512 Feb 18 09:47 noenter
106% cd noenter
noenter: Permission denied
107%

Bruce Hunter 2/21/01 page 337


UNIX Part I
Security
Mode Bits

Permissions are also called permissions modes or mode bits. Here is what
they mean. The first description is for files, the second for directories.

r read file, read files in the directory

w write file, write or erase a file in the directory

x execute file (script or a.out), traverse the directory

- no permissions, or regular file (position dependent)

s set user-ID bit / set group-ID bit (position dependent)

d a directory

l a symbolic link for file or directory

t sticky bit, set link

Set UID means that a user can execute a command as if it were executed
by the owner.

-r-sr-xr-x 1 root system 46797 Dec 14 1992 /bin/passwd

Here the set UID bit is set so anyone can execute /bin/passwd as if root
executed it. It is needed so the file /etc/passwd can be written to.

The sticky bit enables a link/unlink attribute for a directory. Files can only be
unlinked if the requesting process has write permission for the directory and
is either the owner of the file or the directory.

For files the sticky bit enables the save text attribute for an executable file.
The program is not unmapped after usage. It allows user delete only.

Bruce Hunter 2/21/01 page 338


UNIX Part I
Security
What’s a soft link?

lrwxrwxrwx 1 root system 19 Dec 14 1992 \


/usr/bin/perl -> /usr/local/bin/perl

perl is not in /usr/bin, but there is a symbolic link to it.

Now a hard link and the sticky bit.

bash-2.00$ mkdir sticky


bash-2.00$ ls -ld sticky/
drwxr-x--- 2 bhunter users 512 Jul 14 07:24 sticky/
bash-2.00$ chmod +t sticky
bash-2.00$ ls -ld sticky/
drwxr-x--T 2 bhunter users 512 Jul 14 07:24 sticky/
bash-2.00$ cd sticky/
bash-2.00$ touch foo
bash-2.00$ ln foo bar
bash-2.00$ ls -l
total 0
-rw-r----- 2 bhunter users 0 Jul 14 07:27 bar
-rw-r----- 2 bhunter users 0 Jul 14 07:27 foo
bash-2.00$ pwd
/usr/users/a5fs/bhunter/junk/sticky

The link, bar, can be removed only by the owner or by root even if directory
permissions would otherwise permit it. Links can be made across directories
but not physical partitions.

bash-2.00$ ln sticky/foo snarg


bash-2.00$ ls
snarg sticky
bash-2.00$ ls -l
total 2
-rw-r----- 3 bhunter users 0 Jul 14 07:27 snarg
drwxr-x--T 2 bhunter users 512 Jul 14 07:55 sticky

Bruce Hunter 2/21/01 page 339


UNIX Part I
Security
Exercise

Explain the following permissions.

109% cd cron
110% ls -la
total 32
drwxr-xr-x 4 bin cron 512 Dec 14 1992 .
drwxrwxr-x 15 bin bin 1024 Feb 14 04:06 ..
drwxrwx--- 2 bin cron 512 Dec 14 1992 atjobs
drwxrwx--- 2 bin cron 512 Aug 13 1993
crontabs
111% ls -l /bin/passwd
-r-sr-xr-x 1 root system 46797 Dec 14 1992 /bin/passwd
112% ls -l /etc/passwd
-rw-rw-r-- 1 root system 229 Jan 07 1993 /etc/passwd
113% ls -l /etc/shutdown
lrwxrwxrwx 1 root system 18 Dec 14 1992 \
/etc/shutdown -> /usr/sbin/shutdown
114% ls -l /etc/motd
-r-xr--r-- 1 bin bin 880 Dec 14 1992 /etc/motd

Bruce Hunter 2/21/01 page 340


UNIX Part I
Security
Permissions on Executable Files

Write a script and execute it. Here’s the file.

125% cat hi
:
echo "hello"

126% ls -l hi
-rw-r--r-- 1 bhunter system 16 Feb 18 10:13 hi

Now run it.

127% ./hi
./hi: The file access permissions do not allow the\
specified action..
128% chmod +x hi
129% hi
hello
130% ls -l hi
-rwxr-xr-x 1 bhunter system 16 Feb 18 10:13 hi
131%

What did you learn? You can’t execute a file that doesn’t have execute per-
missions, and you don’t get them automatically.

Bruce Hunter 2/21/01 page 341


UNIX Part I
Security
Changing Permissions

If you own a file or directory, you can change its permissions.

33% whoami
bhunter
34% chmod 000 /etc/motd
chmod: /etc/motd: Operation not permitted.
35% ls -l /etc/motd
-r-xr--r-- 1 bin bin 880 Dec 14 1992 /etc/motd
36%

What happened? This user doesn’t own the file.

128% chmod +x hi

The user changed the permissions by adding the x bits.

130% ls -l hi
-rwxr-xr-x 1 bhunter system 16 Feb 18 10:13 hi
131%

Bruce Hunter 2/21/01 page 342


UNIX Part I
Security
chmod

Syntax

In symbolic mode it’s

chmod [ -R ] [ -f ] [ [ u ] [ g ] [ o ] | [ a ]
] { {- | + [r ] [ w ] [ x ] [ X ] [ s ] [ t ] ] }
| { = [ [ r ] [ w ] [ x ] [ X ] [ s ] [ t ] ] } } {
File ... | Directory ... }

In absolute or numeric mode it’s

chmod [ -R ] [ -f ] PermissionCode { File ... |


Directory
... }

chmod modifies the read, write, and execute permissions of files and modi-
fies the search permission codes of directories. Permissions can be defined
symbolically or numerically. When you use the symbolic mode to specify
permission modes, the first set of parameters selects the permission field,
as follows:

u File owner

g Group and entries pertaining to the file owner’s group

o All others

a All — user, group, and others. This has the same effect as specifying
the ugo flags. The a flag is the default permission field. If the
permission field is omitted, the default is the a flag and the file creation
mask (umask) is applied.

Bruce Hunter 2/21/01 page 343


UNIX Part I
Security
The second set of flags selects whether permissions are to be taken away,
added, or set exactly as specified:

- Removes the specified permissions.

+ Add the specified permissions.

= Clear the selected permission field and sets it to the mode specified.
If you do not specify mode following =, the chmod command removes
all permissions from the selected field.

The third set of parameters chmod selects the permissions as follows:

r Read permission.

w Write permission.

x Execute permission for files.

X Execute/search permission if the file is a directory or if the current


file-mode bits have at least one of the execute bits set. It is ignored
if the file is not a directory and none of the execute bits are set in the
current file mode bits.

s Set User-ID or Set Group-ID permission to that of file. This flag is only
valid with the u and g flags of the permission field.

t The sticky bit, sets link permission for directories, saves text attributes
for files.

Bruce Hunter 2/21/01 page 344


UNIX Part I
Security
133% touch perms
134% chmod a= perms
136% ls -l perms
---------- 1 bhunter system 0 Feb 18 12:00 perms
137% chmod a+r perms
138% ls -l perms
-r--r--r-- 1 bhunter system 0 Feb 18 12:00 perms
139% chmod u+w perms
140% ls -l perms
-rw-r--r-- 1 bhunter system 0 Feb 18 12:00 perms
141% chmod +x perms
142% ls -l perms
-rwxr-xr-x 1 bhunter system 0 Feb 18 12:00 perms
143% chmod o-r perms
144% ls -l perms
-rwxr-x--x 1 bhunter system 0 Feb 18 12:00 perms
145%

Bruce Hunter 2/21/01 page 345


UNIX Part I
Security
chmod Numeric or Absolute Mode

The chmod command also permits you to use octal notation to set each bit
in the permission mode. chmod sets the permissions to the permission code
you provide. This permission code is constructed by combining (the logical
OR) of the following values:

4000 Sets user ID on execution.

2000 Sest group ID on execution.

1000 Sets the link permission to directories or sets the save text
attribute for files.

0400 Permits read by owner.

0200 Permits write by owner.

0100 Permits execute or search by owner.

0040 Permits read by group.

0020 Permits write by group

0010 Permits execute or search by group.

0004 Permits read by others.

0002 Permits write by others.

0001 Permits execute or search by others.

Bruce Hunter 2/21/01 page 346


UNIX Part I
Security

145% chmod 000 perms


146% ls -l perms
---------- 1 bhunter system 0 Feb 18 12:00 perms

147% chmod 755 perms


148% ls -l perms
-rwxr-xr-x 1 bhunter system 0 Feb 18 12:00 perms

149% chmod 644 perms


150% ls -l perms
-rw-r--r-- 1 bhunter system 0 Feb 18 12:00 perms

151% chmod 600 perms


152% ls -l perms
-rw------- 1 bhunter system 0 Feb 18 12:00 perms

Bruce Hunter 2/21/01 page 347


UNIX Part I
Security
Mode Lab

1) Make your own directory. Make it accessable only to your group.

2) Touch a file dummy.

3) Change its mode to -------

4) Change its mode back to -rw-r--r--.

Bruce Hunter 2/21/01 page 348


UNIX Part I
Security
Default Permissions and umask

Compilers set the permissions of their a.out output files to be executable.


With that exception, UNIX normally creates files as -rw-rw-r-- or mode
664. As security tightens, some systems will default file creation to mode
644 or even 600. This is done with a command called umask. It is used in
your .profile, .login, or .chsrc files. The syntax is

umask creationmask

Where the creation mask is a number to be ANDed against the default


mask. Here is what the manual has to say about umask.

"creationmask specifies the value of the file mode creation mask. The cre-
ationmask parameter is constructed by logically ANDing file permission bits
defined in the sys/mode.h file. Nine bits of the creationmask parameter
are significant.”

If the default mask were 000, every file would be -rw-rw-rw. If you set
your mask to 022, every file you create will be -rw-r--r--.

159% cd
160% grep umask .??*
.loginx11:umask 022
161% touch whatmask
162% ls -l whatmask
-rw-r--r-- 1 bhunter system 0 Feb 18 12:26 whatmask

This is the case of ordinary files. Directories are different. They have the x
bits set.

165% cd junk
166% mkdir null
167% ls -ld null
drwxr-xr-x 2 bhunter system 512 Feb 18 12:28 null
168%

Bruce Hunter 2/21/01 page 349


UNIX Part I
Security
Let’s play with umask. The link snarg was made recently. Look at its per-
missions and the umask that existed when it was set.

% ls -l
total 2
-rw-r----- 3 bhunter users 0 Jul 14 07:27 snarg
drwxr-x--T 2 bhunter users 512 Jul 14 07:55 sticky
% umask
027

Now add a umask line to .cshrc.

umask 022
set history = 80
set filec
set notify
set cdpath=( $home )
set prompt="$HOSTNAME""[\!]%"

Be safe and source .cshrc and then make a new file and list it.

fri2006[21]%source .cshrc
fri2006[22]%touch umsk
fri2006[23]%ls -l umsk
-rw-r--r-- 1 bhunter users 0 Jul 14 08:24 umsk
fri2006[24]%umask
22

Bruce Hunter 2/21/01 page 350


UNIX Part I
Security
Terminal Locking

When you leave your system, for any reason, you should log out or lock
their terminal. Some software, like X Windows, has a built-in lock com-
mands. The most frequently used is xlock. The xlock command locks an
X server until the user enters a password at the keyboard. While xlock is
running, all new server connections are refused. The screen saver is dis-
abled, the mouse cursor is turned off, the ability to toggle to another shell is
disabled, the screen is blanked, and a changing pattern is displayed. If a key
or a mouse button is pressed, a prompt asks for the password of the user
who started xlock.

Use the lock command If you are not in X Windows.

Bruce Hunter 2/21/01 page 351


UNIX Part I
Security
Quiz

What is a creation mask?

How does it work?

What is the command name to use it?

Give a command line to make a script usable by everyone but changeable


only by you.

What’s a set-UID bit?

How do you set it?

How do you keep others out of one of your directories?

Bruce Hunter 2/21/01 page 352


UNIX Part I
Security
.rhosts

The file $HOME/.rhosts is used by the BSD R-Protocol commands. This


file defines which remote hosts are permitted to invoke R-Protocol com-
mands, which can execute on the local host without supplying a password.

This file should be hidden in the local user’s home directory and must be
owned by the user. The permissions of the .rhosts file should be set to
mode 600. The format of the $HOME/.rhosts file is:

HostNameField [UserNameField]

When a remote command executes, the local host uses the local system’s
/etc/hosts.equiv and $HOME/.rhosts file to validate the remote host
and remote user.

.rhosts file supports the following host/name entries:

+@domain user_name
+ # A Giant No No
HostName
-HostName
+@NetGroup
-@NetGroup

A + signifies that any host on the network is trusted and that can never be
the case!

The HostName entry is the name of a remote host and signifies that any
user logging in from HostName is trusted. A -HostName entry signifies that
the host is not trusted. +@NetGroup or -@NetGroup entry signifies that all
hosts in the netgroup or no hosts in the netgroup, respectively, are trusted.

The @NetGroup parameter is used by YP (NIS) for grouping.

Bruce Hunter 2/21/01 page 353


UNIX Part I
Security
The Trojan Horse

Here is a Trojan horse script:

201% cat t_horse


#!/bin/sh
clear
echo -n "login: "
read user_name
echo -n "password: "
stty -echo
read user_passwd
stty echo
echo $user_name $user_passwd >/tmp/gotcha
clear
echo "Login attempt failed, try again"

Could it work? First you see

login:

and then

password:

echo turns off. Here is the end.

Login attempt failed, try again

Did it work?

200% cat /tmp/gotcha


dfool takeme
201%

A cracker’s version would rcp the information to another system. Be safe.

If you doubt your login prompt, enter a newline to it and the password: re-
quest.

Bruce Hunter 2/21/01 page 354


UNIX Part I
Security
How else can you protect yourself from the horse? Make your $HOME and
$HOME/bin directories -rwxr-xr-x. If they can’t be written to, they can’t
be added to. Keep all executable unchangeable by others. The permissions

-rwxr-xr-x

for a file and a parent directory permission of

drwxr-xr-x

is minimal to keep someone from altering the contents of an executable and


make it work for them.

Bruce Hunter 2/21/01 page 355


UNIX Part I
Security

Quiz

What are the permissions for $HOME/.rhosts ?

What .rhosts entries are illegal?

How do you avoid the Trojan horse?

Who can change permissions on your files?

Bruce Hunter 2/21/01 page 356


UNIX Part I
Security
Entering Passwords to a Script

The shells allow you to flip flop between full duplex and half duplex with the
stty command changing the echo attribute.

#!/bin/sh
echo "Name: \c"
read name
echo "Password; \c"
stty -echo
read password
stty echo
...

It is difficult to impossible to manipulate this attribute from languages like


PERL but a script can be wrapped with a shell script that turns echo off.

% cat wrapper
#!/bin/sh
stty -echo
./noecho.pl

% cat ./noecho.pl
#!/usr/intel/bin/perl
print "Name: ";
$name = <>;
print "\nHi $name"

% ./wrapper
Name:
Hi Bruce

Bruce Hunter 2/21/01 page 357


UNIX Part I
Security
Now for a reverse approach. PERL will call a shell script that turns off echo.

The PERL script.

#!/usr/intel/bin/perl
print "Name: ";
$name = <>;
chomp $name;
print "Password: ";
$password = ‘getpw‘;
print "\nname: $name password: $password\n"

The shell script getpw.

#!/bin/sh
stty -echo
read password
stty echo
echo $password

The run and its results.

% !$
getpw.pl
Name: Bruce
Password:
name: Bruce password: Sol205*GS

Bruce Hunter 2/21/01 page 358


UNIX Part I
Security

Physical Security

There is a lot more to security than permissions and commands. Lock your
car and leave the keys in the door lock. How secure are you? Leave tapes
or floppy disks around, and how secure are you? The answer is obvious.
Lock up all tapes, diskettes, printouts, and so on. If there is a library where
they belong, be sure that they are there. If a terminal or workstation is in a
locked area, be sure the door is locked and closed.

Bruce Hunter 2/21/01 page 359


UNIX Part I
UNIX Networking

UNIX
Networking

UNIX Part I
section10
Rev 1/13/99
ECT001118

Bruce Hunter 2/21/01 page 360


UNIX Part I
UNIX Networking

"The network is the computer.”


Scott McNeily

Bruce Hunter 2/21/01 page 361


UNIX Part I
UNIX Networking

Networking and Productivity


Networking offers great opportunities for increased productivity.

Examples:

Access to mass-storage devices from file servers

Inter-domain mail, Internet mail and other Internet services.

Accessing a high performance unix work server from your NT sys-


tem

Accessing a high-performance unix work server from a lesser unix


system

Attaching to systems with different architectures to get work done


that cannot be done elsewhere.

Accessing information that is not available from your system or in


your domain.

Bruce Hunter 2/21/01 page 362


UNIX Part I
UNIX Networking

Protocols
You’re going to send a letter. You write it and put it in an envelope.
Then you write the following information on the envelope.

sender

sender address

recipient

recipient address

Then you mail it. At the post office, your letter is sorted and put in a
bag. The bag is containerized with other bags and shipped.

Networking is a similar process. You have a message to send, your


data. The system stuffs it in an envelope, a packet, and writes the
sender and recipient addresses on it. This is the Ethernet data.
Inside the Ethernet envelope is a Internet envelope, and inside of it
is a transport envelope, a packet. Inside of that is your data. It may
be split into many envelopes.

Sender 32c
Sender_Address

Recipient
Recipeint_Address

Bruce Hunter 2/21/01 page 363


UNIX Part I
UNIX Networking

Layers and Protocols

Layer Name Address Data Form


Type

Transport Port Packets or


Segments

Network Internet Datagrams

Data Link Ethernet Frames


or 802.3

These three layers make the Ethernet-TCP/IP protocol stack. They


provide three levels of encapsulation, each operating at a different
level.

Bruce Hunter 2/21/01 page 364


UNIX Part I
UNIX Networking

application
user applications

presentation
applications preparation

session
applications preparation

transport
port-to port transporation

network
internetwork routing

data link
data preparation and network linkage

physical
wire or cable transmission hardware

OSI/ISO Network Layers

Bruce Hunter 2/21/01 page 365


UNIX Part I
UNIX Networking

Each layer has its own name, addres type, and data form.

Layer Name Address Type Data Form

Transport Port Packets or Segments


Network Internet Datagrams
Data Link Ethernet Frames

Each delivers its encapsulated data to its corresponding layer on the


node of the sending or receiving system as if it were directly con-
nected at that layer. This gives rise to the name peer-to-peer proto-
cols.

Bruce Hunter 2/21/01 page 366


UNIX Part I
UNIX Networking

virtual path
applications

4 4

actual path
3 3

2 2

Physical

Electrical Path

Peer-to-Peer Protocol Paths

TCP/IP-Ethernet

Bruce Hunter 2/21/01 page 367


UNIX Part I
UNIX Networking

The Ethernet
The lowest layer is the Ethernet layer. It uses a 48-bit (6-byte)
address. The job of the Ethernet is to get the data out on the wire
and have it received only by the addressee. This is done by each
node reading the destination address and accepting it if the frame is
its own.

CSMA/CD
The Ethernet operates on the principal of CSMA/CD, carrier sense,
multiple access with collision detection. It senses if there is any traf-
fic on the wire (media) which acts as an antenna. If there is not it is
free to send. In the event of a collision, it will back off and resend
later.

All of this takes place in the lowest layer, the physical layer and is
handled, in hardware, by the MAU (media access unit) and the NIC
(network interface card.)

Bruce Hunter 2/21/01 page 368


UNIX Part I
UNIX Networking

Internet/Ethernet Addresses
The internet and associated ethernet addresses are stored in the
system’s arp table.

bash-2.00$ arp -a
a8fs123 (132.233.123.160) at 0:0:3c:4:13:f2
a9fs123 (132.233.123.161) at 0:0:3c:2:48:32
giga2-123 (132.233.123.2) at 0:c0:80:8f:8d:74
sub123-gw1.fm.intel.com (132.233.123.100) at
0:e0:f7:41:9:41
a11fs123 (132.233.123.165) at 0:0:3c:2:77:82
giga1-123 (132.233.123.214) at 0:c0:80:89:18:af
a13fs123 (132.233.123.166) at 0:0:3c:1:f4:b2
a1fs123 (132.233.123.150) at 0:0:3c:1:dd:92
a19fs123 (132.233.123.151) at 0:0:3c:4:40:e1
a14fs123 (132.233.123.167) at 0:0:3c:1:ed:12
flc2002 (132.233.123.23) at 8:0:20:80:18:5e
a3fs123 (132.233.123.152) at 0:0:3c:4:7b:22
a5fs123 (132.233.123.154) at 0:0:3c:2:59:2
a4fs123 (132.233.123.156) at 0:0:3c:2:5a:a2

Bruce Hunter 2/21/01 page 369


UNIX Part I
UNIX Networking

Internet Addressing
The use of Ethernet addresses is the only method to get local deliv-
ery. To get from one network or subnetwork to another requires
internetworking and, therefore, an Internet address.

Data in the Internet moves from net to net. Once on a local network,
it is delivered on the basis of its Ethernet address.

Internet

Ethernet

Data moving in packets across networks is received on the basis of


its Internet address. That address is a unique four-byte number
divided into two parts to form three address classes: A, B, and C.
The two parts are the network and hosts parts. The network part is
unique to one network; the host part unique is only within that net-
work.

Bruce Hunter 2/21/01 page 370


UNIX Part I
UNIX Networking

Ethernet Encapsulation
Ethernet frames are encapsulated to the Ethernet standard. The
frame information holds the sender’s and recipient’s address, the
type, the data, and a cyclic redundancy check. This envelope is put
on by the link layer as the data leaves a node, and it’s stripped off as
it enters a node. The recipient address is read by every node on the
segment, and the entire frame is accepted or rejected on the basis
of the address.

Bruce Hunter 2/21/01 page 371


UNIX Part I
UNIX Networking

Concentrators / Hubs
Using Etehrnet cable as te prmary carrier is not without problems so
in most mid- to large-scale systems it has been replaced with hubs.

Hub

Backbone or Fiberoptic link

Typically the hub is connected with twisted-wire pair to the workst-


stions and fiber-optic cable to another concentrator connected to the
servers.

Bruce Hunter 2/21/01 page 372


UNIX Part I
UNIX Networking

Quiz

How may protocol layers (minimum) on any sent message or file?

How are messages accepted by a host from the wire?

Bruce Hunter 2/21/01 page 373


UNIX Part I
UNIX Networking

A Simple Network

server

workstation

Every system on the network is called a node. The Ethernet cable acts as an
antenna sending to all nodes.

Bruce Hunter 2/21/01 page 374


UNIX Part I
UNIX Networking

backbone

s1 s2
R

sn1

sn2

Two subnets with multi-homed servers

The servers are connected to the subnet segments without the need of the
router. The router connects the segments and is capable of joining a half
dozen subnetworks and connecting to the backbone as well.

Bruce Hunter 2/21/01 page 375


UNIX Part I
UNIX Networking

Quiz

Name the components of a contemporary network.

svr

Bruce Hunter 2/21/01 page 376


UNIX Part I
UNIX Networking

The Berkeley “R” Protocols

UCB created the first UNIX Ethernet-TCP/IP networking. Three com-


mands are commonly associated with UCB’s networking achieve-
ments. They are referred to as the ’R protocols.’

rcp Copies files from one networked UNIX system to


another.

rlogin Logs in to another UNIX system.

rsh Executes a shell-level command on another system.

With all three commands, you must have a login on the other
machine. With NIS (YP) running within a domain, and your file sys-
tems mounted to all systems by NFS, you not only will have an
account on all systems, but also your environment will appear to be
the same on all systems.

Bruce Hunter 2/21/01 page 377


UNIX Part I
UNIX Networking

rcp
rcp is remote copy. It copies

remote to remote

local to local

local to remote

remote to local

The syntax is

rcp from to

From or to can have the form

file_name

or

machine_name:file_name

as in

% rcp ww4 stalker:/usr/system/jill/ww4

Bruce Hunter 2/21/01 page 378


UNIX Part I
UNIX Networking

Full rcp syntax


rcp [ -p] { { User@Host:File | Host:File | File }
User@Host:File | Host:File | File | User@Host:Directory |
Host:Directory | Directory } | [ -r] { User@Host:Directory
Host:Directory |Directory } { User@Host:Directory |
Host:Directory | Directory }

rcp is used to copy one or more files between the local host and a
remote host, or between two remote hosts, or between files at the
same remote host.

If the path for a file or directory on a remote host is unspecified or is


not fully qualified, the path is interpreted as beginning at the home
directory for the remote user account.

The remote host allows access if:

1) The local host is included in the remote host’s


/etc/hosts.equiv file and the remote user is not the
root user.

2) the local host and user name is included in a


$HOME/.rhosts file on the remote user account.

Bruce Hunter 2/21/01 page 379


UNIX Part I
UNIX Networking

.rhosts
.rhosts is in every user’s home directory. Its permissions must be
0600.

13% ls -l .rhosts
-rw------- 1 bhunter uucp 14Mar 221993 .rhosts

The contents of .rhosts is the name and system of any user


allowed to use the R commands,

14% cat .rhosts


+@mcd bhunter
15%

This entry allows this user access from any mcd system.

Bruce Hunter 2/21/01 page 380


UNIX Part I
UNIX Networking

rlogin
rlogin connects your system with any remote system that you
have permission to reach.

The command’s syntax is

rlogin remotehost [-e C] [-8] [-l User]

A few flags:

-e C changes the escape character to C.

-l user changes the remote user name to the one you specify;
otherwise, your local user name is used at the remote host.

Bruce Hunter 2/21/01 page 381


UNIX Part I
UNIX Networking

.rlogin Security
The remote host allows access if one or both of these conditions are
met:

1) The local host is included in the remote /etc/hosts.equiv


file, the local user is not the root user, and the -l user flag is
not specified.

2) The local host and user name is included in the


$HOME/.rhosts file in the remote user account.

Example: rlogin stalker

Bruce Hunter 2/21/01 page 382


UNIX Part I
UNIX Networking

rsh

rsh is used to remotley execute a command on another system.

rsh syntax
rsh [-l username] [-n] hostname [command]
rsh hostname [-l username] [-n] [command]
hostname [-lusername] [-n] [command]

rsh connects you to the hostname and executes the command.


rsh copies its standard input to the remote command, the standard
output of the remote command to its standard output, and the stan-
dard error of the remote command to its standard error. Interrupt,
quit and terminate signals are propagated to the remote command.
rsh normally terminates when the remote command does.

Options or Flags

-l username
Use username as the remote username instead of your
local username. In the absence of this option, the
remote username is the same as your local username.

-n redirect the input of rsh to /dev/null.

Bruce Hunter 2/21/01 page 383


UNIX Part I
UNIX Networking

Example of rsh
38% rsh jeager who
bhunter hft/0 Jan 21 07:32
bhunter pts/1 Jan 21 07:32
bhunter pts/0 Jan 21 07:32
39%

Note the complaint, in spite of the fact that the system did the rsh
anyway.

Bruce Hunter 2/21/01 page 384


UNIX Part I
UNIX Networking

The Heterogeneous Networking Commands

The R commands work on UNIX only. Therefore a pair of more uni-


versal commands are needed. These are

telnet

and

ftp

telnet does a login to another host while ftp (file transfer proto-
col) transfers files between hosts.

Bruce Hunter 2/21/01 page 385


UNIX Part I
UNIX Networking

telnet
telnet will log you on to any system that it can reach, providing
that you have an account there.

39% telnet fmsu04


Trying...
Connected to mcd.
Escape character is ’^]’.

SunOS UNIX (mcd)


login:msnerd
Password:

...

13% logout
[1] 14813
Connection closed.

Bruce Hunter 2/21/01 page 386


UNIX Part I
UNIX Networking

telnet Syntax
telnet [ -d] [ -n tracefile]\
[ -e terminal_type][host [port] ]

telnet Flags
-e terminal_type
overrides terminal-type negotiation.

-n tracefile
records network trace information in the file specified
by the tracefile variable.

A few of the telnet subcommands are:

? [subcommand] requests help on telnet subcommands.

close closes the TELNET connection and returns to telnet


command mode when the open subcommand is used to establish
the connection.

display [argument]displays all of the set and toggle values


if no argument variable is specified; otherwise, it lists only those
values that match the argument variable.

Bruce Hunter 2/21/01 page 387


UNIX Part I
UNIX Networking

open host [port] opens a connection to the specified host.


The host specification can be either a host name or an Internet
address in dotted-decimal form. If no port variable is specified,
the telnet subcommand attempts to contact a TELNET server
at the default port.

quit closes a TELNET connection and exits the telnet


program. A ^D in command mode also closes the connection
and exits.

Bruce Hunter 2/21/01 page 388


UNIX Part I
UNIX Networking

ftp
The ftp command puts the user in a virtual login on a remote
machine, where the user has a set of limited commands that allow
file manipulation and transfer.

ftp example
43% ftp fmsu04
Connected to mcd.
220 mcd FTP server (SunOS 4.1) ready.
331 Password required for bhunter.
Password:
230 User bhunter logged in.
ftp> get /etc/motd
/etc/motd: Operation not permitted.
ftp> get /usr/system/bhunter/foo
200 PORT command successful.
150 ASCII data connection for /usr/system/bhunter/
foo (132.233.71.32,1053) (82 bytes).
226 ASCII Transfer complete.
92 bytes received in 0.12 seconds (0.7123 Kbytes/s)
ftp> quit
221 Goodbye.
44%

Bruce Hunter 2/21/01 page 389


UNIX Part I
UNIX Networking

ftp syntax
ftp [-d] [-g] [-i] [-n] [-v] [HostName]

The /usr/bin/ftp command is the interface to the File Transfer


Protocol (FTP). This command uses FTP to transfer files between
the local host and a remote host or between two remote hosts.

The FTP protocol allows data transfer between hosts that use dis-
similar file systems.

-g Disables the expansion of metacharacters in file names.


Interpreting metacharacters can be referred to as expanding
(globbing) a file name.

-i Turns off interactive prompting during multiple file transfers.

Bruce Hunter 2/21/01 page 390


UNIX Part I
UNIX Networking

ftp Subcommands
If you execute the ftp command and do not specify the host name
of a remote host, the ftp command immediately displays the

ftp>

prompt and waits for an ftp subcommand. The ftp command inter-
preter, which handles all subcommands. They are entered at the
ftp> prompt To end an ftp session when you are running interac-
tively, use the quit or bye subcommand or ctrl-D key sequence.

![command [parameters]
This invokes an interactive shell on the local host.

?[subcommand]
This displays a help message describing the subcommand.
If you do not specify a subcommand parameter, the ftp command
displays a list of known subcommands.

append localfile [remotefile]


This appends localfile to a file on the remote host.

cd remotedirectory
Thischanges the working directory on the remote host to th
remotedirectory.

close
Ends the file-transfer session, but does not exit the ftp command.

cr strips the carriage return character from a


carriage-return/line-feed sequence when receiving records
during ASCII-type file transfers.

Bruce Hunter 2/21/01 page 391


UNIX Part I
UNIX Networking

dir [remotedirectory][localfile]
Writes a listing of the content of the specified remote directory
to the specified local file.

get remotefile [localfile]


copies the remote file to the local host.

glob toggles file-name expansion (globbing) for the mdelete,


mget, and mput subcommands. If globbing is disabled, file-name
parameters for these subcommands are not expanded.

help [subcommand] Displays help information.

lcd [directory] Changes the working directory on the local


host.

ls [remotedirectory] [localfile] writes an abbreviated


file listing of a remote directory to a local file.

mdelete remotefiles expands the files specified by the


remote files parameter at the remote host and deletes the remote
files.

mdir [remotedirectories localfile] expands the


directories specified by the remotedirectories parameter at the
remote host and writes a listing of the contents of those
directories to the file specified in the localfile parameter.

mkdir [remotedirectory] Creates the directory specified in the


remotedirectory parameter on the remote host.

mput [localfiles] expands the files specified in the localfiles


parameter at the local host and copies the indicated local files to
the remote host.

Bruce Hunter 2/21/01 page 392


UNIX Part I
UNIX Networking

Working There, Seeing It Here


Sometimes it is necessary to do your work on a system other then
the one on your desk. A few reasons would be the need for:

another OS (e.g. NT to UNIX)

another architecture

another version or revision level of the OS

another version or revision level of software

more memory

a faster processor or processors

To get a working session going:

rlogin or telnet to the other system

In the window for that system set the DISPLAY variable to your
system

On the console of your system run xhost with the + option

On the remote host

21% setenv DISPLAY frx056:0.0

On your system

bash-2.00$ xhost +
1346-207 access control disabled, clients
can connect from any host
bash-2.00$ hostname
frx056

Bruce Hunter 2/21/01 page 393


UNIX Part I
UNIX Networking

NFS
NFS or Network File Systems allows a host to get files from another
host, usually a file server, as if the files and file systems were
mounted on the local host. NFS is transparent. Users do not have to
know that it is there as long as it works well, which it almost always
does.

Do a df from your workstation to see it at work:

44% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 944 76% 857 83% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1720 86% 335 8% /var
/dev/hd3 8192 7880 3% 28 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 150688 81% - - /misc
fmsu05:/usr/fmo/ssd 944238 90594 90% - - /usr/fmo/ssd
fmsu01:/usr/spool/mail 408391 292557 28% - /usr/spool/mail
fmsu01:/amoeba 408399 262086 35% - - /mproj/amoeba
fmsu01:/sam16u3 1893612 252410 86% - - /uproj/sam163
fmsu05:/samgr 917358 63802 93% - - /gproj/sam16
fmsu01:/g16v1 1836252 754996 58% - - /vproj/sam161
fmsu05:/g16v 1278413 74744 94% - - /vproj/sam16
fmsu01:/sam16u2 1835996 412575 77% - - /uproj/sam162
fmsu01:/sam16u1 917870 206056 77% - - /uproj/sam161
fmsu05:/sam16u 1968100 134997 93% - - /uproj/sam16
fmsu05:/sam16 1278413 177015 86% - - /mproj/sam16
fmsu01:/sam16m1 917870 49078 94% - - /mproj/sam161
fmsu05:/g16 1278413 271993 78% - - /gproj/sam161
fmsu05:/samgr2 1298653 499607 61% - - /gproj/sam162
fmsu05:/e2 944238 739466 21% - - /e2
fmsu01:/28u008s 508958 93717 81% - - /gproj/28u008s
fmsu05:/usr/mfg 638430 37771 94% - - /usr/mfg
fmsu05:/usr/local.gen/ibm 472111 57203 87% /usr/local
fmsu05:/usr/system 1836252 337304 81% - - /usr/system
fmsu05:/tapeout2 1240333 264423 78% - - /usr/tapeout2
fmsu01:/tapeout3 918382 172715 81% - - /usr/tapeout

Note that most of these systems are mounted from a file server.

Bruce Hunter 2/21/01 page 394


UNIX Part I
UNIX Networking

They will appear local in spite of their being remotely mounted . The
mechanism are daemons at either end. Here is part of the output of
ps.

45% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 6:50 /etc/init
.
root 6206 3095 0 Jan 16 - 0:13 /usr/etc/ypbind
root 6465 3095 0 Jan 16 - 0:02 /usr/etc/biod 6
root 6724 6465 0 Jan 16 - 0:02 /usr/etc/biod 6
root 6981 6465 0 Jan 16 - 0:01 /usr/etc/biod 6
root 7238 6465 0 Jan 16 - 0:01 /usr/etc/biod 6
root 7495 6465 0 Jan 16 - 0:02 /usr/etc/biod 6
root 7752 6465 0 Jan 16 - 0:02 /usr/etc/biod 6
root 8010 3095 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8267 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8524 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8781 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9038 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9295 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9552 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9809 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 10067 3095 0 Jan 16 - 0:00 /usr/etc/rpc.mountd
root 10326 3095 0 Jan 16 - 0:00 /usr/etc/rpc.statd
root 10584 3095 0 Jan 16 - 0:00 /usr/etc/rpc.lockd
.

The nfsds are NFS daemons, and the biods are I/O daemons for
NFS to provide buffering.

Remember how the servers were mounted to the workstations?

Bruce Hunter 2/21/01 page 395


UNIX Part I
UNIX Networking

The workstations attached to the file servers make a remote proce-


dure call, RPC, to the server to make it execute the usual stat,
read, write, close calls that would have been made on the local
machine if the files were mounted locally. They know of the files by
way of a file handle passed from the server to the workstation when
the file was opened.

NFS Problems
The biggest problem that you will ever have with NFS is the network
or the server being down. You will get a stale file handle error mes-
sage and perhaps some other console error like YP server not
found. Your SA probably knows already, but be sure.

AFS
AFS is vaguely similar to NFS and is used widely here at Intel. While
it is more complex in its mounting it is low in its profile. Watch these
cd and pwd results.

% cd /fes/bhunter
% pwd
/fes/bhunter
% cd ~
% pwd
/usr/users/a5fs/bhunter

/fes is not a AFS mount while the home directory is.

Bruce Hunter 2/21/01 page 396


UNIX Part I
UNIX Networking

NIS (YP)
YP provides a service to all systems that subscribe to it. It gives
tham maps of everything that YP is set up for. Typically YP provides
maps for

passwd

hosts

group

services

netmasks

mail

rpc

ypservers

To see a YP map use the command ypcat.

Each system binds to a YP server at boot time. To see your servers


name

46% ypwhich
fmsu05

A good example of a necessary service from YP is the password file


passwd. If you were on a system that did not use YP, you would use
the passwd command, but on a YP system you use yppasswd.

Bruce Hunter 2/21/01 page 397


UNIX Part I
UNIX Networking

YP Commands
ypcat see a YP map

ypwhich show the YP master

yppasswd change your password on the YP master

Problems with YP
There are multiple YP servers, so if one goes down, another will
take over. Although it is unlikely you will lose all YP servers without
losing the net (or at least the file servers), if you do you, will get this
message

YP server not responding

Bruce Hunter 2/21/01 page 398


UNIX Part I
UNIX Networking

Sendmail

/usr/mail is local only unless sendmail is active. What you see


as your mail program is only a user agent. To move mail, your sys-
tem also needs a delivery agent and, last but not least, a routing
agent to act between the other two agents. This agent is sendmail.

/ucb/mail bin/mail mh elm USER AGENTS

sendmail ROUTING AGENT

local (/bin/mail) TCP/IP UUCP DELIVERY AGENTS

Bruce Hunter 2/21/01 page 399


UNIX Part I
UNIX Networking

Do a ps to see sendmail at work.

47% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 6:52 /etc/init
root 1721 3095 0 Jan 16 - 0:00 /etc/writesrv
root 2032 1 0 Jan 16 - 2:11 /etc/syncd 60
root 2740 1 0 Jan 16 - 2:38 /etc/cron
root 3095 1 0 Jan 16 - 0:00 /etc/srcmstr
root 3570 1 0 Jan 16 - 0:00 /usr/lib/errdemon
root 3785 3095 0 Jan 24 - 0:00 /etc/syslogd
root 4644 3095 0 Jan 16 -0:00 /usr/lib/sendmail -bd -q30m
root 5050 1 0 Jan 16 - 0:00 /etc/uprintfd
root 5163 3095 0 Jan 16 - 1:13 /usr/etc/portmap
root 5422 3095 0 Jan 16 - 0:00 /etc/inetd
root 5683 3095 0 Jan 16 - 0:40 /usr/sbin/snmpd

Bruce Hunter 2/21/01 page 400


UNIX Part I
UNIX Networking

DNS
Domain Name Service resolves host names to Internet addresses.
Locally host names are resolved from the file /etc/hosts or YP's
hosts maps. An organization with 25,000 nodes would need a
25,000 entry-hosts, file and keeping it up to date is impossible.
DNS solves the problem by having DNS servers inside and outside
the local domain (or zone as it is called in DNS) service a request for
name resolution to a local resolver.

Name: fmec.intel.com . (dot)

mil
org
com

cisco
ora

intel internet aol

intranet

epg flash
it

ccad
fmec

Note: DNS is the backbone of Windows 2000 networking

Bruce Hunter 2/21/01 page 401


UNIX Part I
UNIX Networking

Quiz

Describe, briefly, the functions of

NIS

YP

DNS

NFS

What is

a RPC,

a file handle?

ypcat is used to get what?

Bruce Hunter 2/21/01 page 402


UNIX Part I
UNIX Networking

Samba and Exceed


Intel’s engineering computing strategy at the user level is to use
strategic infrastructure release systems (SIR boxes) as the base
platform. These systems run on high-end Intel processors, have
large memory and local disks, run NT as the local OS and access
UNIX by way of Samba and Exceed. Running UNIX from these sys-
tems is seamless and almost indistinguishable from running a free-
standing UNIX system.

Bruce Hunter 2/21/01 page 403


UNIX Part I
Using the X-Windows System

X Windows

ECT001118
Revised 7/13/99
UNIX Part I
section 11

2/21/01 page 404 B Hunter


UNIX Part I
Using the X-Windows System

X and Productivity
The X Windows system is a major productivity tool. It lets you do more by:

Giving multiple windows for multiple jobs

Gives access to systems with the power, space, speed, or architecture you
need to get your jobs done quickly

Giving you a screen that is comfortable for size, location, color, and font to
minimize fatigue.

2/21/01 page 405 B Hunter


UNIX Part I
Using the X-Windows System

Names and Actions

title bar
maximise
menu
iconify

scroll
drag

2/21/01 page 406 B Hunter


UNIX Part I
Using the X-Windows System

History
The X Windows System was started at Stanford and was known as W. Its
technology was taken by Project Athena at the University of Massachusetts
and renamed X.

Starting X
To bring up X from a UNIX login session, type in

% runx

or

% startx

or whatever your sits or local administrator has called the start-up script.

You will see a blue-grey background when X starts. This is your root win-
dow.

After a few moments, you will have a console window and perhaps even a
working window.

2/21/01 page 407 B Hunter


UNIX Part I
Using the X-Windows System

The Console Window


The console window is a virtual window for your X session. If you kill this
window, you will kill X. Do not use the console window for a work window.
You may need it to manage other windows, to lock the screen, or to track
processes.

console
xlock
46% !x
xlock
47% !x
xlock
48% !x
xlock
49%

Icons
Icons show a window that has been "put away for a while" and is repre-
sented with an icon. Double click on the icon to bring the window back.

xterm

2/21/01 page 408 B Hunter


UNIX Part I
Using the X-Windows System
Work windows have a border which is used to raise and resize the window.
At the top is a title bar used to move the window. At the lower left in Motif, or
right on others, is a slider used to move the text through the window.

There are three buttons at the top. The left-most button is used to pull down
the window menu. The right-most button is used to maximize the window.
The one next to it is used to iconify the window.

If you have an icon sitting in the root window, you can activate it by double
clicking on it.

Windows are resized by moving the pointer to the border and clicking on it,
then moving the border. Clicking on an object is called selecting.

console

2/21/01 page 409 B Hunter


UNIX Part I
Using the X-Windows System
The mouse can be used to grab text by selecting the start of the text and
dragging over the text until is all selected.

To copy the text to another place, click middle when the cursor is where you
want the text. If you are moving it to an editor, be sure the editor is in append
or insert mode.

Click middle on your mouse to dump text. In NT this is a copy (^C) and
paste (^V) .

The Root Window Menu


You can get a root menu by selecting (click left) in the root menu. Typically it
is used to get another window.

2/21/01 page 410 B Hunter


UNIX Part I
Using the X-Windows System

Getting and Setting Up X Windows In AIX


To invoke X just type x11. Some systems have a more complicated start up.
Once in X Windows, you will see that your path is long and has several X
directories:

. /usr/local/frame/3.0x/bin /usr/ucb /usr/bin /bin /usr/local/bin


. /usr/bin/X11 /usr/local/X11/bin /usr/lpp/X11/Xamples/bin

This is not the path you have set up in your home dot files. Those paths
looked like:

39% grep path ~/.login ~/.cshrc


/usr/system/bhunter/.login:set path = ( /usr/ucb /usr/
bin /bin /usr/local/bin .)

/usr/system/bhunter/.cshrc:set path = ( . $FMHOME/bin


$path )
40%

Now let’s fall back and see how it all happened. The key path directory is
/usr/local/bin This is where you picked up the x11 command.

41% ls -l /usr/local/bin/x11
Frwxrwxr-x 1 102 staff 4238 Jul 06 /usr/local/bin/x11
42%

2/21/01 page 411 B Hunter


UNIX Part I
Using the X-Windows System
What does this file do? Here are the “punch” lines:

#! /bin/csh -f
set XDIR=/usr/bin/X11# op-systems’s X directory
set LXDIR=/usr/local/X11/bin# local X directory
setenv DISPLAY “unix:0”
echo “DISPLAY = $DISPLAY”
xrdb -load <<XRDB
Mwm*configFile: ~/.mwmrc.aix
XRDB
$XINITRC >& /tmp/$USER.x11
reset -I

Now here are the main things this script does:

• Adds appropriate directories to your $path variable.

• Sets the X windows display variable to the local system.

• Loads the X Windows data base.

• Runs .xinitrc.

• Kills the X server when $XINITRC finishes.

• Resets the terminal after the X server is killed.

Most of the action occurs when this script invokes your local xinitrc file

2/21/01 page 412 B Hunter


UNIX Part I
Using the X-Windows System
~/.xinitrc.aix. Here is the basic parts of the script:

#!/bin/csh
mwm &
sleep 1
if (-e $HOME/.bitmap) xsetroot -bitmap $HOME/.bitmap -fg green -
bg SteelBlue
aixterm -g =80x37+0+163 -fn Rom14 -fg yellow -bg blue -i &
aixterm -g =80x69+200+0 -fn Rom10 -fg yellow -bg NavyBlue -i &
aixterm -g =80x25+0+163 -fn Rom10 -fg green -bg black &
aixterm -g =80x25+0+560 -fn Rom10 -fg green -bg black &
aixterm -g =132x25+0-100 -fn Rom10 -fg white -bg NavyBlue -i &
aixterm -g =80x25 -fn Rom12 -fg white -bg blue -i &
xbiff &
aixterm -name console -e ~mcdcad/bin2/console.aix

The key lines here are the line that runs mwm in the background and the
aixterm lines that set up your intial windows. mwm is your window man-
ager. It in turn invokes ~/.mwmrc.aix which creates your windows, sets
their characteristics and looks like:

Menu RootMenu

2/21/01 page 413 B Hunter


UNIX Part I
Using the X-Windows System
{
“Root Menu” f.title
no-label f.separator
“Windows” f.menu WindowsMenu
no-label f.separator
“ELM” f.exec “cd ~/Mail; aixterm -e elm”
no-label f.separator
“Frame3.0x” f.exec “setenv FMHOME /usr/local/frame/3.0x;
$FMHOME/bin/maker”
no-label f.separator
“xterm” f.exec “xterm -T xterm &”
“fmwu99” f.exec “xterm -title fmwu99 -e rlogin fmwu99 &”
“FOLSM3” f.exec “xterm -title FOLSM3 -e rlogin folsm3 &”
no-label f.separator
“GENERIC” f.exec “xterm -bg orchid -fg white -title GENERIC -fn
6x10 -e rlogin ‘hostname‘ -l generic &”
no-label f.separator
“Refresh” f.refresh
no-label f.separator
“Restart...” f.restart
no-label f.separator
“QUIT...” f.quit_mwm
}
Menu WindowsMenu
{
“BIG” f.exec “aixterm -g =80x25 -fn Rom22 -fg white -bg blue &”
“TALL” f.exec “aixterm -g =80x69+200+0 -fn Rom10 -fg yellow -bg
NavyBlue&”
“BLUE” f.exec “aixterm -g =80x37+0+163 -fn Rom14 -fg yellow -bg
blue &”
“R1” f.exec “aixterm -g -0+162 -fn 6x10 -fg white &”
“R2” f.exec “aixterm -g -0+460 -fn 6x10 -fg white &”
“R3” f.exec “aixterm -g -0+758 -fn 6x10 -fg white &”
“SMALL” f.exec “aixterm -fn 6x10 -fg black -bg white &”
“WIDE” f.exec “aixterm -g =132x25+0-100 -fn Rom10 -fg white -bg
NavyBlue&”
“UPPER” f.exec “aixterm -g =80x25+0+163 -fn Rom10 -fg black -bg
green &”
“LOWER” f.exec “aixterm -g =80x25+0+560 -fn Rom10 -fg black -bg
green &”
“CONSOLE” f.exec “aixterm -g =80x8+0+0 -fn 6x10 -fg NavyBlue -bg
white &”
}
Menu DefaultWindowMenu
{

2/21/01 page 414 B Hunter


UNIX Part I
Using the X-Windows System
“Iconify” f.minimize
“Lower” <Key>F2 f.lower
“Raise” <Key>F3 f.raise
“Move” f.move
“Normalize” f.normalize
“Maximize” f.maximize
no-label f.separator
“CycleUp” f.circle_up
“CycleDown” f.circle_down
“EXIT” Alt<Key>F4 f.kill
}
Keys DefaultKeyBindings
{
<Key>F4 icon f.normalize
<Key>F4 window f.minimize
Shift<Key>Escape icon|window f.post_wmenu
Meta<Key>space icon|window f.post_wmenu
Meta<Key>Tab root|icon|window f.next_key
Meta Shift<Key>Tabroot|icon|window f.prev_key
Meta<Key>Escape root|icon|window f.next_key
Meta Shift<Key>Escape root|icon|window f.prev_key
Meta Ctrl Shift<Key>exclamroot|icon|window f.set_behavior
Meta<Key>F6windowf.next_key transient
}
Buttons DefaultButtonBindings
{
<Btn1Down> frame|icon f.raise
<Btn3Down> frame|icon f.post_wmenu
<Btn1Down> root f.menu RootMenu
<Btn3Down> root f.menu RootMenu
Meta<Btn1Down>icon|window f.lower
Meta<Btn2Down>window|icon f.resize
Meta<Btn3Down>window f.move
}

This is where you will do any customizing.

2/21/01 page 415 B Hunter


UNIX Part I
Using the X-Windows System
Quiz

Where did the X Windows System come from?

Is it the same as Windows?

How do you start it?

What is a “root window”?

Can you customize X? If so where?

How do you scroll?

How do you iconify a window?

How do you maximize a window?

What can you do with a “Root Menu”?

What is a pull-down menu?

Is there just one version of X?

2/21/01 page 416 B Hunter


UNIX Part I
Getting Help in UNIX

Getting
Help
With
UNIX
UNIX Part I
section 13
ECT001118
Revised 1/13/99

Bruce Hunter 2/21/01 page 417


UNIX Part I
Getting Help in UNIX
Books

There is nothing like a good book. Here are a few recommendations

sed and awk byDale Dority. (O’Reilly & Asociates)

UNIX Shell Programming by Arthur & Burns. (Wiley)

The UNIX Shell Field Guide by Anderson & Anderson.


(Prentice Hall)

Portable UNIX1 by Topham. (Wiley)

The UNIX Programming Environment by Kernighan & Pike.


(Prentice-Hall)

A Guide to vi by Sonnenchen, (Prentice-Hall)

Practical UNIX Security by Garfinkle & Spaford. (O’Reilly)

The Manual

You can always do a

man keyword

to get the manual page as in

man find | lpr -P lw1

You can buy you own set of BSD manuals published by Berkeley and the
SVR4 manual set (in pieces) published by Prentice Hall.

1. titute for a manual set (up to SVR4)

Bruce Hunter 2/21/01 page 418


UNIX Part I
Getting Help in UNIX
Who Do You Call or See?

All systems supported by IT EC

Call the Customer Support Help Desk

Need Something Done?

Type request at your UNIX prompt or use ARC1.

1. Web based, go to your EC web page to start.

Bruce Hunter 2/21/01 page 419


UNIX Part I

EC
Engineering
Computing

ECT001118
Rev 0
11/1/99
Sect. 12

Bruce Hunter 2/21/01 page 420


UNIX Part I

What Is EC?
Engineering Computing is the engineering arm of IT. EC provides the
resources to do Intel’s technical work. Each major campus has its EC orga-
nization. For example Folsom’s is FMEC. Within each local EC organization
are individual support groups:

CSS (CSC) Computing Support Services, the help desk

CM (Client Management) Workstations

EAS software, applications, Softstore, Netbatch, rics, ...

CRM system administration of 1000+ compute servers

FDDM file server and distributed data (including AFS)

WES Intel Architecture Windows engineering system

ECT engineering computing training

You will become the most familiar with the help desk, CSS.

Who is who in EC?

For more information on your site EC orga-


nization consult the EC web page for your
site. For example for the Folsom campus

http://fmec.fm.intel.com

Bruce Hunter 2/21/01 page 421


UNIX Part I

EC Computers

Workstations

EC supplies and supports a number workstation types including:

AIX RISC systems running AIX (old)

Sun running Solaris

SIR iaNT systems (Intel on Intel)

Systems like Suns are provided for special purpose jobs like design but the
preferred system is the SIR box. This system boots to NT and provide UNIX
access through Exceed and SAMBA.

The SIR Build for EC Customers

SIR is standard-infrastructure release. It is a high-end Intel architecture sys-


tem running NT as its front-end system and Exceed and SAMBA to give
UNIX access to UNIX back-end compute servers.

The SIR system comes loaded with almost everything an engineering cus-
tomer could want. The NT Resource Kit is loaded as well as most of the
freeware that we are used to on UNIX such as bash and the major GNU fil-
ters and other freeware.

What is not loaded is usually available from SMS.

Bruce Hunter 2/21/01 page 422


UNIX Part I

SMS
SMS is a menu-driven NT window that allows you to pick and install soft-
ware for your system. If it is not in SMS and your organization regularly uses
it, contact your key contact to request adding it to SMS.

Compute Servers
The compute servers are the back-end muscle of our computing. The older
systems are AIX while the newer ones are HPs. An interesting addition are
a few hundred Intel systems running LINUX.

Windows 2000?

The base operating system for EC SIR workstations is


NT. Not just ordinary NT but our own release including
public domain tools like sed, bash, vim, and gawk as well
as a full UNIX interface. It provides access to all of Intel
business organizations and communications as well a
giving a high-performance front end to UNIX.

Bruce Hunter 2/21/01 page 423


UNIX Part I

Special Tools for UNIX Usage

EC provides software for UNIX and UNIX access from NT. Some are pur-
chased and other are local.

Local software:

runx

chpasswd

rics

Netbatch

COD

Purchased

eXceed

SAMBA

AFS (fs)

Tivoli

Bruce Hunter 2/21/01 page 424


UNIX Part I

runx
runx is a simple script used to start your X-Windows session. To use it sim-
ply type

runx

at the prompt of your login session.

chpasswd
chpasswd is a tool to change your UNIX and AFS password. It should be
used in preference to yppasswd. To use it type

chpasswd

from any window or from the login console.

rics
rics is a script that will seek out the best possible UNIX compute server for
your Exceed session. When you run Xsession to initiate an Xstart you will
see it in the command window.

setenv DISPLAY @D; rics -proj idt;


’set DISPLAY @D; runx -idt’

The -idt flag is the user’s group. Yours will probably be different.

Bruce Hunter 2/21/01 page 425


UNIX Part I

Netbatch

Netbatch is a system for queueing jobs on the “best possible” compute


server at that time. A job is submitted to NetBatch. Netbatch queues the job
until an appropriate NetBatch machine is available to run it. The STDOUT, if
any, is directed into a log file.

NetBatch Terminology

pool A set of machines to run jobs with one master.

class, a classification for jobs such as persistent or resubmittable

qslot. a scheduling mechanism for queueing by priority

Related Commands:

nbs

nbq

nbstat

nbqstat

nbqrm

nbslot

Bruce Hunter 2/21/01 page 426


UNIX Part I

Hummingbird eXceed

Exceed is a UNIX interface from NT. As we set it up it gives a secure login


to the least loaded available UNIX compute server. It provides an X-Window
session.

SAMBA

SAMBA mounts your UNIX file tree to your NT file tree. UNIX files and
directories can be reached from NT.

Bruce Hunter 2/21/01 page 427


UNIX Part I

AFS, the Andrew File System


AFS give a secure method of mounting file system that reside on other cam-
puses. That way cross-site projects function as easily as if they were all in
the same domain.

AFS is a distributed file system allowing a large set of machines to share


access to a set of files.

AFS uses a client/-server model, where all the data is stored on file server
machines. Files are transferred to client machines as necessary and cached
locally, on disk.

AFS Features

Scalable
It can accommodate N * 105 users

Secure
Uses Kerberos authentication

Flexibility through ACLs


More flexibility than rwx

Cached
Data accessed many time is cached locally gaining speed and relieving the
network

Bruce Hunter 2/21/01 page 428


UNIX Part I

NFS AFS

one site multiple sites


rwx protection ACL and Kerberos
local replicated
terabytes gigabytes

Hardware

Each AFS cell consists of file servers and data-base servers. Our FS to DB
server ratio is approximately 2:1. For example Folsom has 9 file servers and
4 data-base servers.

The data-base servers are the home of the Volume Location Data Base or
VLDB which is used to find the data on the file servers.

Cells and File Names

A single administrative group of servers and clients is known as an AFS cell.


There is a cell at nearly every major Intel site. The local cell, contains com-
puters such as IBMs, and HPs.

Files within AFS are named as /afs/<cell>/<filepath> and the path


names are uniform on all AFS clients everywhere in the world. The <cell>
variable specifies exactly which to look in. Most times, users will see the
@cell alias which points to the cell through which the user accesses AFS.

Volumes

UNIX divides the hard disks into partitions. AFS divides the partitions into
subsections named volumes. Each volume houses a subtree of related files
and directories.

Bruce Hunter 2/21/01 page 429


UNIX Part I

Tokens
AFS doesn’t use UNIX UIDs for authentication. To access files which are not
world accessible using AFS, you must have a valid AFS token. You may see
what tokens you currently hold using the tokens command

% tokens

Tokens held by the Cache Manager:


....

--End of list--

Changing The AFS Password

You may change your AFS password from any machine within the cell using
the kpasswd or chpasswd commands. The kpasswd change takes effect
throughout the cell immediately. To make your life a little easier use your
UNIX password and change them both art the same time with chpasswd.

Bruce Hunter 2/21/01 page 430


UNIX Part I

File Protection and ACLs

File protections are not the same in AFS as in UNIX. AFS adds to the stan-
dard UNIX file protection, with an access control list or ACL (a little like NT
using NTFS.)

ACLs are the main mechanism used to control access to files and directo-
ries. Only directories have ACLs in AFS; access to files in a directory is con-
trolled by the ACL of the directory.

When you create a new subdirectory, it automatically inherits the ACL of the
parent at that time. Changing the parent ACL later will not change the child.

ACLs override most all of the UNIX mode bits. The UNIX mode bits set on a
directory have no effect in AFS but the user bits do have meaning. These
file bits may be used to further restrict access to a file that the ACL allows
access to.

Changing a Directory’s ACL

The ACL of a directory may be changed using the fs setacl command.


By default, fs sa modifies an existing ACL, rather than replacing it For
example, if you want to give user jrambo the right to insert (i) and delete
(d) files in a directory, you can say:

fs sa /afs/scmpd/foo/bar jrambo di

Bruce Hunter 2/21/01 page 431


UNIX Part I

The fs Command
The general and administration AFS command is fs. This command is
effectively a suite of commands AFS commands are found in the fs man
page. Commands in bold are user commands.

Summary of the AFS Commands

. fs Commands are
:
apropos search by help text

checkservers check local cell’s servers

checkvolumes check volume-ID/name mappings

cleanacl clean up access control list

copyacl copy access control list

diskfree show server disk space usage

examine display volume status

exportafs enable/disable translators to AFS

flush flush file from cache

flushvolume flush all data in volume

getcacheparms get cache usage info

getcellstatus get cell status

getserverprefs get server ranks

help get help on commands

listacl list access control list

Bruce Hunter 2/21/01 page 432


UNIX Part I

listcells list configured cells

listquota list volume quota

lsmount list mount point

messages control Cache Manager messages

mkmount make mount point

monitor set cache monitor host address

newcell configure new cell

quota show volume quota usage

rmmount remove mount point

setacl set access control list

setcachesize set cache size

setcell set cell status

setquota set volume quota

setserverprefs set server ranks

setvol set volume status

storebehind store to server after file close

sysname get/set sysname (i.e. @sys) value

whereis list file’s location

whichcell list file’s cell

wscell list workstation’s cell

Bruce Hunter 2/21/01 page 433


UNIX Part I

Tivoli

Tivoli is a software suite that provides some automated system administra-


tion as well as reporting. Its primary goal is monitoring.

The Tivoli was developed by Tivoli Systems, Inc., It runs monitors on syn-
chronously or asynchronously. Tivoli retains data from previously executed
monitors which is used later to determine the action to perform Software
monitors are known as modules.

Modules are custom-designed scripts that monitor a particular resource on a


Tivoli client. When a module detects a significant change of state on a node,
an event is generated and forwarded to the Tivoli-event console.

The event console is used for event management and is rules based. The
rules shows how Tivoli should handle events. Tivoli permits support EC per-
sonnel to log information about events, act on them, and then close them.
Troubleshooting the event is done by opening windows on the UNIX system
reporting the problem. It can send e-mail, page support personnel, and gen-
erate trouble tickets.

Bruce Hunter 2/21/01 page 434

Das könnte Ihnen auch gefallen