unix 1

434
UNIX Part I Bruce Hunter 2/21/01 page 1 UNIX I Welcome to UNIX $ Sect. 1 Rev 2 ECT001118 10/1/99

Upload: manvendra86

Post on 26-Nov-2014

276 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 1

UNIX I

Welcome to

UNIX

$

Sect. 1

Rev 2ECT001118

10/1/99

Page 2: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 2

Welcome to UNIX Part I

This is an interactive course to get you started in contemporary networkedUNIX. 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 youever need to know to get started in UNIX. For most, it is the stepping stoneto 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.

Page 3: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 3

Productivity

UNIX is a productivity system. Intel is a productivity company that makesproductivity 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 thatincrease 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

Page 4: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 4

Prerequisites

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

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

Page 5: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 5

Conventions

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

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 keynames like enter for the

enter key.

Enter

Page 6: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 6

UNIX Part I

Whatis

UNIX?Welcome to

UNIX

$

Section 2Rev 7/12/99

Page 7: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 7

UNIX Part I

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 likeSamba and Exceed.

Page 8: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 8

UNIX Part I

The purpose of this section is to show you some of the major concepts ofUNIX and expose you to a few of the commands.

Flags and Commands

Boxes show the flags (options) ofthe commands for the curious whowant to know about them beforethey are introduced.

Page 9: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 9

UNIX Part I

The Kernel

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

Hardware

Drivers

The Kernel

Page 10: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 10

UNIX Part I

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

UNIX memory is complex. It consists of high-speed storage (DRAM) androtating 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 alldevice input and output is handled by device drivers.

system calls

driver bottom ends

driv

erfr

onte

nds

file systems

processes control

scheduler

memory management

Page 11: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 11

UNIX Part I

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

Page 12: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 12

UNIX Part I

The Shell, File Systems, and 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, andthe file systems.

The Kernel

The Shell

FileSystem

Net-

Utilities

work

Page 13: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 13

UNIX Part I

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

V6BSD

V7

SVR2XENIX

SunOS

4.3

ULTRIX

4.2

SVR4 SCOODT

POSIX

AIX

Solaris

OSF/1

mach

‘69

‘76

‘79

‘80

8386

‘82

‘90

‘90‘88

‘82-

‘89‘90

‘93

Ken ThompsonDennis Ritchie

Linux

Page 14: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 14

UNIX Part I

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

Page 15: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 15

UNIX Part I

The UNIX File System

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

/ (root)

mountpoint

/usr/var

/spool

/lp

/bin

/mail

/include

/

/sys

/errno.h

leafnode

mount path

Page 16: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 16

UNIX Part I

Here is a partial UNIX file tree.

/

/usr

/dev /bin /tmp /var

/lib /include /src

/spool

/man

/man1

/etc /unix

ls.1

/bin/ucb

/ls/mail

/mail

/mail

mail.1

link

The following commands are file and directoryrelated.

ls see the directory contents

pwd where am I?

cd change directories

Page 17: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 17

UNIX Part I

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?

Page 18: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 18

UNIX Part I

The Data StreamUNIX treats its data stream in a special way. Every UNIX command hasthree 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

Page 19: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 19

UNIX Part I

PipesWhat if you could connect one command, program, or script to another?

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

304% who|wc -l3

stdin

STDOUT

stderr

STDIN

stdout

stderr

who wc -l

Page 20: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 20

UNIX Part I

Redirection

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

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

You can also redirect the error stream, STDERR.

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

Note the first line. The user has switched shells.

Page 21: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 21

UNIX Part I

You can also redirect into a command from a file.

310% wc -l<who.file3

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.file316% echo fmsu04>>who.file317% rsh fmsu04 who>>who.file318% cat who.filebhunter hft/0 Jan 21 07:32bhunter pts/1 Jan 21 07:32bhunter pts/0 Jan 21 07:32fmsu04shilo 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.file0 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.

Page 22: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 22

UNIX Part I

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.) initbecomes mother to each log-on shell as each user logs on to the system.

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

init (PID 1)

login password

shell

getty

sh PID 429

fork(2)

sh PID 430exec(2)

mail(1)

(parent)

(child)

Page 23: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 23

UNIX Part I

When the command executes, it dies on exit..

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

sh PID 429

sh PID 430 mail(1)

(parent)

(child)

sh

fork

exec exit

shwaitsleep

SIGCHLD

Page 24: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 24

UNIX Part I

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?

Page 25: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 25

UNIX Part I

Memory Allocation

UNIX executables (processes) have three areas of memory.

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

Text (intructions and data)

Stack

Heap (dynamic storage)

Page 26: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 26

UNIX Part I

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. Thefirst processes you see are started by rc when the system boots.

374% ps -efUSER PID PPID C STIME TTY TIME CMDroot 1 0 0 Jan 16 - 12:30 /etc/initroot 1721 3095 0 Jan 16 - 0:00 /etc/writesrvroot 2032 1 0 Jan 16 - 4:22 /etc/syncd 60root 2740 1 0 Jan 16 - 5:09 /etc/cronroot 3095 1 0 Jan 16 - 0:00 /etc/srcmstrroot 3570 1 0 Jan 16 - 0:00 /usr/lib/errdemonroot 3616 3095 0 Jan 31 - 0:01 /etc/syslogdroot 4644 3095 0 Jan 16 - 0:00/usr/lib/sendmail -bdroot 5050 1 0 Jan 16 - 0:00 /etc/uprintfdroot 5163 3095 0 Jan 16 - 1:24 /usr/etc/portmaproot 5422 3095 0 Jan 16 - 0:00 /etc/inetdroot 5683 3095 0 Jan 16 - 1:14 /usr/sbin/snmpdroot 6071 3095 0 Jan 16 - 0:11 /etc/qdaemonroot 6206 3095 0 Jan 16 - 0:14 /usr/etc/ypbindroot 6465 3095 0 Jan 16 - 0:05 /usr/etc/biod 6root 6724 6465 0 Jan 16 - 0:05 /usr/etc/biod 6root 6981 6465 0 Jan 16 - 0:04 /usr/etc/biod 6root 7238 6465 0 Jan 16 - 0:04 /usr/etc/biod 6root 7495 6465 0 Jan 16 - 0:04 /usr/etc/biod 6root 7752 6465 0 Jan 16 - 0:04 /usr/etc/biod 6root 8010 3095 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 8267 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 8524 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 8781 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9038 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9295 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9552 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9809 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 10067 3095 0 Jan 16 - 0:00 /usr/etc/rpc.mountdroot 10326 3095 0 Jan 16 - 0:00 /usr/etc/rpc.statd

Page 27: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 27

UNIX Part I

Other process belong to the user(s):

bhunter 12370 14663 0 Jan 21 - 0:00 aixterm -e xrloginforeverbhunter 12642 17498 0 Jan 21 pts/0 0:00 -shbhunter 12911 1 0 Jan 21 hft/0 1:09 xterm

root 13260 3095 0 Jan 16 - 0:00 /usr/lpp/info/bin/infodbhunter 13444 14979 0 Jan 25 pts/2 0:26 tee /usr/system/

bhunter/fmconsole.logbhunter 13649 14663 0 Jan 21 hft/0 0:14 xclock

root 13825 1 0 Jan 16 - 3:21 loadavgd 2112root 14301 1 0 Jan 16 - 0:00 pld -s:7 -L:/usr/

spool/pld_jeager.logbhunter 14485 18836 0 Jan 25 pts/5 0:01 -shbhunter 14663 15160 0 Jan 21 hft/0 0:00 /bin/csh /usr/sys-

tem/bhunter/.xinitrc.aixbhunter 14979 1 0 Jan 25 pts/2 0:00 sh /usr/local/frame/3.0x/bin/scripts/makerbhunter 15160 15961 0 Jan 21 hft/0 0:00 csh -f /usr/local/

bin/x11root 15863 1 0 Jan 16 - 18:24 /usr/local/bin/

laservbhunter 15961 1 0 Jan 20 hft/0 0:02 -cshbhunter 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-lbhunter 16985 14663 0 Jan 21 hft/0 13:24 xautolockbhunter 17216 1 6 Jan 21 hft/0 5721:48 /usr/bin/X11/X -

n :0 -T -a 3bhunter 17498 14663 0 Jan 21 - 0:00 aixterm -e /usr/

system/mcdcad/bin/console.aixbhunter 17777 12911 0 Jan 21 pts/2 0:00 -shbhunter 18115 19138 0 08:21:03 pts/4 0:00 rlogin fmsu04bhunter 18369 1 0 08:21:03 pts/1 0:00 xterm -e rlogin

fmsu04bhunter 18531 12370 0 Jan 21 pts/1 0:00 sh /usr/local/X11/

bin/xrlogin forever

Page 28: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 28

UNIX Part I

bhunter 18836 1 1 Jan 25 hft/0 0:51 xtermbhunter 19138 18369 0 08:21:03 pts/4 0:00 rlogin fmsu04bhunter 19333 13444 0 Jan 25 pts/2 75:41 /usr/local/frame/

3.0x/bin/bin.ibm.rs6000/makerbhunter 19584 1 0 Jan 21 hft/0 0:23 xtermbhunter 21592 14663 0 Jan 21 hft/0 1:26 mwmbhunter 21761 14485 6 09:35:19 pts/5 0:00 ps -ef

375%

The ps commandAny time you need to look at the processtable execute ps from the shell.

Page 29: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 29

UNIX Part I

A long list of the process table shows the parent process id (PPID) as well.

376% ps -eflF S USER PID PPID C PRI NI ADDR SZ WCHAN STIME TTY

TIME CMD202803 S root 1 0 0 60 20 1004 256 Jan 16 - 1

2:30 /etc/init240801 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/laservbhunter 15961 1 0 Jan 20 hft/0 0:02 -cshbhunter 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.

Page 30: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 30

UNIX Part I

Filters

The UNIX analogy of pipes and streams is a hydraulic analogy. If data flowsthrough 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 indepth later.

Regular Expressions

A regular expression, or RE, is a string that represents any string that it willmatch. The RE /foo/ will match foo in foobar or in foofoo. Regularexpressions 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.

Page 31: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 31

UNIX Part I

grep

In the primitive UNIX editor, ed, you can do a global search on a regularexpression and have it print.

g/RE/p

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

389% cat foo122333444455555666666777777788888888999999999end

390% grep end fooend

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

391% ps -ef |grep shbhunter 12133 10852 0 Jan 28 pts/6 0:00 -shbhunter 12642 17498 0 Jan 21 pts/0 0:00 -shbhunter 14485 18836 0 Jan 25 pts/5 0:01 -shbhunter 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 -cshbhunter 16257 19584 0 Jan 21 pts/3 0:00 -shbhunter 17777 12911 0 Jan 21 pts/2 0:00 -shbhunter 22346 14485 2 10:34:09 pts/5 0:00 grep sh

392%

grep flags

-v "I dont want to hearabout it!"

-s work silent

-n give line numbers

-c count lines

Page 32: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 32

UNIX Part I

sed

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

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

fmsu01:/28u008sv 1836252 1017586 44% - - /vproj/28u008sfmsu01:/training/mproj 192383 168156 12% - - /mproj/trainingfmsu01:/training/uproj 192383 168156 12% - - /uproj/trainingfmsu01:/p42c 1312925 444590 66% - - /vproj/p42cfmsu05:/28f008sm 917862 600054 34% - - /mproj/28f008sfmsu05:/28f008su 917870 826071 10% - - /uproj/28f008sfmsu01:/28f008sg 917870 826071 10% - - /gproj/28f008sfmsu05:/f2f4gr 458671 412791 10% - - /gproj/f2f4fmsu01:/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/work397%

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

Page 33: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 33

UNIX Part I

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

Page 34: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 34

UNIX Part I

awk

awk is a pattern matching language as well as a filter. It understandsrecords 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 particularuser’s $HOME.

407% ypcat passwd |grep bhunterbhunter:rzojv8/Uf2q92:100:0:Bruce H Hunter:\

/usr/system/bhunter:/bin/csh410% ypcat passwd |grep bhunter|awk -F: ’{ print $6 }’/usr/system/bhunter411%

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

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

awk Flags

-F field delimiter

-f take commands from file

Page 35: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 35

UNIX Part I

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

Page 36: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 36

UNIX Part I

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 }’ | sort28f008s28f016s28u008s87ff800abashiracesachanadhakeadmafaiziafarinasagafkenaibrahimakrahmer..vbuivdalvivdiscipuvheilviewlogicvirsinghvlakhanivlawvlocasiovwplotwchangwlchangwmansfieyxiaolinzahrazippy425%

sort Flags

-d dictionary order

-n nummeric

-r reverse

-f fold to lower case

-u uniq

Page 37: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 37

UNIX Part I

tr

tr translates from column 1 to column2. Change the data stream’s lettersfrom 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’

Page 38: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 38

UNIX Part I

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 separatethe 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.

AIXbosfileforIBMininformationlppofOperatingpertinentPleaseREADMEreleaseseeSystemthethistousrVersionWelcome

\012 is a newline

Page 39: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 39

UNIX Part I

Putting It Together for More Productivity

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

% whognorton 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)

Page 40: Unix 1

What is UNIX?

Bruce Hunter 2/21/01 page 40

UNIX Part I

OK that is just part of the data. Now reduce it to something useful. First howmany logins?

% who | wc -l102

Next. Who are they, the short form.

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

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.

Page 41: Unix 1

Bruce Hunter 2/21/01 page 41

Logging InUNIX Part I

LoggingIntoUNIX

login:

I UNIX Part IECT001118

Last Rev 7/12/99section 3

Page 42: Unix 1

Bruce Hunter 2/21/01 page 42

Logging InUNIX Part I

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 thescreen, usually at the bottom. Otherwise the screen will always be blank.Graphic systems may have an elaborate login screen with a central displayrequesting your logname and password.

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

login: jrambopassword:

Page 43: Unix 1

Bruce Hunter 2/21/01 page 43

Logging InUNIX Part I

Getting A User Account

You need a user account to get on to a UNIX system and you will get it fromyour local support organization, like Folsom Engineering’s FMEC CSC..They will assign a login name that uses your first initial and no more thanseven 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 agroup number and home directory. Your manager will also have to sign foryou to accept responsibility for your actions.

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

Page 44: Unix 1

Bruce Hunter 2/21/01 page 44

Logging InUNIX Part I

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% cd51% 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." Theyare not shown when you doa ls. A ls -a however willshow them.

Page 45: Unix 1

Bruce Hunter 2/21/01 page 45

Logging InUNIX Part I

Primary Dot Files

There are two that you cannot survive without. They are

.loginand

.cshrc

for C shell and tcsh users and

.profile

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

TERM=xtermSHELL=/bin/cshHOME=/usr/system/bhunterLOGNAME=bhunterUSER=bhunterPath=/usr/ucb:/usr/bin:/bin:/usr/local/bin:/usr/system/bhunter/bin:TZ=PST8PDTLOCPATH=/usr/lib/nls/locEXINIT=map z xpMANPATH=/usr/local/manEDITOR=viXENVIRONMENT=/usr/system/bhunter/.Xdefaults.aixXINITRC=/usr/system/bhunter/.xinitrc.aixDISPLAY=unix:0.0

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

Page 46: Unix 1

Bruce Hunter 2/21/01 page 46

Logging InUNIX Part I

Your .login looks like

#!/bin/cshset mail = /var/spool/mail/bhunterbiff nstty eraseset noglob; eval ‘tset -Q -s‘; unset noglobset prompt = "\!% "set EDITOR = vilimit coredumpsize 0set path = ( /usr/ucb /usr/bin /bin /usr/local/bin /usr/system\bhunter/bin .)

set MANPATH = /usr/local/man

Your .cshrc looks like

#!/bin/cshstty -nlalias h historyalias l lookalias top "ls -lt | sed -n ’1,20p’"set history = 24set prompt = ’\!% ’setenv EXINIT "map z xp"setenv FMHOME /usr/local/frame/3.0xset path = ( $FMHOME/bin $path )setenv MANPATH /usr/local/mansetenv EDITOR vi

Page 47: Unix 1

Bruce Hunter 2/21/01 page 47

Logging InUNIX Part I

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 justthe same way there are differentshells to meet your needs and likes.

Page 48: Unix 1

Bruce Hunter 2/21/01 page 48

Logging InUNIX Part I

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

Page 49: Unix 1

Bruce Hunter 2/21/01 page 49

Logging InUNIX Part I

The flags and arguments may be optional. As an example you could use thelist command simply as

% ls

or with a flag

%ls -l

or even two

% ls -CF

or three

% ls -lit

and more

% ls -alit

and you can add arguments like

% ls .profile

or flags and arguments

$ ls -ld /

A few ls flags

-a see dot files

-l long

-t by time

-d list directories as directories

-F fold into columns

Page 50: Unix 1

Bruce Hunter 2/21/01 page 50

Logging InUNIX Part I

A Few Things Unique to UNIX

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

62% pwd/usr/system/bhunter63%

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 mandatoryfile extensions as in other operating systems like foo.exe, foo.bat, orfoo.com.

Page 51: Unix 1

Bruce Hunter 2/21/01 page 51

Logging InUNIX Part I

Passwords

UNIX passwords do not exist on the machine. Your password is encryptedby the UNIX crypt(3) command, and the results of that encryption arestored in /etc/passwd or in YP’s passwd map.

217% ypcat passwd|grep kleeklee: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 inqwerty. It must be nothing that can be looked up in a dictionary, and use nodoggy/kitty guessable names. An example of a good password for a skierwho 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

Page 52: Unix 1

Bruce Hunter 2/21/01 page 52

Logging InUNIX Part I

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 Xdirectories:

./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 pathslooked like:

39% grep path ~/.login ~/.cshrc/usr/system/bhunter/.login:set path = ( /usr/ucb /usr/bin /bin /usr/local/bin .)

Page 53: Unix 1

Bruce Hunter 2/21/01 page 53

Logging InUNIX Part I

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?

Page 54: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 54

UNIX Part I

Files

UNIX Part I

Rev. 7/12/99ECT001118section 4

Page 55: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 55

UNIX Part I

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-nodenumber.

They are stacked as a hierarchy.

I-Nodes

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

NTFS (NT File System)

NT’s NTFS is POSIX (unix) complient.

Page 56: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 56

UNIX Part I

The File Tree

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

/ (root)

mountpoint

/usr/var

/spool

/lp

/bin

/mail

/include

/

/sys

/errno.h

leafnode

mount path

Page 57: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 57

UNIX Part I

Parents and Children

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

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

currentposition

parent

child

childofchild

.

..

Page 58: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 58

UNIX Part I

Traditional UNIX Directories

Some directories are traditional to all UNIX systems.

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

/

/usr

/dev /bin /tmp /var

/lib /include /src

/spool

/man

/man1

/etc /unix

ls.1

/bin/ucb

/ls/mail

/mail

/mail

mail.1

link

Page 59: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 59

UNIX Part I

Path Names and Base Names (FQN)Look at the file tree again to see paths.

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 aFQN, a fully-qualified name.

/

/usr

/dev /bin /tmp /var

/lib /include /src

/spool

/man

/man1

/etc /unix

ls.1

/bin/ucb

/ls/mail

/mail

/mail

mail.1

link

Page 60: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 60

UNIX Part I

Relative Paths

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

cd man

orcd /usr/lib/man

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

cd ../ucb

/

/usr

/dev /bin /tmp /var

/lib /include /src

/spool

/man

/man1

/etc /unix

ls.1

/bin/ucb

/ls/mail

/mail

/mail

mail.1

link

Page 61: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 61

UNIX Part I

Mounts

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

31% /etc/mountnode 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,intrfmsu05 /usr/fmo/ssd /usr/fmo/ssd nfs Jan 16 07:56 rw,hard,bg,intrfmsu01 /usr/spool/mail /usr/spool/mail nfs Jan 16 07:56 rw,hard,bg,intrfmsu01 /amoeba /mproj/amoeba nfs Jan 16 07:56 rw,hard,bg,intrfmsu01 /sam16u3 /uproj/sam163 nfs Jan 16 07:56 rw,hard,bg,intrfmsu05 /samgr /gproj/sam16 nfs Jan 16 07:56 rw,hard,bg,intrfmsu01 /g16v1 /vproj/sam161 nfs Jan 16 07:56 rw,hard,bg,intrfmsu05 /g16v /vproj/sam16 nfs Jan 16 07:56 rw,hard,bg,intrfmsu01 /sam16u2 /uproj/sam162 nfs Jan 16 07:56 rw,hard,bg,intr

.

.

or the df command

32% dfFilesystem 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/workfmsu01:/misc 819342 149786 81% - - /miscfmsu05:/usr/fmo/ssd 944238 83861 91% - - /usr/fmo/ssdfmsu01:/usr/spool/mail 408391 290661 28% - - /usr/spool/mailfmsu01:/amoeba 408399 247000 39% - - /mproj/amoebafmsu01:/sam16u3 1893612 486009 74% - - /uproj/sam163fmsu05:/samgr 917358 73783 91% - - /gproj/sam16fmsu01:/g16v1 1836252 746162 59% - - /vproj/sam161

.

.

Page 62: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 62

UNIX Part I

Moving Around Directories

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

19% pwd/usr

Use the cd command to move:

20% cd lib21% pwd/usr/lib

Use ls to get a listing:

22% lsINnet lex libodm.a nmfMail.rc lib.b liboldX.a ntermX11 lib300.a libplot.a objreposacct lib300S.a libpp.a phones-filealiases lib300s.a libprint.a profiledasw lib4014.a libqb.a psbind lib450.a librpcsvc.a pse.expboot libIM.a librts.a qm.expbugfiler libMrm.a libs.a rasbugformat libPW.a libsm.a refercalprog libSIMshr.a libsnmp.a remote-filecfgodm.ipl libUil.a libsrc.a sacflow1 libX11.a libsys.a securitycpp libXdmcp.a libtermcap semutilcrt0.o libXext.a libtermcap.a sendmaildag libXfx.a libtli.a sendmail.cfderoff libXm.a liby.a sendmail.

Page 63: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 63

UNIX Part I

cd

The cd command defaults to your home directory, $HOME. Let’s go back towhere you were when you did a long, long ls.

glink.o libisode.a methods unixtomhgraf libl.a mh uucphelp libld.a microcode vfontedprinst_updt liblvm.a mkwhatis vgrindefsinstl libm.a ms w2006kernex.exp libmsaa.a netsvc xpasslearn libnck.a nls yaccpar23%23% pwd/usr/lib24% cd25% pwd/usr/system/bhunter26% echo $HOME/usr/system/bhunter27%

pushd and popd

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

bash-2.00$ pwd/usr/users/a5fs/bhunter/weeklysbash-2.00$ pushd /fes/bhunter/fes/bhunter ~/weeklysbash-2.00$ pwd/fes/bhunterbash-2.00$ lsC NT PERL class.foilsbash-2.00$ popd~/weeklys

Page 64: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 64

UNIX Part I

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.

Page 65: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 65

UNIX Part I

ls

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

command_name -flag

-l is the flag for a long listing:

33% cd /usr/include34% ls -ltotal 1848drwxr-xr-x 2 bin bin 512 Dec 14 1992 DPSdrwxr-xr-x 2 bin bin 512 Dec 14 1992 INlrwxrwxrwx 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.hlrwxrwxrwx 1 root system 24 Dec 14 1992 X11 -> /usr/lpp/X11/include/X11lrwxrwxrwx 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.

Page 66: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 66

UNIX Part I

That is more then you may want to see. You can do a ls -CF to fold theoutput for compactness .

35% ls -FDPS/ dbug.h isode/ nlist.h syms.hIN/ dbxstclass.h jcode.h notice.h sys/Mrm@ defenv.h jctype.h odmi.h sysexits.hNLchar.h diag/ jfs/ paths.h syslog.hNLctype.h dirent.h langinfo.h pcsim@ tar.hNLregexp.h dumprestor.h ldfcn.h piostruct.h tcpip_audit.hNLxio.h errno.h limits.h poll.h term.hX11@ exceptab.h linenum.h procinfo.h termio.hXm@ execargs.h loader.h protocols/ termios.ha.out.h fatal.h locale.h pwd.h time.hacl.h fcntl.h login.h regex.h tiuser.haio.h filehdr.h lprio.h regexp.h ttyent.haixfont.h float.h lvm.h reloc.h typchk.haixif/ fnmatch.h lvmrec.h resolv.h [email protected] fp.h macros.h rpc/ uinfo.har.h fp_fort_c.f malloc.h rpcsvc/ ulimit.harpa/ fp_fort_t.f math.h scnhdr.h unctrl.hasl.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 X11X11: symbolic link to /usr/lpp/X11/include/X11.39% file X11 nfsX11: symbolic link to /usr/lpp/X11/include/X11.nfs: directory40%

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

Page 67: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 67

UNIX Part I

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. ~ savestime.

Which is faster?

cd /usr/users/a5fs/bhunter/perl

or

cd ~/perl

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

bash-2.00$ env | grep WORKWORK=/fes/bhunterbash-2.00$ pwd/usr/users/a5fs/bhunterbash-2.00$ cd $WORKbash-2.00$ pwd/fes/bhunter

Page 68: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 68

UNIX Part I

man

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

69% man lsls 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 contentsof each specified Directory or the name of each speci-fied File, along with any other information you ask forwith the flags. If you do not specify a File or Direc-tory,the ls command displays the contents of the currentdirectory.

.

Page 69: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 69

UNIX Part I

Some commands like ls have very long man pages. The first thing youwant to do is find the meanings of the flags.

The mode displayed with the -e and -l flags is interpretedas 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 flagwas specified or the symbolic link did not point to anexisting 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.

Page 70: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 70

UNIX Part I

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 entriethat begin with a . (dot).

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

-c Uses the time of last modification of the i-node foreither sorting (when used with the -t flag) or fordisplaying (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 helpfulwhen 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

Page 71: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 71

UNIX Part I

Signals

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

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

Page 72: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 72

UNIX Part I

Metacharacters and Wild Cards, Globbing

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

Wild cards are a subset of metacharacters. They expand shell’s ability todeal 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]

Page 73: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 73

UNIX Part I

Here is a very busy C source code directory. How busy is it (how many Cprograms), and are there any header (.h) files?

323% cd;cd c.c324% lsREADME error.c io.c prbarry.c str.oTD.c ex15.c jump.c prptr.c strcopy.ca.out ex21.c jumpt.c prt1.c stringi.cab.c ex23.c lab.c psum.c strings.carg.c ex25.c letter.c ptrtostruct.c struct.cargold.c ex27.c ln1.c recoil.c struct2.cascii.c f.c ln2.c reply.c structa.casum.c fac.c ln3.c s.c structp.cati.c fapp.c ln4.c s10.c subs.cbell.c fib.c lname.c s10_10.c substring.cbit.c fiban.c malo.c seek.c swap.cbitA.c fibon.c max.c seekOLD.c sys.cbit_a.c fin.c max2.c simpstruct.c tD.cbita.c flag.c me.c srt.c testi.cbitf.c flags.c nexts.c ss.c time.cbitup.c for.c odd.c sscan.c timesequal.cbufio.c fout.c onesc.c ssr.c top.cc_l.c functproto.c orperms.c sst.c true.ccase.c get.c pay_lab.c st.c twoD.ccase_lab.c gstring.c payroll.c st2.c uf.ccast.c h.c perms.c st_a.c ufp.cch5.c hello.c pl.c st_funct.c userfunct.cchar_val.c idx.c po.c statauto.c usrf_ret.cdefine.c idxp.c porrage.c stcmp.c w_struct_sun.cdowhile.c infunct.c pound.c sti.c while.cecho.c inode.h pr.c str.c write_struct.c

325% ls *.hinode.h326% ls *.c|wc -l

235

Page 74: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 74

UNIX Part I

How many C files start with an s or a t?

327% ls [st]*.cs.c sscan.c stcmp.c structa.c time.cs10.c ssr.c sti.c structp.c timeseq.cs10_10.c sst.c str.c subs.c top.cseek.c st.c strcopy.c substring.c true.cseekOLD.c st2.c stringi.c swap.c twoD.csimpstruct.c st_a.c strings.c sys.csrt.c st_funct.c struct.c tD.css.c statauto.c struct2.c testi.c328%

How many C files start with st?

328% ls st*.cst.c st_funct.c sti.c stringi.c struct2.cst2.c statauto.c str.c strings.c structa.cst_a.c stcmp.c strcopy.c struct.c structp.c329%

Do any files start with uppercase characters?

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

Page 75: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 75

UNIX Part I

Making A Directory

The mkdir command makes a new directory:

279% cd280% cd junk281% mkdir snarg282% ls -ld snargdrwxr-xr-x 2 bhunter system 512 Feb 01 13:44 snarg283% dateTue Feb 1 13:44:52 PST 1994284%

rmdir

rmdir removes a directory:

284% rmdir snarg285% ls -ld snargls: 0653-341 The file snarg does not exist.286%

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

Page 76: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 76

UNIX Part I

Here a directory snarg is made and an empty file put in (with the touchcommand). Then an attempt is made to remove snarg with rmdir.

286% mkdir snarg287% touch ./snarg/bar288% ls -l snargtotal 0-rw-r--r-- 1 bhunter system 0 Feb 01 13:49 bar289% rmdir snargrm: 0653-603 Cannot remove directory snarg.290%

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

291% rm ./snarg/bar292% rmdir snarg293%

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

Page 77: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 77

UNIX Part I

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 filesin /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]*

Page 78: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 78

UNIX Part I

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>nittNow is the timefor all good mento come to the aidof their country.^D

Did it work?

453% cat nittNow is the timefor all good mento come to the aidof their country.454%

Page 79: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 79

UNIX Part I

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

Page 80: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 80

UNIX Part I

vi in 30 seconds :-)

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

% vi foo

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

now is the time~~~~~~~~~~~~~~~~~~~~~~~~~foo [new file]

Page 81: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 81

UNIX Part I

You’re on the command line.

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

Now is the time~~~~~~~~~~~~~~~~~~~~~~~~: wq

Page 82: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 82

UNIX Part I

Moving and Renaming Files

If a file system is on another mounted partition, it has to be copied byte forbyte 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 characters461% mv bar /tmp462% ls /tmpaixfile mpeng.x11 xresources.19054.bakbar xresources.14543.bak xresources.19129.bakbhunter.x11 xresources.16629.bak xresources.21812.bakerr.out xresources.18053.bakerrlog xresources.18348.bak463% rm /tmp/bar464%

mv is also used to rename a file:

464% touch thx314465% mv thx314 a.file466%

466% lsa.file bs ione rev true.fw.3.backupadduser.out bu nitt reverse truefapp find90 perl.f snarg vi.fmtbhh.shell foo r t w.3

Page 83: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 83

UNIX Part I

Removing files: rm

The rm command removes (unlinks) a file.

294% cd snarg295% touch foo bar snarg296% lsbar foo snarg297%297% rm snarg298% lsbar foo299% rm bar300% lsfoo301% rm foo

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

302% rm foobarrm: foobar: A file or directory in the path name doesnot exist.303%

In almost any operation system a removed file isgone! Some allow them to be restored with spe-cial utilities if the file structures haven beenchanged (much.) UNIX would allow a file to bereconstructed but one problem, a multi-processsystem like UNIX and NT never sits still andquickly recovers and reuses the space the filepreviously occupied.

Page 84: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 84

UNIX Part I

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 thecode:)

Now look at a different scenario.

341% lsbar.c error.file foo.h who.filebar.h foo.c snarg.c

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

343% rm * .crm: .c: A file or directory in the path name does notexist.344% ls345%

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

Page 85: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 85

UNIX Part I

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 treatedas file names. This allows you to specify file names startingwith one - (dash).

Page 86: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 86

UNIX Part I

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. Doan

rm -i ?

The system will ask

remove * ?

and you say

y

You have a huge project directory, your project is done, and you have hadeverything 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 itjust a while back.

Page 87: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 87

UNIX Part I

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 calledtrue.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.

Page 88: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 88

UNIX Part I

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 ofwc. There are two files, true.f and truef. They were named from thetrue 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 truef515%

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

515% wc true*47 114 837 true.f47 114 839 truef94 228 1676total516%

Same number of lines, same number of words, different number of charac-ters? How do you know for sure?

Page 89: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 89

UNIX Part I

cmp

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

cmp file1 file2

519% cmp true.f trueftrue.f truef differ: byte 33, line 4520%

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

1 true or false Command234 Purpose56 Returns an exit value of zero (true) or a nonzero exit value7 (false).

1 true or false Command234 purpose56 Returns an exit value of zero (true) or a nonzero exit value7 (false).

Page 90: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 90

UNIX Part I

diffdiff tells you

1) If files are different

2) Where they are different

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

diff file1 file2

516% diff true.f truef4c4< Purpose---> purpose17c17< The true command returns a zero exit value. The false command---> The true command returns zero exit value while the false com-mand517%

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

Page 91: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 91

UNIX Part I

links

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

349% pwd/usr/system/bhunter/junk/snarg350% 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 \00000020 8100 2e2e 0000 0000 0000 0000 0000 0000

\0 . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0\00000040351% ls352%

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

352% touch foo353% !odod -xc .0000000 0533 2e00 0000 0000 0000 0000 0000 0000

005 3 . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0\0 \00000020 8100 2e2e 0000 0000 0000 0000 00000000

\0 . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0\00000040 053a 666f 6f00 0000 0000 0000 0000 0000

005 : f o o \0 \0 \0 \0 \0 \0 \0 \0 \0\0 \00000060354%

Page 92: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 92

UNIX Part I

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 foo369%

Page 93: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 93

UNIX Part I

Hard LinksA hard link is a link created by the system administrator to give anothername to a file or device. /dev has devices with multiple names. The floppydrives are a good example.

357% ls -l fd*brw-rw-rw- 1 root system 11, 0 Jan 24 14:10 fd0brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0.18brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 fd0.9brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0hbrw-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 samedevice? 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 fd033 brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0.1819 brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 fd0.933 brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0h34 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

Page 94: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 94

UNIX Part I

Soft or Symbolic Links

Sometimes a file or a directory isn’t where you think it is. A link is createdto 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 itis in /usr/intel/bin.

370% ls -l /usr/bin/perllrwxrwxrwx 1 root system 19 Dec 14 1992

/usr/bin/perl -> /usr/local/bin/perl371%

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 pathname.

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.

Page 95: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 95

UNIX Part I

What Kind Of File Is It?

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

49% pwd/usr/system/bhunter/c.c50% file README ab.c a.outREADME: ascii textab.c: c program texta.out: executable (RISC System/6000 V3.1

or object module not stripped51%

51% cd52% cd scripts53% file *2args: commands textEWS: ascii textalchk: commands textargs: English textarticl: [nt]roff, tbl, or eqn input textbase8: commands textcleanx: commands textderoff: ascii textdmon: commands textdmonb: commands textenter: ascii textetr: c program text..

rwall: commands textsed: directoryshf: English textsubst: commands textt: emptyth: commands textuntil: ascii textyorn: ascii text54%

Page 96: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 96

UNIX Part I

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 yourhome directory. How are you going to do it? What is the command syntax?

Page 97: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 97

UNIX Part I

Looking At Files

catThe most primitive of the look at commands is cat.

259% cat foo122333444455555666666777777788888888999999999end

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 incharacters, lines, and words. Here the -w (word count) flag is used.

265% wc -l PLAN207 PLAN266%

Page 98: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 98

UNIX Part I

moreHere is more turned loose on the original outline for this class.

266% more PLANNew 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 TypeInteractive with multiple labs

OutlineWhat Is UNIX?

A Brief Discription of the UNIX Operating System

Multi-user, multi process, general purpose, andprogrammer friendly

Fully networkable

Currently favoring distributed computing and sharedresources

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.

Page 99: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 99

UNIX Part I

more options and flags

You will move forward a line if you hit enter in more. Strike the space barand 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 thenext screen of text is displayed.

-s Squeezes multiple blank lines from the output to produce onlyone blank line.

-u Prevents more from underlining or from creatingstand-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.

+numberStarts at the line specified by the number.

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

Page 100: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 100

UNIX Part I

K SpacebarDisplays 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 afull screen of lines.

Kf Skips forward the number of screens specified for K and printsa full screen of lines.

Kb Skips backwards K number of screens and prints a full screenof lines.

K^B Skips backwards K number of screens and prints a full screenof 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 %commandInvokes 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.

Page 101: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 101

UNIX Part I

vi

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

vi -R

Also look at vedit.

Page 102: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 102

UNIX Part I

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 cat270%

Page 103: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 103

UNIX Part I

odod 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.out0000000 000737 000007 026511 065606 000000 005022 000000 0000620000020 000110 010002 000413 000001 000000 001020 000000 0001040000040 000000 000020 000000 000050 000000 001000 000000 0000000000060 000000 000060 000004 000002 000004 000004 000007 0000050000100 000002 000003 030514 000000 000000 000000 000000 0000000000120 000000 000000 000000 000000 000000 000000 027160 0605440000140 000000 000000 000000 000000 000000 000000 000000 0000240000160 000000 000754 000000 000000 000000 000000 000000 0000000000200 000000 000010 027164 062570 072000 000000 000000 0010000000220 000000 001000 000000 001020 000000 001000 000000 0044420000240 000000 000000 000015 000000 000000 000040 027160 0605440000260 000000 000000 000000 000000 000000 000000 000000 0007600000300 000000 002020 000000 000000 000000 000000 000000 0000000000320 000000 000010 027144 060564 060400 000000 000000 0000000000340 000000 000000 000000 000104 000000 003000 000000 0046440000360 000000 000000 000013 000000 000000 000100 027142 0715630000400 000000 000000 000000 000104 000000 000104 000000 000020

Page 104: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 104

UNIX Part I

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

$ od -x a.out0000000 01df 0007 2d49 6b86 0000 0a12 0000 00320000020 0048 1002 010b 0001 0000 0210 0000 00440000040 0000 0010 0000 0028 0000 0200 0000 00000000060 0000 0030 0004 0002 0004 0004 0007 00050000100 0002 0003 314c 0000 0000 0000 0000 00000000120 0000 0000 0000 0000 0000 0000 2e70 61640000140 0000 0000 0000 0000 0000 0000 0000 00140000160 0000 01ec 0000 0000 0000 0000 0000 00000000200 0000 0008 2e74 6578 7400 0000 0000 02000000220 0000 0200 0000 0210 0000 0200 0000 09220000240 0000 0000 000d 0000 0000 0020 2e70 61640000260 0000 0000 0000 0000 0000 0000 0000 01f00000300 0000 0410 0000 0000 0000 0000 0000 00000000320 0000 0008 2e64 6174 6100 0000 0000 00000000340 0000 0000 0000 0044 0000 0600 0000 09a40000360 0000 0000 000b 0000 0000 0040 2e62 73730000400 0000 0000 0000 0044 0000 0044 0000 0010

How about in both character and hex?

$ od -xc a.out |head0000000 01df 0007 2d49 6b86 0000 0a12 0000 0032

001 ß \0 007 - I k \0 \0 \n 022 \0 \0 \0 20000020 0048 1002 010b 0001 0000 0210 0000 0044

\0 H 020 002 001 013 \0 001 \0 \0 002 020 \0 \0 \0 D0000040 0000 0010 0000 0028 0000 0200 0000 0000

\0 \0 \0 020 \0 \0 \0 ( \0 \0 002 \0 \0 \0 \0 \00000060 0000 0030 0004 0002 0004 0004 0007 0005

\0 \0 \0 0 \0 004 \0 002 \0 004 \0 004 \0 007 \0 0050000100 0002 0003 314c 0000 0000 0000 0000 0000

\0 002 \0 003 1 L \0 \0 \0 \0 \0 \0 \0 \0 \0 \0$

Page 105: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 105

UNIX Part I

The -c flag makes more sense with characters in the file.

275% od -xc ~/foo0000000 2020 2020 2020 2020 310a 0932 320a 0933

1 \n 2 2 \n 30000020 3333 0a09 3434 3434 0a09 3535 3535 350a

3 3 \n 4 4 4 4 \n 5 5 5 5 5 \n0000040 0936 3636 3636 360a 0937 3737 3737 3737

6 6 6 6 6 6 \n 7 7 7 7 7 7 70000060 0a09 3838 3838 3838 3838 0a09 3939 3939

\n 8 8 8 8 8 8 8 8 \n 9 9 9 90000100 3939 3939 390a 2020 2020 2020 2020 656e

9 9 9 9 9 \n e n0000120 640a

d \n0000122

Page 106: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 106

UNIX Part I

Do I Have Enough Space?

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

255% dfFilesystem 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/workfmsu01:/misc 819342 149781 81% - - /miscfmsu05:/usr/fmo/ssd 944238 84130 91% - - /usr/fmo/ssdfmsu01:/usr/spool/mail 408391 290868 28% - /usr/spool/mailfmsu01:/amoeba 408399 247002 39% - - /mproj/amoebafmsu01:/sam16u3 1893612 468626 75% - - /uproj/sam163fmsu05:/samgr 917358 44177 95% - - /gproj/sam16fmsu01:/g16v1 1836252 746162 59% - - /vproj/sam161fmsu05:/g16v 1278413 77031 93% - - /vproj/sam16fmsu01:/sam16u2 1835996 572904 68% - - /uproj/sam162fmsu01:/sam16u1 917870 76799 91% - - /uproj/sam161fmsu05:/sam16u 1968100 185982 90% - - /uproj/sam16fmsu05:/sam16 1278413 141670 88% - - /mproj/sam16fmsu01:/sam16m1 917870 48603 94% - - /mproj/sam161fmsu05:/g16 1278413 587612 54% - - /gproj/sam161fmsu05:/samgr2 1298653 423523 67% - - /gproj/sam162fmsu05:/e2 944238 739466 21% - - /e2fmsu01:/28u008s 508958 93717 81% - - /gproj/28u008sfmsu05:/usr/mfg 638430 35947 94% - - /usr/mfg

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

Page 107: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 107

UNIX Part I

Who Took All The Space?

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

256% cd257% du120 ./bin4 ./LAN352 ./mail18 ./p-h16 ./AIX10 ./ez4 ./lpr2 ./local/bin4 ./local/local/bin6 ./local/local10 ./local..

26 ./stalker2 ./root_owned2 ./hunter2 ./restore56 ./x4001790 ./intro36 ./weeklys/9354 ./weeklys8 ./travel8 ./chandler12 ./justification18 ./c.class.comments22 ./sed41012 .258%

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

Page 108: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 108

UNIX Part I

QuizWhat 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 youcan.

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)?

Page 109: Unix 1

Files and File Systems

Bruce Hunter 2/21/01 page 109

UNIX Part I

Productivity Tricks for Files and File Systems

Keep directories small. File searching is sequential and that is inefficient inlarge 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 thefind 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 nothave enough room so find out before wasting the computer’s, the network’sand your time.

Page 110: Unix 1

The shells

Bruce Hunter2/21/01 page 110

UNIX Part I

The Shells

sh

csh

ksh

UNIX Part I

bash

tcsh

pdksh

Rev 7/12/99ECT001118

Section 5

zsh

Page 111: Unix 1

The shells

Bruce Hunter2/21/01 page 111

UNIX Part I

Shell?

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

Full cusomization by and for the user

Ease of use

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

Job Control

Command linkage

Command to file “redirection”

Automation of log-on and log-off tasks

Automated start up of other automated tasks

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

Speed

UNIX command compatibility

Features

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

csh

ksh

sh

tcsh

bash

rsh

pdksh

zsh

Page 112: Unix 1

The shells

Bruce Hunter2/21/01 page 112

UNIX Part I

ConventionsThis class will deal with three of the shells. When looking at examples, youwill 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 exposedto Bourne, Korn, and C shells. You will even get into shell programming.Here more than any one place are the keys to productivity.

Page 113: Unix 1

The shells

Bruce Hunter2/21/01 page 113

UNIX Part I

There Is More Than One Shell

The shell is a very special process. It is your command interpreter, it is aprogramming 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 acommand-line editor programmable word completion,and spelling correction

Page 114: Unix 1

The shells

Bruce Hunter2/21/01 page 114

UNIX Part I

Globbing

Globbing is a funny-sounding name for file-name expansion. The shells livein a world of files and directories, and that is their orientation. The shellshave 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*.cbell.c bit.c bitA.c bit_a.c bita.c bitf.cbitup.c bufio.c163%

Now a search of all three-letter prefixes.

163% ls ???.carg.c c_l.c fib.c get.c ln1.c ln4.c s10.c sst.c str.cati.c ch5.c fin.c idx.c ln2.c max.c srt.c st2.c sys.cbit.c fac.c for.c lab.c ln3.c odd.c ssr.c sti.c top.c164%

A search for just header files.

164% ls *.hinode.h

Look for inode programs and header files.

168% !165ls inode.[ch]inode.c inode.h169%

Page 115: Unix 1

The shells

Bruce Hunter2/21/01 page 115

UNIX Part I

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

Page 116: Unix 1

The shells

Bruce Hunter2/21/01 page 116

UNIX Part I

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.hTD.c bitup.c ex15.c flags.c io.cab.c bufio.c ex21.c for.c jump.carg.c c_l.c ex23.c fout.c jumpt.cargold.c case.c ex25.c functproto.c lab.cascii.c case_lab.c ex27.c get.c letter.casum.c cast.c f.c gstring.c ln1.cati.c ch5.c fac.c h.c ln2.cbell.c char_val.c fapp.c hello.c ln3.cbit.c define.c fib.c idx.c ln4.cbitA.c dowhile.c fiban.c idxp.c lname.cbit_a.c echo.c fibon.c infunct.cbita.c envp.c fin.c inode.c175%

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.cmalo.c pound.c simpstruct.c str.o time.cmax.c pr.c srt.c strcopy.c timesequal.cmax2.c prbarry.c ss.c stringi.c top.cme.c prptr.c sscan.c strings.c true.cnexts.c prt1.c ssr.c struct.c twoD.codd.c psum.c sst.c struct2.c uf.conesc.c ptrtostruct.c st.c structa.c ufp.corperms.c recoil.c st2.c structp.c userfunct.cpay_lab.c reply.c st_a.c subs.c usrf_ret.cpayroll.c s.c st_funct.c substring.c w_struct_sun.cperms.c s10.c statauto.c swap.c while.cpl.c s10_10.c stcmp.c sys.c write_struct.cpo.c seek.c sti.c tD.c

Page 117: Unix 1

The shells

Bruce Hunter2/21/01 page 117

UNIX Part I

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?

Page 118: Unix 1

The shells

Bruce Hunter2/21/01 page 118

UNIX Part I

The Shell’s EnvironmentEach user’s shell memorizes its environment. Here’s one displayed with theenv command.

434% envTERM=xtermSHELL=/bin/cshHOME=/usr/system/bhunterLOGNAME=bhunterUSER=bhunterPATH=/usr/ucb:/usr/bin:/bin:/usr/local/binTZ=PST8PDTLOCPATH=/usr/lib/nls/locLANG=En_USEXINIT=map z xpFMHOME=/usr/local/frame/3.0xMANPATH=/usr/local/manEDITOR=viXENVIRONMENT=/usr/system/bhunter/.Xdefaults.aixXINITRC=/usr/system/bhunter/.xinitrc.aixDISPLAY=unix:0.0WINDOWID=12582925TERMCAP=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 PATHWORK=/fes/bhunterexport WORK

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

Page 119: Unix 1

The shells

Bruce Hunter2/21/01 page 119

UNIX Part I

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 theseare typical.

Page 120: Unix 1

The shells

Bruce Hunter2/21/01 page 120

UNIX Part I

/etc/passwd

The shell gets its first bits of information (environmental variables) from thefile /etc/passwd (or the YP version or part of the login process.) Here isone 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.

Page 121: Unix 1

The shells

Bruce Hunter2/21/01 page 121

UNIX Part I

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 arraysof strings). One of them , envp, is a pointer to all the user’s variables. This Cprogram prints each variable to the screen one at a time.

Preset Variables

All you have to do to invoke a presetvariable is mention its name. nogob isan example of a preset. say

noglob

and your shell will not expand variables

Page 122: Unix 1

The shells

Bruce Hunter2/21/01 page 122

UNIX Part I

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

485% a.outTERM=xtermSHELL=/bin/cshHOME=/usr/system/bhunterLOGNAME=bhunterUSER=bhunterPATH=/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=PST8PDTLOCPATH=/usr/lib/nls/locNLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/prime/%NODMDIR=/etc/objreposLANG=En_USEXINIT=map z xpFMHOME=/usr/local/frame/3.0xMANPATH=/usr/local/manEDITOR=viXENVIRONMENT=/usr/system/bhunter/.Xdefaults.aixXINITRC=/usr/system/bhunter/.xinitrc.aixDISPLAY=unix:0.0WINDOWID=12582925TERMCAP=co#80:li#24:

cc, the C compilor, automatically names its outputprogram a.out.

Page 123: Unix 1

The shells

Bruce Hunter2/21/01 page 123

UNIX Part I

Ordinary Shell Variables

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

440% set hi = Hello441% set there = ’ world’442% echo $hi $thereHello world443%

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

443% setargv ()cwd /usr/system/bhunter/introhi Hellohistory 24home /usr/system/bhunterpath /bin /usr/ucb /usr/bin /bin /usr/local/binprompt !%shell /bin/cshstatus 0term xtermthere worlduser bhunter

Use unset to unset a variable

444% unset hi there

Page 124: Unix 1

The shells

Bruce Hunter2/21/01 page 124

UNIX Part I

Setting Variables

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

Variables in sh, bash, pdksh, zsh, and kshSetting a variable in sh looks like a simple assignment

$ a=1234$ b=5678$ c=90$ echo $a$b$c1234567890$ echo $a1234$

Note the is no white space around the assignment operator.

Variables in csh and tcshC shell needs a little more hand holding, via the set command.

467% set a = A468% echo $aA

Note the white space on either side of the =.

Page 125: Unix 1

The shells

Bruce Hunter2/21/01 page 125

UNIX Part I

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 theENV.

TERM=VT100export 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.0xsetenv MANPATH /usr/local/mansetenv EDITOR vi

Page 126: Unix 1

The shells

Bruce Hunter2/21/01 page 126

UNIX Part I

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.

Page 127: Unix 1

The shells

Bruce Hunter2/21/01 page 127

UNIX Part I

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 nodisadvantages., 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 languageinterpreter and interactive login shell It includes a command-line editor, pro-grammable word completion and spelling correction.

Page 128: Unix 1

The shells

Bruce Hunter2/21/01 page 128

UNIX Part I

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 theshell 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 throughthe 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 willbackground it. For example if you had just suspended a foreground job, andit was the only one under job control,

bg %1

will restart it detached from the terminal.

Page 129: Unix 1

The shells

Bruce Hunter2/21/01 page 129

UNIX Part I

Choosing a Shell For Productivity

sh

Old, command compatible, fast, feature poor

csh

Old, slow, fewer features that the newer shells, UNIX command returnsincompatible 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 syntaxand features. Advantages:

command completion

aliases

command-line editing

expansive use of history

typeset for integers and arrays

more built-ins for speed

Page 130: Unix 1

The shells

Bruce Hunter2/21/01 page 130

UNIX Part I

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 inthe passwd file.

Killing a shell

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

Lab

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

Page 131: Unix 1

The shells

Bruce Hunter2/21/01 page 131

UNIX Part I

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-upfiles like

.profile sh ksh

.bashrc bash

.login csh

.cshrc csh

.x* X

.*rc almost anything

Page 132: Unix 1

The shells

Bruce Hunter2/21/01 page 132

UNIX Part I

How many dot files do you have?

487% cd488% 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.bak489% ls .??*|wc -l47490%

Page 133: Unix 1

The shells

Bruce Hunter2/21/01 page 133

UNIX Part I

.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/binMAIL=/usr/spool/mail/$USERexport TERM MAIL PATHbiff n# FMHOME line added by the FrameMaker setup programFMHOME=/usr/system/sframePATH=:.:$FMHOME/bin:$PATHexport FMHOME PATH

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

Page 134: Unix 1

The shells

Bruce Hunter2/21/01 page 134

UNIX Part I

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, GIDand the shell of choice.

Page 135: Unix 1

The shells

Bruce Hunter2/21/01 page 135

UNIX Part I

.login

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

#!/bin/cshset mail = /var/spool/mail/bhunterbiff nstty erase ^Hset prompt = "\!% "set EDITOR = vilimit coredumpsize 0set path = ( /usr/ucb /usr/bin /bin /usr/local/bin

/usr/system/bhunter/bin .)set MANPATH = /usr/local/man

Page 136: Unix 1

The shells

Bruce Hunter2/21/01 page 136

UNIX Part I

.cshrc

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

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

Page 137: Unix 1

The shells

Bruce Hunter2/21/01 page 137

UNIX Part I

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, andthe shell for the file parameter.

Page 138: Unix 1

The shells

Bruce Hunter2/21/01 page 138

UNIX Part I

ksh Files

$HOME/.kshrcIs used to control history.

/usr/bin/ksh Is the executable.

/tmp/sh* Contains temporary files that are created whena shell is opened.

.profileThe set-up file for sh and ksh .

Page 139: Unix 1

The shells

Bruce Hunter2/21/01 page 139

UNIX Part I

bash Files

/bin/bash

~/.bashrc

/etc/profile

~/.profile

When an interactive shell that is not a login shell is started, bash reads andexecutes commands from ~/.bashrc, if that file exists. When invoked as alogin 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

Page 140: Unix 1

The shells

Bruce Hunter2/21/01 page 140

UNIX Part I

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?

Page 141: Unix 1

The shells

Bruce Hunter2/21/01 page 141

UNIX Part I

Shell Command Substitution, QuotingThe single quotes (’...’) are used to preserve white space and preventsh 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 twograve accents (‘‘), the standard output from these commands may be usedto set a variable. For example

nusers=‘who|wc -l‘

No interpretation is done on the string before the string is read, except toremove backslashes. Backslashes may be used to escape a grave accent(‘) or another backslash. If the command substitution lies within a pair ofdouble quotes (" ...‘ ...‘ ... "), a backslash used to escape adouble 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\ndon’t

Page 142: Unix 1

The shells

Bruce Hunter2/21/01 page 142

UNIX Part I

Shell Quotes in a Nutshell

’ ’ preserve white space, prevent metacharacter andvariable interpretation.

"" preserve white space, interpret metacharacters andvariables

‘‘ run the enclosed command, return results

\ preserve the meaning of the next character orgive it a new one.

Examples:

% grep ’now is the time’ text.doc

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

n=‘who | wc -l‘

echo "Name? : \c"

Page 143: Unix 1

The shells

Bruce Hunter2/21/01 page 143

UNIX Part I

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

Page 144: Unix 1

The shells

Bruce Hunter2/21/01 page 144

UNIX Part I

sh Parameter SubstitutionShell variables are dereferenced, that is, the variable is read from its placein storage when proceeded by a $.

echo $HOME

There are two types of parameters, positional and keyword. If parameter isa digit, it is a positional parameter. For example

echo $1

will print the first command-line argument passed to a shell. They are usedin scripts. Positional parameters may be assigned values by set in csh orsimply 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 requiredonly when parameter is followed by a letter, digit, or underscore that is not tobe interpreted as part of its name. It is used to clarify a variable or preventpossible confusion. Here the character b, not the variable b, is concate-nated to the variable a and stored in str.

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

Page 145: Unix 1

The shells

Bruce Hunter2/21/01 page 145

UNIX Part I

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.

$ lsPLAN mail.doc shell.doc.lck$ echo $?0$

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

:(datewhoamittyhostname)>$0.$$

Page 146: Unix 1

The shells

Bruce Hunter2/21/01 page 146

UNIX Part I

The run of the script:

297% chmod +x sh.param298% sh.param299% toptotal 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

Page 147: Unix 1

The shells

Bruce Hunter2/21/01 page 147

UNIX Part I

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

62% cat args#!/bin/shecho -n "PID = "echo $$echo "there are $# arguments"while [ $# != 0 ]

doecho "$1"shift

doneexit 0

The execution

63% args one two threePID = 19994there are 3 argumentsonetwothree64%

Shell Scripts

The program on the lastpage and the one on thispage are called “shellscripts.” They are written inan editor and executed bysimply giving their name tothe shell.

Page 148: Unix 1

The shells

Bruce Hunter2/21/01 page 148

UNIX Part I

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.

Page 149: Unix 1

The shells

Bruce Hunter2/21/01 page 149

UNIX Part I

sh Blank Interpretation

After parameter and command substitution, the results of substitution arescanned for internal field separator characters and split into distinct argu-ments. String literals enclose in quotes are taken as one string. This istokenizing.

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 takenow is the time and pass it to grep as an RE so grep will scan the filefoo.file for it.

Page 150: Unix 1

The shells

Bruce Hunter2/21/01 page 150

UNIX Part I

The Data Streams

UNIX can redirect the data stream. Every UNIX process has three openstream 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.

Page 151: Unix 1

The shells

Bruce Hunter2/21/01 page 151

UNIX Part I

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 beassociated 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.

Page 152: Unix 1

The shells

Bruce Hunter2/21/01 page 152

UNIX Part I

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 2with the file associated with file descriptor 1 (namely, file bar). If the order ofredirections are reversed, file descriptor 2 is associated with the terminal(assuming file descriptor 1 had been) and file descriptor 1 is associated withfile 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 1993122333444455555666666777777788888888999999999endcat: 0652-050 Cannot open bar.$

Page 153: Unix 1

The shells

Bruce Hunter2/21/01 page 153

UNIX Part I

C Shell STDERR Redirection

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

558% cat foo bar >& error559% cat error

Wed Nov 10 14:55:06 PST 1993122333444455555666666777777788888888999999999endcat: 0652-050 Cannot open bar.

Page 154: Unix 1

The shells

Bruce Hunter2/21/01 page 154

UNIX Part I

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.fDateTue Dec 20 14:01:20 PST 1994

Now let’s redirect in the other direction:

554% sed ’s/Date/date:/’<date.fdateTue Dec 20 14:01:20 PST 1994

A classic:

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

Page 155: Unix 1

The shells

Bruce Hunter2/21/01 page 155

UNIX Part I

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 1993122333444455555666666777777788888888999999999end

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

Page 156: Unix 1

The shells

Bruce Hunter2/21/01 page 156

UNIX Part I

sh Special Commands / Metacharacters

: Has no effect, the command does nothing, but a zero exitcode is returned. This is sometimes used to start Bourneshell scripts.

. filename Read and execute commands from filename andexecutes in the current shell. It is like source in csh.

Page 157: Unix 1

The shells

Bruce Hunter2/21/01 page 157

UNIX Part I

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,

Page 158: Unix 1

The shells

Bruce Hunter2/21/01 page 158

UNIX Part I

Here is a shell script with the x flag set.

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

Now it runs with debugging on.

27% !$myscript+ echo Name :Name :+ read nameArnold+ pwddir=/usr/system/bhunter/junk+ echo Arnold, you are in /usr/system/bhunter/junkArnold, you are in /usr/system/bhunter/junk+ exit 028%

Now with no x flag.

30% myscriptName :RamboRambo, you are in /usr/system/bhunter/junk31%

Page 159: Unix 1

The shells

Bruce Hunter2/21/01 page 159

UNIX Part I

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 theforeground can be suspended with a ^Z.1

118% du />/tmp/du.udg^ZSuspended

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

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

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

Page 160: Unix 1

The shells

Bruce Hunter2/21/01 page 160

UNIX Part I

Example, Job Control in zshThe program:

#!/usr/intel/bin/zsh# name: long_jobn=0while [ $n -ne 10 ]do

echo $n(( n += 1))sleep 10

done

Execution in zsh with job control.

% long_job012^Zzsh: done long_job% jobs[1] + done long_job% fg %1[1] + continued long_job3456789%

Page 161: Unix 1

The shells

Bruce Hunter2/21/01 page 161

UNIX Part I

You will probably want to protect a job in background from being accidentlykilled 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 commandkill. Use the PID number

% kill 1135

or the csh job number.

% kill %1

Page 162: Unix 1

The shells

Bruce Hunter2/21/01 page 162

UNIX Part I

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. Whatare your options?

Page 163: Unix 1

The shells

Bruce Hunter2/21/01 page 163

UNIX Part I

csh Built-In CommandsAll built-in commands are executed within the C shell. If a built-in commandoccurs as any component of a pipeline except the last, it is executed in asubshell. Most of these you have seen before.

alias name defAssigns def to the alias name. Examples are:

alias h historyalias l lookalias 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 theresulting command(s). This is usually used to executecommands generated as the result of command or variablesubstitution, since parsing occurs before these substitutions.

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

Page 164: Unix 1

The shells

Bruce Hunter2/21/01 page 164

UNIX Part I

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

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

-h Display the history list without leading numbers. This is usedto produce files suitable for sourcing using the -h option tosource.

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

-lLists process IDs, in addition to the normal information.

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

Sends a signal (default signal 15 to the job identified bythe PID of job number.

-l List the signal names that can be sent.

nice [ +n|-n ] commandSets a jobs priority by incrementing the process priority value forthe shell or for command by n. The higher the priority value, thelower the priority of a process, and the slower it runs. If noincrement is specified, nice sets the nice value to 4.The range of nice values is from -20 through 19.

nohup commandRuns command with the hangup signal ignored.

Page 165: Unix 1

The shells

Bruce Hunter2/21/01 page 165

UNIX Part I

set var = valueWith no arguments, set displays the values of all shell variables.With the var argument alone, set assigns an empty (null) value tothe 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 wordWith no arguments, setenv displays all environment variables.With the VAR argument it sets the environment variable VAR tohave 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 nameReads commands from the file name and executes it in thecurrent shell.

source .login

unset patternremoves variables whose names match pattern.

SourceThe sh equivalent to source is dot. Just place a dot in front ofyour command and it executes in the current (Bourne) shell.

$ . .profile

Page 166: Unix 1

The shells

Bruce Hunter2/21/01 page 166

UNIX Part I

Preset VariablesSome shell variables have predefined meanings although they are not partof the ENV:

argv Is the argument list. It contains the list of command linearguments 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.

ignoreeofWhen set ignores EOF from terminals. This protects againstaccidentally killing a C shell by typing a ^D.

noclobberRestricts output redirection so that existing files are notdestroyed by accident. > redirections can only be made tonew files.

noglob Stops filename metacharacter substitution.

notify If set notifies you immediately as jobs are completed, rather thanwaiting 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.

Page 167: Unix 1

The shells

Bruce Hunter2/21/01 page 167

UNIX Part I

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 anotherutility such as & which is used by mail or < or > which are used by severalother utilities.

Page 168: Unix 1

The shells

Bruce Hunter2/21/01 page 168

UNIX Part I

Using the Secondary Prompt

Many useful commands and constructs cannot complete on one line. Assuch C shell will reject them. The other shells however can handle them justfine 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> dofor> ls -ld $ffor> file $ffor> done-rw-r----- 1 bhunter users 97 Jul 14 09:56:shell script-rwxr-x--- 1 bhunter system 310 Nov 11 1998 class.plclass.pl: shell script-rwxr-x--- 1 bhunter users 465 Jul 09 15:42 concreate...

Page 169: Unix 1

The shells

Bruce Hunter2/21/01 page 169

UNIX Part I

Which Shell Am I In?

If you cannot remember which shell you are in any particular window checkthe 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 bhunterbhunter 13580 140800 0 Jun 30 pts/62 0:00 -cshbhunter 32550 123686 1 Jul 09 pts/11 0:01 bashbhunter 40506 13580 0 15:23:39 pts/62 0:00 zshbhunter 60758 62472 0 Jul 08 pts/51 0:01 bashbhunter 62472 80380 0 Jun 30 pts/51 0:00 -cshbhunter 72206 92678 0 Jun 30 pts/9 0:00 bashbhunter 92678 85498 0 Jun 30 pts/9 0:00 -cshbhunter 123686 18980 0 Jun 30 pts/11 0:00 -sh

It is also helpful to look at $0.

$ echo $0bash

Page 170: Unix 1

The shells

Bruce Hunter2/21/01 page 170

UNIX Part I

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 correctionsand 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 thehistory variable. The most recent command is retained in any case. Ahistory substitution begins with a !

544% history529 man ksh530 pwd531 vi sh.man532 top533 pwd534 cd intro535 top536 vi sh.man537 vi csh.man538 sh539 vi ~/.cshrc540 echo *541 glob *542 ls543 grep prompt ~/.??*544 history

545%

Page 171: Unix 1

The shells

Bruce Hunter2/21/01 page 171

UNIX Part I

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!

Page 172: Unix 1

The shells

Bruce Hunter2/21/01 page 172

UNIX Part I

Shell History-Event Designators

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

! 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.

Page 173: Unix 1

The shells

Bruce Hunter2/21/01 page 173

UNIX Part I

Word DesignatorsYou can retrieve parts of the last command line with word designators. Herethe 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 toinvocation environment"stdio.h", line 204 ("llib-lc"): warning: function getsreturn value is always ignored"stdio.h", line 205 ("llib-lc"): warning: function putsreturn value is always ignored68% 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 inthe event.

Page 174: Unix 1

The shells

Bruce Hunter2/21/01 page 174

UNIX Part I

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% history59 args60 chmod +x args61 args62 cat args63 args one two three64 cd65 cd c.c66 ls67 lint io.c68 cc io.c69 rm .aout70 rm a.out71 h

72% !60:pchmod +x args73%

You can edit the last command line with

^string^replacement

Taking the ability to print, you can combine it and

79% !60:pchmod +x args80% ^args^hostschmod +x hosts81%

Page 175: Unix 1

The shells

Bruce Hunter2/21/01 page 175

UNIX Part I

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 inthe 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.

Page 176: Unix 1

The shells

Bruce Hunter2/21/01 page 176

UNIX Part I

AliasesShells maintains a list of aliases that you can create, display, and modifyusing the alias and unalias commands. You already saw alias back afew pages. Here it is again in these lines from .cshrc

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

Aliases can be removed with unalias. Aliases are a help in cutting downon typing repetitive commands, but they can be overdone. With too manyaliases your csh will get slower than it already is. Minimize aliases. Takeadvantage of having your own ~/bin and put small scripts there to do youreveryday 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 thesyntax.

dir() {ls -al | more

}

Another example

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

}

Page 177: Unix 1

The shells

Bruce Hunter2/21/01 page 177

UNIX Part I

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 killcsh.

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’"

Page 178: Unix 1

The shells

Bruce Hunter2/21/01 page 178

UNIX Part I

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 orbetween shell execed processes that aredescendents of a common parent that initi-ated the pipeline.

Page 179: Unix 1

The shells

Bruce Hunter2/21/01 page 179

UNIX Part I

ksh Features Different from csh

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

HISTSIZE=10export HISTSIZE

To set the ksh prompt to your current history number assign it to theprompt 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:*

Page 180: Unix 1

The shells

Bruce Hunter2/21/01 page 180

UNIX Part I

The Number-One Productivity Tool, Scripts

Nothing boosts user productivity more then task automation through the useof scripts. The shells are not alone as scripting languages. There is alsoPERL 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

Page 181: Unix 1

The shells

Bruce Hunter2/21/01 page 181

UNIX Part I

The Shells As Programming Languages

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

104% cat rmail#!/bin/shrm /var/spool/mail/bhunter

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

#!/bin/shfor dir in ‘ypcat passwd|awk -F: ’$3>99 { print $6 }’‘

doecho $dirdu $dir|sed -n ’$p’

doneexit# udu# shows each users total disk usage

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

97% cat cleanx#!/bin/cshwhile (sleep 10)

rm /tmp/x[0-9]*.crm /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

Page 182: Unix 1

The shells

Bruce Hunter2/21/01 page 182

UNIX Part I

Shell Scalar VariablesOrdinary variables, as opposd to presets or environmenatl variables, arecreated on the fly.

#!/bin/sha=3b=4c=‘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/kshtypeset -i i=0typeset -i lentypeset -u names

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

names[$i]=$name((i = $i + 1))

donelen=$i((i = 0))

while [ $i -lt $len ]do

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

done

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

Page 183: Unix 1

The shells

Bruce Hunter2/21/01 page 183

UNIX Part I

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 nameecho "Hi $name"

Lab

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

name? : McLanehi McLane

Don’t forget to make it executable.

Page 184: Unix 1

The shells

Bruce Hunter2/21/01 page 184

UNIX Part I

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/shecho $1

and then execute it

%myprog barbar

The positional parameter $1 is the first argument, and here it is echoed. Avariable $# stores how many arguments there are. It is like argc in C.

If you write

#!/bin/shecho "you have $# arguments"

you get the results

194% argc 1you have 1 arguments

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

Page 185: Unix 1

The shells

Bruce Hunter2/21/01 page 185

UNIX Part I

Lab

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

bash-2.00$ name_lab John RamboName: Rambo, John

Page 186: Unix 1

The shells

Bruce Hunter2/21/01 page 186

UNIX Part I

A Lab Answer

#!/bin/shecho "Name: $2, $1"

Page 187: Unix 1

The shells

Bruce Hunter2/21/01 page 187

UNIX Part I

Comments

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

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

Permissions for Scripts

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

% chmod +x myprog

or

% chmod 755 myprog

Page 188: Unix 1

The shells

Bruce Hunter2/21/01 page 188

UNIX Part I

exit

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

#!/bin/cshexit 0# comments

The return code is traditionally

• 0 OK

• 1 through N traceable error

• -1 really bad error

Page 189: Unix 1

The shells

Bruce Hunter2/21/01 page 189

UNIX Part I

Decisions, the if

Programs branch on decisions.

echo "exit y/n? : "read aif [ "$a" = "y" ]then

exit 0;else

continuefi

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

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

continueelse

echo "File locked"exit -1

fi

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

if test -f lock.filethen

echo "File locked"exit -1

elsecontinue

fi

Page 190: Unix 1

The shells

Bruce Hunter2/21/01 page 190

UNIX Part I

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

#!/bin/kshtypeset -i i=0typeset -i lentypeset -u names

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

names[$i]=$name((i = $i + 1))

donelen=$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.

Page 191: Unix 1

The shells

Bruce Hunter2/21/01 page 191

UNIX Part I

test

The if and while most frequently use the test command to supply theboolean 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 adirectory you have permission to search in the directory.

Page 192: Unix 1

The shells

Bruce Hunter2/21/01 page 192

UNIX Part I

-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

Page 193: Unix 1

The shells

Bruce Hunter2/21/01 page 193

UNIX Part I

Lab

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

Page 194: Unix 1

The shells

Bruce Hunter2/21/01 page 194

UNIX Part I

A Lab Answer

#!/bin/shif [ $# -ne 2 ]then

echo "use name_lab first_name last_name"exit 1

fiecho "Name: $2, $1"

Page 195: Unix 1

The shells

Bruce Hunter2/21/01 page 195

UNIX Part I

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. Theword switch is not used but is pattern matched on the argument(s) closed onthe 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 loadelse

echo no loadfi;;

esac

Page 196: Unix 1

The shells

Bruce Hunter2/21/01 page 196

UNIX Part I

Looping, The while

The while will loop on a true condition,

while /bin/truedo

echo -n "$n "n=‘expr $n + 1‘

done

or a command results

while sleep 60do

ps | grep -v grep | grep -s myprogif $?then

or a test

n = 1while [ $n -ne 11 ]do

echo -n "$n "n=‘expr $n + 1‘

done

Page 197: Unix 1

The shells

Bruce Hunter2/21/01 page 197

UNIX Part I

Lab

Write a script to do a countdown

bash-2.00$ !cd.lab10987654321

Page 198: Unix 1

The shells

Bruce Hunter2/21/01 page 198

UNIX Part I

Lab Answers

#!/bin/shi=10while [ $i -ne 0 ]do

echo $ii=‘expr $i - 1‘

doneecho bang!

#!/usr/intel/bin/zshtypeset -i i=10while [ $i -ne 0 ]do

echo $i((i = $i - 1))

doneecho bang!

Page 199: Unix 1

The shells

Bruce Hunter2/21/01 page 199

UNIX Part I

Looping, the for

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

for n in 1 3 5 7 9do

echo $ndone

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

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

echo $namedone

Page 200: Unix 1

The shells

Bruce Hunter2/21/01 page 200

UNIX Part I

break and continue

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

#!/bin/sh

while ‘/bin/true‘do

echo "continue y/n: \c"read acase $a in

y)continue;;

n)break;;

*)echo "can’t type?, try again"continue;;

esacdoneexit 0

Page 201: Unix 1

The shells

Bruce Hunter2/21/01 page 201

UNIX Part I

Lab

Create a file foo with the contents

1223334444555556666667777777888888899999999900000000000

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

Page 202: Unix 1

The shells

Bruce Hunter2/21/01 page 202

UNIX Part I

A Lab Answer

#!/bin/shi=1for line in ‘cat foo‘do

i=‘expr $i + 1‘# 1234567890

if [ "$line" = "0000000000" ]then

breakfiecho "$i $line"

done

Page 203: Unix 1

The shells

Bruce Hunter2/21/01 page 203

UNIX Part I

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 aslong as the are in double sets of parentheses.

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

while [ $i -lt 3 ]do

echo "length in feet: \c"read lenecho "width in inches: \c"read wecho "heigth in inches: \c"read hecho "$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"

Page 204: Unix 1

The shells

Bruce Hunter2/21/01 page 204

UNIX Part I

zsh Math

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

#!/usr/intel/bin/zsh

echo "repeat test"n=10m=1repeat $ndo

echo $m number :: $n((m += 1))

done

Page 205: Unix 1

The shells

Bruce Hunter2/21/01 page 205

UNIX Part I

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.

Page 206: Unix 1

The shells

Bruce Hunter2/21/01 page 206

UNIX Part I

Command Grouping

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

#!/bin/shecho date > f.filedate >> f.fileecho users >> f.filewho>> f.file

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

#!/bin/sh(echo datedateecho userswho)> 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.

Page 207: Unix 1

The shells

Bruce Hunter2/21/01 page 207

UNIX Part I

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 theywere typed in from the keyboard. The operator used is <<. It is followed by astring that will terminate the input.

#!/bin/shed profile<<END/biff y/s//biff n/

wqEND

It works in ksh and csh.

#!/bin/cshed profile<<END/biff y/s//biff n/wqEND

Page 208: Unix 1

The shells

Bruce Hunter2/21/01 page 208

UNIX Part I

sh Named FunctionsHere 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 astrue functions.

#!/bin/shgeta () {while [ 1 ]

doecho -n "enter yes or no : "read ansansw=‘echo $ans|cut -c 1‘

case $answ iny|Y)

return;;

n|N)echo "exiting"exit 1

;;*)

echo "enter y or n";;

esacdone}echo -n "do you want to continue : "getaecho "script body"exit 0

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

Page 209: Unix 1

The shells

Bruce Hunter2/21/01 page 209

UNIX Part I

Good Books

UNIX Shell Programming, Arthur & Burns

UNIX C Shell Field Guide, Anderson & Anderson

The Korn Shell, Bolsky & Korn

Page 210: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 210

vi

Editing andRegular Expressions

UNIX Part 5Rev 6/3/99ECT001118

With a little sed and vim

Page 211: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 211

Agenda

First we will do a crash course on vi to get the look and feel of it. You willthen study the ed editor since its commands are the basis for vi. Then youwill 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 advancetechniques will complete the course.

Page 212: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 212

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

Page 213: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 213

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. Retypingthe same or similar text is a waste of time. Visually searching text is a wasteof 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 theserepeated operations. The advantages of vi over a simple editor like xeditor 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

Page 214: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 214

A vi Crash Course

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

There are many other editors onUNIX. From X Windows you canuse

xedit

and in or out of X

ed

ex

vi and vim

emacs

pico

Page 215: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 215

Preparing to use vi

The UNIX default editor is vi. It is easy to use and does a lot. It runs in threemodes. When you are entering text, it is in append mode. To leave appendmode, you type an escape. The editor then will be in vi mode where themajority of letter keys have a special meaning. For example, the x removesa letter, while the a puts you back in append mode. The third mode is ex ored mode, named after ed, the primitive editor. By entering a slash (/), yougo to the bottom line to execute a search. By entering a colon, you go to thebottom line to enter a command. Typical commands are w for write, q forquit, 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

Page 216: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 216

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 beat the ooo in mistoook. Type in x and one o will be removed. Type in cw tochange 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) andthen 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.

Page 217: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 217

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 beon the bottom. Hit / again then enter. You are back at ing. Type $ and youare 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 goto 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 onecharacter 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?

Page 218: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 218

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

Page 219: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 219

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 Hcommand. Let’s put the system date on the first line. Put a blank line abovethe top line. Type in O in vi command mode. Now hit escape. Go to thecommand line. Type

:r !date

Fri Nov 18 14:23:57 PST 1994I 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 linethat begins with its own end — a blank line. Now remove it with a dd. Go tothe end of the file with a

:$

Add

This text appendedfor exercise 3.

Page 220: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 220

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 tothe end of file ($) put a tab at the beginning of the line." Now get all the linenumbers with this command-line expression.

:set number

1 Mon Nov 21 09:52:17 PST 19942 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 appended7 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 19942 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.

Page 221: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 221

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 1994I 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 theline." Note the backslash escape for the dot. Now find the lines that don’tend with a dot.

/[^.]$

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

Page 222: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 222

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 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

^U move the screen up a half page

^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

H J K L

ControlCharacters

Page 223: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 223

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 Ncharacters of the end of line

flash set a visible bell (no beep)

hardtabs= set tabs to

showmode show when in input mode

Page 224: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 224

Learn ed?

UNIX system administators should know ed. If the system won’t come upinto 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 itscommand mode is in ex, therefore ed mode. It is important that you knowed for its own sake and as parent to all the other commands.

ed

grep

egrep

ex

vi

sed

awkperl

vim

Page 225: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 225

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 youneed to know about ed applies to ex and vi.

The Creation of viBSD architect Bill Joy1 created vi, when he was at Berkeley. He knew thata 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, onethat you could see what it is that you are working on. Up until this time avisual editor could only be on fixed proprietary systems where the terminalswere all of one kind, such as DEC VT-100 terminals. UNIX is created to useany kind of a ASCII terminal.

/etc/termcap

To create a visual editor, first a system had to be created where each usersession could place the terminal characteristics of the users’ terminalswithin each user’s environment. To do this, Bill Joy created termcap. Hereis 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

Page 226: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 226

Pattern MatchingPattern matching is used to locate lines in text and set up operations like thesearch 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 addressesto 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 samesequence 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).

Page 227: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 227

No one single skill, in UNIX, will be as important to you as the skillful use ofregular expressions. It is the keystone for productivity.

Regular ExpressionsRe

gula

r Exp

ress

ions

Regular ExpressionsReg

ular

Expr

essi

ons

Regular ExpressionsRegu

lar E

xpre

ssio

nsR

egular ExpressionsRegu

lar E

xpre

ssio

ns

REs

Page 228: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 228

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 cin 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 * means0-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 searchesor addresses. The next is a substitution.

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

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

Page 229: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 229

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 \{\} \[\]\(\) \& \1some have to be escaped for quantifiers,or tags and brackets need to be escapedto 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

Page 230: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 230

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 itwere not enclosed.

Tag NumbersTagged expressions have tag numbers. In the example above foo is \1and 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. Thepattern \number (for example \2) matches the string matched by theoccurrence of the subpattern specified by number, counting from left to right.

The expression

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

replaces foo bar or fooeybar with foobar.

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

tags

tag numbers

Page 231: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 231

Addressing LinesThere are three types of addresses in the ed editor.

Line Number AddressesLine 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 forconstructing 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 byx, by the k subcommand (ed mode) or m subcommand in vi mode.

Page 232: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 232

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 foo691,$p1223334444555556666667777777888888889999999990000000000end/33333+,$-1p4444555556666667777777

Page 233: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 233

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

17,19

is an acceptable adress range but so is

.,.+2

or

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

16 Description17 The true command returns a zero exit value. The false command18 returns a nonzero exit value. These commands are most often19 used as part of a shell script.2021 Examples2223 To construct a loop that displays the date and time once each24 minute, use the following code in a shell script:2526 while true27 do28 date29 sleep 6030 done3132 Implementation Specifics

...36 Related Information3738 How to Create and Run a Shell Script in AIX Version 3.2 System39 User's Guide: Base and Devices provides information on creating40 and executing shell procedures.

Page 234: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 234

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 $

Page 235: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 235

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/

Page 236: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 236

Here is a little PERL program to do comma-after-three-zero notation. Seehow simple the REs are!

#!/usr/local/bin/perl5while(<DATA>) {

s/0000$/0,000/;while(/0000/) {

s/0000,/0,000,/}print

}__END__30000000100000000000100000000000000000

The results

1009% big_no.pl30,000,000100,000,000,000100,000,000,000,000,000

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

Page 237: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 237

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 alab 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.

Page 238: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 238

Review; Modes in Vi

Like ed, vi runs in command mode and in append or insert mode, but it hastwo 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 vimode done by issuing short mnemonic commands (subcommands) from thescreen, the text itself.

An example of the first, or ex mode, is writing the file. In command modeyou strike the colon (:) character and go to the bottom line. There you enterthe ed command w.

:w

The bottom line will then tell you what you wrote.

vi Mnemonicsvi 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 themplain, shifted, or as control characters. The numbers are used as repeatcharacters.

Using vi For Reading FilesEditors are regularly used to read files. While you can use cat, type, ormore to see a file, only the editor will move you by lines, do complete jumpsin either direction and search for specific strings or expressions. Usevi -R to invoke a read-only editor.

Moving Through TextOnce you have read the text initially on the screen you will want to movethrough the text. To move a full screen down use ^F (forward). To move upor 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 withthe ed / command from the bottom (25th) line. such as /vt100

Page 239: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 239

More MovementsH 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 TextTo really understand cursor movment commands, you have to look at theqwerty 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 witha small movement of the index finger. The leftmost key, h, moves the thecursor down, its left neighbor, k, moves the cursor up.

That it! Almost all the other keys have mnemonic values like u up, d down,etc. For trickier movements use:

h j k l

Page 240: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 240

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.

RepeatsLet’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.

DeletionsOops!, you made a mistake! We all do, and that is why vi has so manycommands 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.

ChangesSometimes you delete messes; other times you change them.

cw Changes the word from the cursor position to the last character inthe 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.

Page 241: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 241

Adding a LineTo 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 arealready in insert mode.

Append to the End of the Current LineYou are at the beginning or the middle of a line, and you want to add to theend of it. Type A (not a) and you are at the end and in append mode.

Going to the End of the LineYou are at the beginning or the middle of a line, and you want to position thecursor at the last character on the line. Type $.

Page 242: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 242

Going to the Beginning of the LineYou 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 CommandA U, as an ex command, will undo the last edits and restore what was therefirst.

Repeating a CommandThe last command will be repeated if you type a dot (This will happen if youwant 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 Putsvi buffers its moves. When you delete a character, word, words, or a linethat 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 somewhereelse.

Say that you misstype the and get teh. Now put the cursor over the e andhit x, and the e is gone. Without moving the cursor type in p, and the e isback but now after the h .

teh

to

the

with just two keystrokes. Later you’ll see how to do it with one by mapping akey (like z) to the x and p commands.

Page 243: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 243

Try to swap lines. Do a dd on a line and follow it with a p. Start with

1223334444555556666667777777888888889999999990000000000end

With the cursor on 999999999, strike dd, then p.

1223334444555556666667777777888888880000000000999999999end

Now go back to 0000000000 and repeat your performance.

1223334444555556666667777777888888889999999990000000000

Page 244: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 244

y is for yankYou 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 torepeat a line? Put the cursor at the end of the foo file and do a yy followedby a p.

1223334444555556666667777777888888889999999990000000000endend

Here’s one more exercise with named buffers. Put the cursor on one of theends 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

Page 245: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 245

Joining LinesFrequently you need to join lines. Put the cursor anywhere on the line to bejoined and hit J. Try it with foo on the next to last end.

end1223334444555556666667777777888888889999999990000000000end end

Accidently Joining LinesYou reach out for the a key and touch caps lock. Oops! Now you try to moveyour 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 dozenlines into one.

Finding a Letter on a LineThe 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.

Page 246: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 246

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.

Page 247: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 247

Those are the vi commands. Now the ex commands.

ex and ed Editor Commands in viIn vi ed and ex command are typed from the bottom or “25th line.” Manyex 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 exampleRemoves 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 firstline to the last on the ex command line and get

end1223334444555556666667777777888888889999999990000000000end end

Page 248: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 248

Now one final deletion

:$-1d

and

1223334444555556666667777777888888889999999990000000000end

Page 249: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 249

Copying Text

Now the tree is OK; let’s mess it up again and copy all of it to its end.

:1,$copy $12233344445555566666677777778888888899999999900000000001223334444555556666667777777888888889999999990000000000

You could have done the same thing with

1,$co $

or

1,$t $

Page 250: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 250

Addresses and Pattern MatchesAddresses 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 andtype

:/^1$/

which is a pattern that becomes the address. Now let’s add begin to the topof the file.

begin1223334444555556666667777777888888889999999990000000000end

and write it to a file by pattern matching the addresses.

:/begin/,/end/w snarg

Page 251: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 251

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 thecurrent cursor position.

:r !ls

Writing a Partial File From viIf 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 TextAnything 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

Page 252: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 252

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 modeor a U from ex mode.

U/u

Page 253: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 253

Quitting viThe command

:q

will quite vi unless you have made changes. If so, and you are seriousabout quitting without saving, do this:

:q!

Getting the Current Line NumberTo 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%-- .

Page 254: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 254

Customizing vi

Like all things UNIX, you can customize vi to your preferences. You canset characteristics

: set redraw term=xterm wrapmargin=10

To see all the settings use set all

:set allnoautoindent nomodeline noslowopenautoprint nonumber nosourceanynoautowrite nonovice tabstop=8nobeautify nooptimize taglength=0closepunct=’".,;)]} tags=tags /usr/lib/tagsdirectory=/var/tmp partialcharacter=- term=xtermnoedcompatible prompt notersenoerrorbells noreadonly timeoutnoexrc redraw ttytype=xtermflash remap warnhardtabs=8 report=5 window=40noignorecase scroll=20 wrapscanlinelimit=1048560 sections=NHSHH HUuhsh+c wrapmargin=0nolisp shell=/bin/csh nowriteanynolist shiftwidth=8 wraptype=wordmagic noshowmatchmesg showmode

You can set editor characteristics in your .cshrc, .login, or .pro-file. Execute the env command to see them.

Page 255: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 255

MappingYou can map your keys to do special vi functions. The key z or Z isn’t usedfor much so, let’s use that one. This will have the z key become a mnemonicto reorder reversed characters.

map z xp

Entering Shell Commands

The following subcommands allow you to run a UNIX command within thevi editor. Enter these subcommands in command mode.

:shEnters 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.

:!commandRuns the specified UNIX command and then returns to the editor.

Page 256: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 256

Special Tricks

Seeing the invisible — set listIs 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.

122333444455555666666777777788888888999999999end

Now see it with set list.

^I1$^I22$^I333$^I4444$^I55555$^I666666$^I7777777$^I88888888$

Page 257: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 257

Buffers

The last 9 deletions are stored in numbered buffers. To retrieve any of thesebuffers 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

end999999999888888887777777666666555554444333221

You can see that buffer 1 was the first, buffer, 2 the second and so. Nowbuffer 1 holds the oldest deletion, and buffer 9 the latest.

Page 258: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 258

Saves and Retreives

To simply do a save ( if you expect a power glitch, the server, or the net toburp) do a

:w

If you do get hit with a crash a copy of your file is saved by UNIX and can beretrieved by invoking vi with the r flag and file name.

% vi -r filename

Page 259: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 259

Formatting Text in Place

It’s a good idea to enter only one sentence to the line. Place a newline afterevery period. It makes way for further editing.

It is line oriented and will not display your textunless 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 thedocument as youwork on it.Just about everything you need to know about ed appliesto ex and vi.To invoke ed you type in ed followed by the file name.

$ed foo

Page 260: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 260

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/ortroff. Today you use marker-language formatters like FrameMaker andimport the files. You can still, however, use nroff to format the documentand 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 textunless youspecifically 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

Page 261: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 261

Now see your text

It is line oriented and will not display your textunless 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 exare line editors, you can not see the document as youwork on it.

Just about everything you need to know about ed appilesto ex and vi. To invoke ed you type in ed followed by thefile name.

$ed foo

It is right justified and “filled.” The !G had us escape to shell to executenroff but the G let you do it to all (Global) the text — in place.

Page 262: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 262

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 vito 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.

Page 263: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 263

Customization

EXINIT

You can semi permanently set vi with the EXINIT environmental variablein your .cshrc, .login, or .profile. Then you will have the vi settingsyou 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/bhunter44% cat .exrcset wrapmargin=10set tabstop=6map z xpab SA System Administration45%

Page 264: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 264

vim, Improved vi

vim has lots of enhancements over vi like multi level undo, multi windowsand buffers, command line editing, filename completion, on-line help, visualselection, automatic man pages for UNIX commands, and so on.

What more could vi do? How about if you could use the mouse to help youedit? 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 functionswork:

c change the marked text

x delete the marked text

y yank the marked text

Page 265: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 265

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 ofteh 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 aboutsearch for and then remove everything on one line.

sed ’/AMD/d’ venddor_list.dat

Or make additions automatically

sed ’/Intel/ \avendor of choice’ venddor_list.dat

There is a whole lot more but that will have to wait for UNIX Part II.

Page 266: Unix 1

The vi Editor

Bruce Hunter 2/21/01 page 266

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

Page 267: Unix 1

Bruce Hunter 2/21/01 page 267

UNIX Devices: Terminals, Printers,...

UNIX part I

Devices

UNIX Part I, section 7Rev 7/13/99ECT001118

Page 268: Unix 1

Bruce Hunter 2/21/01 page 268

UNIX Devices: Terminals, Printers,...

UNIX part I

UNIX reaches all of its physical devices through device drivers built into thekernel. The entry point to devices is found in the directory /dev. Let’s look ata terminal.

216% ls -l /dev/tty*crw-rw-rw- 1 root system 1, 0 Feb 18 14:24 /dev/ttycrw-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’sbecause it’s not a regular file; it’s a special file.

A disk would look a little different.

brw------- 1 root system 15 0 Dec 14 1992 /dev/hdisk0brw------- 1 root system 15,1 Dec 14 1992 /dev/hdisk1

These are block special files. More about them later.

crw-rw-rw 1 root system 1, 0

character special file

major device number

minor device number

link count

Page 269: Unix 1

Bruce Hunter 2/21/01 page 269

UNIX Devices: Terminals, Printers,...

UNIX part I

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/fdbrw------- 1 root root 2, 0 May 5 1998 fd0brw------- 1 root root 2, 12 May 5 1998 fd0D360brw------- 1 root root 2, 16 May 5 1998 fd0D720brw------- 1 root root 2, 28 May 5 1998 fd0H1440brw------- 1 root root 2, 12 May 5 1998 fd0H360brw------- 1 root root 2, 16 May 5 1998 fd0H720brw------- 1 root root 2, 4 May 5 1998 fd0d360brw------- 1 root root 2, 8 May 5 1998 fd0h1200brw------- 1 root root 2, 20 May 5 1998 fd0h360brw------- 1 root root 2, 24 May 5 1998 fd0h720brw------- 1 root root 2, 1 May 5 1998 fd1brw------- 1 root root 2, 13 May 5 1998 fd1D360brw------- 1 root root 2, 17 May 5 1998 fd1D720brw------- 1 root root 2, 29 May 5 1998 fd1H1440brw------- 1 root root 2, 13 May 5 1998 fd1H360brw------- 1 root root 2, 17 May 5 1998 fd1H720brw------- 1 root root 2, 5 May 5 1998 fd1d360brw------- 1 root root 2, 9 May 5 1998 fd1h1200brw------- 1 root root 2, 21 May 5 1998 fd1h360brw------- 1 root root 2, 25 May 5 1998 fd1h720

Note the major number 2. They all have the same device driver. The minornumbers and names reflect three different disk densities.

Page 270: Unix 1

Bruce Hunter 2/21/01 page 270

UNIX Devices: Terminals, Printers,...

UNIX part I

Your Terminal

If you start at a bare screen, you are on a tty. If you are in a window youare on a pseudo terminal.

218% ls -l /dev/*tty*crw-rw-rw- 1 root system 1, 0 Feb 18 14:24 /dev/ttycrw-rw-rw- 1 root system 16, 0 Oct 21 13:42 /dev/tty0lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp0 -> /dev/pts/0lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp1 -> /dev/pts/1lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp2 -> /dev/pts/2lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp3 -> /dev/pts/3lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp4 -> /dev/pts/4lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp5 -> /dev/pts/5lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp6 -> /dev/pts/6lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp7 -> /dev/pts/7lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp8 -> /dev/pts/8lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp9 -> /dev/pts/9lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypa -> /dev/pts/10lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypb -> /dev/pts/11lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypc -> /dev/pts/12lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypd -> /dev/pts/13lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttype -> /dev/pts/14lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypf -> /dev/pts/15219%

It’s not real; it’s virtual. Its driver is a pseudo driver, which does all its work insoftware.

What termianl are you on? Try tty.

% tty/dev/pts/9

Page 271: Unix 1

Bruce Hunter 2/21/01 page 271

UNIX Devices: Terminals, Printers,...

UNIX part I

stty

Terminal’s hardware and termio characteristics are set with stty. To seeyour terminal’s setting use

223% stty -aspeed 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 -imaxbelisig icanon -xcase echo echoe echok -echonl -noflsh-tostop echoctl -echoprt echoke -flusho -pending iextenopost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel224%

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

Page 272: Unix 1

Bruce Hunter 2/21/01 page 272

UNIX Devices: Terminals, Printers,...

UNIX part I

Terminal Type, TERM, termcap and tset

Your UNIX environment knows its terminal type because you set it when youlogged on. It should have been in one of your dot files.

tset -n -Iexport 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 drivermodes should be initialized for this terminal while

-I

suppresses transmission of terminal initialization strings. Your environmen-tal variable TERM may later be reset.

229% envTERM=xtermSHELL=/bin/csh

with lines like

setenv TERM xterm

or

set noglob; eval ‘tset -s -I -Q $TERM‘; unset noglob

from a .loginx11 script.

Page 273: Unix 1

Bruce Hunter 2/21/01 page 273

UNIX Devices: Terminals, Printers,...

UNIX part I

How do you know what terminal you’re on? Ask the machine with tty.

232% tty/dev/pts/4233%

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.

Page 274: Unix 1

Bruce Hunter 2/21/01 page 274

UNIX Devices: Terminals, Printers,...

UNIX part I

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 beaccepted 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.

Page 275: Unix 1

Bruce Hunter 2/21/01 page 275

UNIX Devices: Terminals, Printers,...

UNIX part I

Printers

We may be heading for a paperless society, but for now a lot of trees arebecoming paper for printers. To send someting to a printer, you must firstspool it with a spooling system, and there are no less than three printerqueuing (spooling) systems used in UNIX today.

lp AT&T UNIX spool system

lpr BSD spool system

qprt AIX spool system

fm32f2pub1

pub1 the first (public) system there

fm: Folsom

3: fm3

2: second floor

f2: pole F2

What’s in a name?

Page 276: Unix 1

Bruce Hunter 2/21/01 page 276

UNIX Devices: Terminals, Printers,...

UNIX part I

BSD’s lpr Spooler System

lpr is the BSD printer command. If you have a printer called lw1, you senda 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% lpqQueue Dev Status Job Files User PP % Blks Cp Rnk------- ----- --------- --- ------- - ---- -- ----- -- ---lps20 dplps READYlps20: no entries245%

If you mess up and send a binary file to the printer, you can get rid of it withlprm. There may be a problem, however, if your job went to a print server.

Page 277: Unix 1

Bruce Hunter 2/21/01 page 277

UNIX Devices: Terminals, Printers,...

UNIX part I

What’s a Spooler?

With multiple users sending print requests to multiple printers, a spoolersystem is needed to hold the printer requests and files so they can be sentto printers one at a time.

spooler

request

printer

Page 278: Unix 1

Bruce Hunter 2/21/01 page 278

UNIX Devices: Terminals, Printers,...

UNIX part I

The UNIX System V Print Spooler

The UNIX System V print spooler has its own set of commands. The printcommand 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 epsonprinter 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 areno jobs ahead of yours.

Page 279: Unix 1

Bruce Hunter 2/21/01 page 279

UNIX Devices: Terminals, Printers,...

UNIX part I

spooler (lpsched)

enabledisable

acceptreject

lp

Switchies on the LP spooler

Page 280: Unix 1

Bruce Hunter 2/21/01 page 280

UNIX Devices: Terminals, Printers,...

UNIX part I

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 systemadmins fifos model tempbin forms.readme requests

you definitely are going to have to use lp. The command lpr is usuallyaliased to lp but is not the BSD lpr command.

Page 281: Unix 1

Bruce Hunter 2/21/01 page 281

UNIX Devices: Terminals, Printers,...

UNIX part I

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.

Page 282: Unix 1

Bruce Hunter 2/21/01 page 282

UNIX Devices: Terminals, Printers,...

UNIX part I

Killing A Request On the Same System Doing thePrinting

If you send a binary file to any printer, or a postscript file to a non-postscriptprinter, you will need to cancel the job. The command is cancel whichcancels 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 acomplete list, use lpstat. Specifying a printer cancels the request that iscurrently printing on that printer. .

Note that you get a message when you send a print request to the printer

$ lpr -depson lp.foilrequestid 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), theprint request will probably reach the printer server before you get a chanceto kill it. The most positive move then is to cancel the spool request at theprint server. Note that the print server is most likely to be running under thelpr spooler system.

Page 283: Unix 1

Bruce Hunter 2/21/01 page 283

UNIX Devices: Terminals, Printers,...

UNIX part I

QuizWhat are the printing, queue-observation, and cancel commands for

1) System V

2) BSD

3) AIX

LabFind out which spooler system you have on your system.

Send /etc/motd to the printer.

Check the queue.

Page 284: Unix 1

Bruce Hunter 2/21/01 page 284

UNIX Devices: Terminals, Printers,...

UNIX part I

Floppy Drives

Nearly all workstations and UNIX PCs nearly all have floppy drives. The5 1/4 inch format used to be a standard but is now largely replaced by thesmaller 3 1/2. A floppy drive with diskette can be mounted as a UNIX filesystem, 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 feware

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

Page 285: Unix 1

Bruce Hunter 2/21/01 page 285

UNIX Devices: Terminals, Printers,...

UNIX part I

tar example

255% tar cvf /dev/fd0 sh.mana sh.man 83 blocks.256%

256% tar tvf /dev/fd0-rw-r--r-- 100 0 42139 Feb 03 09:47:46 1994 sh.man257%

Page 286: Unix 1

Bruce Hunter 2/21/01 page 286

UNIX Devices: Terminals, Printers,...

UNIX part I

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/fd0brw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/fd0.18brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 /dev/fd0.9brw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/fd0hbrw-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 fd0is 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/rfd0crw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/rfd0.18crw-rw-rw- 1 root system 11, 2 Jan 16 07:54 /dev/rfd0.9crw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/rfd0hcrw-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 atthe 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 fd0brw------- 1 root root 2, 12 May 5 1998 fd0D360brw------- 1 root root 2, 16 May 5 1998 fd0D720brw------- 1 root root 2, 28 May 5 1998 fd0H1440

Note the densities as part of the name.

Page 287: Unix 1

Bruce Hunter 2/21/01 page 287

UNIX Devices: Terminals, Printers,...

UNIX part I

Diskette Formats For Sun, SCO, and IBM

It is convenient to transfer system files by floppy diskette. This system of filetransfer is sometimes called sneakernet. Before you can use a diskette itmust 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 bothbut with different syntax.

The most common, and therefore portable, format is “high” density 135tracks 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 onusing 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

Page 288: Unix 1

Bruce Hunter 2/21/01 page 288

UNIX Devices: Terminals, Printers,...

UNIX part I

Man Page Example, Linux Folppy Format

FDFORMAT(8) Linux Programmer's Manual FDFORMAT(8)

NAMEfdformat - Low-level formats a floppy disk

SYNOPSISfdformat [ -n ] device

DESCRIPTIONfdformat 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

informationalpurposes 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.

Page 289: Unix 1

Bruce Hunter 2/21/01 page 289

UNIX Devices: Terminals, Printers,...

UNIX part I

The related command is fd. It goes on to explain the formats the fd driverscan 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 20fdnh410 410K 41 10 2 48fdnh420 420K 42 10 2 64fdnh720 720K 80 9 2 24fdnh880 880K 80 11 2 80fdnh1200 1200K 80 15 2 8fdnh1440 1440K 80 18 2 40fdnh1476 1476K 82 18 2 56fdnh1494 1494K 83 18 2 72fdnh1600 1600K 80 20 2 92

3.5 inch double density device files:

Name Capac. Cyl. Sect. Heads Base minor #----------------------------------------------------------fdnD360 360K 80 9 1 12fdnD720 720K 80 9 2 16fdnD800 800K 80 10 2 120fdnD1040 1040K 80 13 2 84fdnD1120 1120K 80 14 2 88

3.5 inch high density device files:

Name Capac. Cyl. Sect. Heads Base minor #----------------------------------------------------------fdnH360 360K 40 9 2 12fdnH720 720K 80 9 2 16fdnH820 820K 82 10 2 52fdnH830 830K 83 10 2 68

Page 290: Unix 1

Bruce Hunter 2/21/01 page 290

UNIX Devices: Terminals, Printers,...

UNIX part I

fdnH1440 1440K 80 18 2 28fdnH1600 1600K 80 20 2 124fdnH1680 1680K 80 21 2 44fdnH1722 1722K 82 21 2 60fdnH1743 1743K 83 21 2 76fdnH1760 1760K 80 22 2 96fdnH1840 1840K 80 23 2 116fdnH1920 1920K 80 24 2 100

3.5 inch extra density device files:

Name Capac. Cyl. Sect. Heads Base minor #----------------------------------------------------------fdnE2880 2880K 80 36 2 32fdnCompaQ 2880K 80 36 2 36fdnE3200 3200K 80 40 2 104fdnE3520 3520K 80 44 2 108fdnE3840 3840K 80 48 2 112

The points to consider here are about man page lookup. First point: Thereare 9 separate manuals 1 - 8 and n. Not everyting is in man 1. Second lookat the bottom of each man page for See Also. It is a pointer to the next manpage. To get a special man page like fd from section 4 use this format:

% man 4 fd

Page 291: Unix 1

mail

Bruce Hunter 2/21/01 page 291

UNIX Part I

Mail

UNIX Part I

Rev ed 7/13/99ECT001118

Section 8

Page 292: Unix 1

mail

Bruce Hunter 2/21/01 page 292

UNIX Part I

Using Mail Productivity

Many employees spend up to an hour or more a day sending and receivingmail. Understanding how mail works and taking advantage of all parts of themail 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.

Page 293: Unix 1

mail

Bruce Hunter 2/21/01 page 293

UNIX Part I

What is mail?

Mail is a loose term email, even looser. In the computer sense mail andemail are utilities by which we can send and receive mail. Traditional-UNIXusers use the command mail, but even that is an over simplification. UNIXmail 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-levelhierarchy of agents.

user agents

routing agent

delivery agents

Page 294: Unix 1

mail

Bruce Hunter 2/21/01 page 294

UNIX Part I

/ucb/mail /bin/mail mh elm

sendmail

local (/bin/mail) TCP/IP UUCP

USER AGENTS

ROUTING AGENT

DELIVERY AGENTS

Agents Used In Mail Deleivery

Page 295: Unix 1

mail

Bruce Hunter 2/21/01 page 295

UNIX Part I

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 Internetdelivery.

To make understanding mail easer, we will deal deal with the user agent/usr/ucb/mail for a while.

Page 296: Unix 1

mail

Bruce Hunter 2/21/01 page 296

UNIX Part I

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 Intelgateway.

Page 297: Unix 1

mail

Bruce Hunter 2/21/01 page 297

UNIX Part I

What’s A Mail Domain?

Medium sized to large organizations like to make the whole organization asingle mail domain. Larger organizations subdivide into mail domains. Fre-quently these domains correspond to internal organizations or divisions, butthey 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

Page 298: Unix 1

mail

Bruce Hunter 2/21/01 page 298

UNIX Part I

UNIX Mail Addressing

Surface mail requires an address; so does mail. If you put a note in anenvelope and put just the recipients name on it, it will be delivered only ifyou 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 localdomain. Beyond that you will need more than the person’s log-on name.Here is an internet-style address.

mail [email protected]

What’s it all about?

This is the “canonical” form. Note that the user’s name is separated from thesystem name with an @. The system from the organization and the organiza-tion from the major domain is separated with a dot.

[email protected]

The major domain name

The recipient’s log-on name

The name of the recipient’s system

The name of the organization

Page 299: Unix 1

mail

Bruce Hunter 2/21/01 page 299

UNIX Part I

Messages and Envelopes

Mail, as a whole, consists of a message and an envelope. With mail, themessage is what you type or include, and the header is the separate infor-mation needed to send the mail. Here is a header.

From [email protected] Wed Feb 16 15:45:29 1994Return-Path: <[email protected]>Received: from trngtrng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-4.1)

id AA23663; Wed, 16 Feb 94 15:45:25 PSTReceived: from INSIDE.intel.COM (fm1n.intel.com) bytrng.intel.com with SMTP id AA07390(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: <[email protected]>Date: 16 Feb 1994 15:35:15 GMTFrom: "Enterprise Change R" <[email protected]>Subject: WW08 ENCO Change Summary Pt2To: "BOB BAUER" <[email protected]>,..

"JOHN REECE" <[email protected]>,"RODNEY RUBERT" <[email protected]>,"AJAY SEVAK" <[email protected]>

(4 entries truncated from copylist)Comment: MEMO 1994/02/16 15:39

Page 300: Unix 1

mail

Bruce Hunter 2/21/01 page 300

UNIX Part I

Let’s send a simple local mail message and look at its header.

46%46% mail bhunterSubject: greetingsGood morning!.Cc:47% hostnamejaeger48%

From [email protected] Thu Feb 17 08:08:26 1994Return-Path: <[email protected]>Received: from trng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-4.1)

id AA19947; Thu, 17 Feb 94 08:08:25 PSTReceived: from jaeger ([132.233.71.32]) by trng.intel.com withSMTP id AA17748(5.65c/IDA-1.4.4 for <[email protected]>); Thu, 17 Feb

1994 08:08:23 -0800From: Bruce H Hunter <[email protected]>Received: by jeager (AIX 3.2/UCB 5.64/trng-SMTP-version-5-3-93)

id AA22340; Thu, 17 Feb 1994 08:08:23 -0800Date: Thu, 17 Feb 1994 08:08:23 -0800Message-Id: <9402171608.AA22340@jeager>To: [email protected]: greetings

Good morning!

49%

Page 301: Unix 1

mail

Bruce Hunter 2/21/01 page 301

UNIX Part I

Note the key lines in the header

Received: from trng.intel.com (fmsu04-2) by fmsu01

From: Bruce H Hunter <[email protected]>

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 traveledthrough a mail master.

From [email protected] Thu Feb 17 08:08:26 1994Return-Path: <[email protected]>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% dfFilesystem 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/workfmsu01:/misc 819342 153712 81% - - /miscfmsu01:/usr/spool/mail 408391 288293 29% - - /usr/spool/mail

Page 302: Unix 1

mail

Bruce Hunter 2/21/01 page 302

UNIX Part I

Mail is stored in /var/spool/mqueue just before delivery. There are atleast two files for each message.

fmsu01# ls -ltotal 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 xfAA18373fmsu01# pwd/var/spool/mqueue

The qf (or queue) files are the header files. The df files are the data. Thesefiles stay just momentarily and are them combined and sent to your ownmail file in/var/spool/mail. Here is a qf file. It will be translated into amail header.

fmsu01# cat qf*P1525T761500592DdfAA18892S<[email protected]>H?P?return-path: <[email protected]>Hreceived: from trng.intel.com (fmsu04-2) by fmsu01 (4.1/SMI-4.1)

id AA18892; Thu, 17 Feb 94 07:56:32 PSTH?x?full-name:Hreceived: from intruder (intruder.intel.com) by trng.intel.comwith SMTP id AA16536(5.65c/IDA-1.4.4 for <[email protected]>); Thu, 17 Feb 199407:56:31 -0800HFrom: Operator <[email protected]>Hreceived: by intruder (4.1/trng-SMTP-version-5-3-93)

id AA11703; Thu, 17 Feb 94 07:56:59 PSTHdate: Thu, 17 Feb 94 07:56:59 PSTHmessage-id: <9402171556.AA11703@intruder>HTo: [email protected]: in.tftpd--eng050-224a.fm.intel.com-132.233.50.240R<dhudock>

Note the sender and recipient addresses.

Page 303: Unix 1

mail

Bruce Hunter 2/21/01 page 303

UNIX Part I

Your mail file in /var/spool/mail belongs to you. You are the owner.

28% cd ../mail29% pwd/var/spool/mail30% ls -l bhunter-rw------- 1 bhunter 36922 Feb 16 17:02 bhunter31% whoamibhunter32%

Page 304: Unix 1

mail

Bruce Hunter 2/21/01 page 304

UNIX Part I

QuizWhere 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?

Page 305: Unix 1

mail

Bruce Hunter 2/21/01 page 305

UNIX Part I

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% fromFrom: "Enterprise Change R" <[email protected]> WedFeb 16 15:45:29 199443%

biff must be built into one of your dot files. xbiff must be part of your Xdot files.

Page 306: Unix 1

mail

Bruce Hunter 2/21/01 page 306

UNIX Part I

Sending Mail

Sending mail is simple. Let’s go back to the greeting message. The user ison 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 allby itself.

Good morning!.Cc:

The Cc line can be ignored if you don’t want courtesy copies. You can alsosend a prepared mail (file) with redirection to the mail command.

mail deastman<ww7

Page 307: Unix 1

mail

Bruce Hunter 2/21/01 page 307

UNIX Part I

No News Is Good News

You will receive no notification if the mail message you send is deliveredsuccessfully. If not it will come back, you will be notified, and the messagewill be in $HOME/deadletter or back in your mail.

Page 308: Unix 1

mail

Bruce Hunter 2/21/01 page 308

UNIX Part I

Receiving (Reading) Mail

Use the mail command to receive mail.

57% mailMail [5.2 UCB] [IBM AIX 3.2] Type ? for help."/usr/spool/mail/bhunter": 6 messages 6 new>N 1 [email protected] Wed Feb 16 15:45 267/12292"WW08 ENCO Change Summary Pt2"N 2 [email protected] Wed Feb 16 15:47 322/15027

"WW08 ENCO Change Summary (TCR"N 3 [email protected] Wed Feb 16 16:51 60/2088

"FOLSOM (FWR) FACILITIES WORK "N 4 [email protected] Wed Feb 16 16:52 122/3287 "CAMaterials Technology Analy"N 5 [email protected] Wed Feb 16 17:02 86/4228

"Hotel Rooms are Scarce in Ari"N 6 [email protected] Thu Feb 17 08:08 17/646 "greetings"

&

The & is mail’s prompt. Type in its message number at the prompt to lookat a message.

& 6Message 6:From [email protected] Thu Feb 17 08:08:26 1994From: Bruce H Hunter <[email protected]>Date: Thu, 17 Feb 1994 08:08:23 -0800To: [email protected]: greetings

Good morning!

&

Page 309: Unix 1

mail

Bruce Hunter 2/21/01 page 309

UNIX Part I

Deleting Mail

Type d at the prompt to delete the message.

&d

The next message will then appear.

& dMessage 5:From [email protected] Wed Feb 16 17:02:25 1994Date: 16 Feb 1994 16:52:16 GMTFrom: "Bulletin Administra" <[email protected]>Subject: Hotel Rooms are Scarce in ArizonaComment: MEMO 1994/02/16 17:00Apparently-To: <[email protected]>

PLEASE READ & FORWARD TO YOUR TRAVELERS. THANK YOU.

Quitting Mail

At the & prompt type q, and mail is terminated.

&At EOF& qSaved 1 message in mboxHeld 4 messages in /usr/spool/mail/bhunter58%

Page 310: Unix 1

mail

Bruce Hunter 2/21/01 page 310

UNIX Part I

If you quit before reading all your mail, the last message will be saved inyour $HOME/mbox. It is a good idea to clean it out from time to time.

58% cd59% ls -l mbox-rw------- 1 bhunter system 24158 Feb 17 08:48 mbox62% cat /dev/null>mbox63%

It’s even easier in sh.

336% ls -l mbox-rw------- 1 bhunter system 6357 Dec 9 07:37 mbox337% sh$ >mbox$ ls -l mbox-rw------- 1 bhunter system 0 Dec 22 08:25 mbox$

Page 311: Unix 1

mail

Bruce Hunter 2/21/01 page 311

UNIX Part I

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 waslisted in the mailbox last time you invoked mail, but you did not read.

22% mailMail [5.2 UCB] [IBM AIX 3.2] Type ? for help."/usr/spool/mail/bhunter": 4 messages 4 unread>U 1 [email protected] Wed Feb 16 15:45 268/12302"WW08 ENCO Change Summary Pt2"U 2 [email protected] Wed Feb 16 15:47 323/15037

"WW08 ENCO Change Summary (TCR"U 3 [email protected] Wed Feb 16 16:51 61/2098

"FOLSOM (FWR) FACILITIES WORK "U 4 [email protected] Wed Feb 16 16:52 123/3297 "CAMaterials Technology Analy"&

Page 312: Unix 1

mail

Bruce Hunter 2/21/01 page 312

UNIX Part I

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 asthe help subcommand.

!commandExecutes 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 messagelistas 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 withoutchanging the original contents of your mailbox. The mailbox returns tothe condition that it was when mail was started. Messages markedto 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).

+NameRefers to a file in your folder directory.

Page 313: Unix 1

mail

Bruce Hunter 2/21/01 page 313

UNIX Part I

foldersLists the names of the folders in your folder directory.

f [messagelist] displays the headings of messages inmessagelist.

h lists the headings in the current group of messages (each groupof messages contains 20 messages by default.Change this with the set screen= statement).

help displays a brief summary of mailbox subcommands. It is identicalto the ? subcommand.

hold [messagelist] Marks each message in messagelist to besaved in your system mailbox (/var/spool/mail/logname)instead of in your $HOME/mbox file. It does not override thedelete subcommand.

l Displays a list of mail subcommands with no explanation ofwhat they do.

mbox [messagelist] Indicates that the messages in messagelist areto be sent to your personal mailbox ($HOME/mbox) when you quitthe mail. This operation is the default action for messages that youhave 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 usinga defined pager program like more or pg to control display to thescreen.

new [messagelist] Marks each message in messagelist as not havingbeen read.

Page 314: Unix 1

mail

Bruce Hunter 2/21/01 page 314

UNIX Part I

n Makes the next message in the mailbox thecurrent message and displays that message. With an argumentlist, it displays the next matching message.

pa Displays the messages in messagelist usingthe 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 messagesread, but, not deleted or saved, are stored in your personal mailbox

($HOME/mbox). All messages you have marked to be deleted areremoved from the mailbox and cannot be recovered. All messagesmarked with the hold or preserve option and messages you havenot 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 whoreceive copies of a message.

s file Saves a messagelist including headinginformation to a file or folder.

size Displays the sizes in lines/characters of themessages in messagelist.

top [messagelist]Displays the top few lines of the messages specified by messagelist.

u Removes the messages in messagelist fromthe list of messages to be deleted when you quit mail.

unread Marks each message in messagelist as not having been read.

Page 315: Unix 1

mail

Bruce Hunter 2/21/01 page 315

UNIX Part I

The ~ Escape

The mail program treats lines beginning with the ~ (tilde) character assubcommands. The following describes the subcommands used whilein the mail editor. The editor recognizes subcommands only if you enterthem at the beginning of a new line.

˜? Displays a summary of the mail subcommands.

˜! commandExecutes the shell command and returns to the message.

˜c addresslistAdds names in addresslist to the list of people to receivecopies of the message. The ˜c subcommand can only be used to addto, not change or delete, the contents of the Cc: list.

˜d Appends the contents of the dead.letter file to the end of themessage.

˜e Starts the alternate editor (like vi) using the message text as theinput file. This editor can be defined with the set EDITOR= statement.

When you exit the editor, you return to the mail editor, where you mayadd text, or send the message by exiting mail.

˜f [messagelist]Includes a messagelist in the current message toforward the message to another user. This subcommand reads eachmessage in the messagelist and appends it to the current end of themessage, 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 informationto that field. Press Enter to save any changes to the field and to

display the next field and its contents.

Page 316: Unix 1

mail

Bruce Hunter 2/21/01 page 316

UNIX Part I

˜m [messagelist]Includes a messagelist in the current message.

˜q Quits the editor without sending the message. Saves the mesage inthe dead.letter file in your home directory, unless the nosaveoption is set.

˜r fileReads the contents of a file into the current message.

˜s stringChanges the subject field to the phrase specified in string.

˜t addresslistAdds 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 editorcan be defined with the set VISUAL= statement. When you exit thateditor, you return to the mail editor, where you may add text to themessage or send the message by exiting mail.

˜w fileWrites the message to file.

˜: subcommandExecutes subcommand and returns to the mail.

˜˜ Allows you to use the ˜ (tilde) character in a message without it beinginterpreted as a command prefix. A ˜˜ key sequence results in onlyone ˜ character being included in the message.

Page 317: Unix 1

mail

Bruce Hunter 2/21/01 page 317

UNIX Part I

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 usercbarret?

Page 318: Unix 1

mail

Bruce Hunter 2/21/01 page 318

UNIX Part I

Customizing mailYour .mailrc script customizes your mail environment.

29% cat .mailrcset append dot autoprintset folder=/usr/system/bhunter/trng.mailalias george gsimko@folsm1alias dave dhudock@folsm1alias gene gsady@fmwu39alias chad [email protected]%

A line that begins with a # (pound sign) followed by a space is treated as acomment. The following key words are used.

alias newalias { address... |previousalias... }Defines an alias or distribution list. The alias can be defined as an actualmail address, or as another alias that is defined in a previous entry in the.mailrc file. To define a group, enter multiple addresses or previousaliases separated by spaces.

alias group dave gene george shilo russ fes_group

ignore fieldlistAdds the header fields in fieldlist to the list of fields to be ignored. Ignoredfields are not displayed when you look at a message with the type or printsubcommand. Use this subcommand to suppress machine-generatedheader fields.

set [optionlist | Option=Value...]Sets an option. The argument following the set option can be either anoptionlist giving the name of a binary option (an option that is either set orunset) or an option=value entry used to assign a value to an option.

unset optionlistDisables the values of the options specified in optionlist. This action is theinverse of the set optionlist entry.

Page 319: Unix 1

mail

Bruce Hunter 2/21/01 page 319

UNIX Part I

Binary Options For set and unsetappendAdds messages saved in your mailbox to the end rather than to the begin-ning of the $HOME/mbox file.

askPrompts for the subject of each message sent. If you do not wish to createa subject field, press Enter at the prompt.

askccPrompts 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.

autoprintSets the delete subcommand to delete the current message and displaythe next message.

debugDisplays debugging information. Messages are not sent while in debugmode. This is the same as specifying the -d flag on the command line.

dotInterprets a period entered on a line by itself as the end of a message youare sending.

holdHolds messages the you have read but have not deleted or saved in thesystem mailbox instead of in your personal mailbox. This option has noeffect on deleted messages.

ignoreIgnores interrupt messages from your terminal and echoes them as @ (atsign) characters.

ignoreeofSets mail to refuse to quit at the ^D key sequence as the end of a mes-sage.

Page 320: Unix 1

mail

Bruce Hunter 2/21/01 page 320

UNIX Part I

keepsavePrevents mail from deleting messages that you have saved with the s or wsubcommand. Normally, messages are deleted automatically when you exitthe mail command. Use keepsave and hold options to hold messages inyour system mailbox. Otherwise, the messages are placed in$HOME/mbox.

metooIncludes the sender in the alias expansion. By default, expanding the aliasremoves the sender. When this option is set in your .mailrc file, sendinga message using an alias that includes your name sends a copy of the mes-sage to your mailbox.

noheaderSuppresses 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.

nosavePrevents retention of interrupted letters in t $HOME/dead.letter.

quietSuppresses the printing of the banner when mail starts. The banner is theline that shows the name of the mail program.

verboseDisplays the actual delivery of messages on the terminal.

Page 321: Unix 1

mail

Bruce Hunter 2/21/01 page 321

UNIX Part I

Value Options for the set Entry

You can use a set entry to assign values to the following options. Forexample, enter

set screen=20

to limit headers to 20 lines per screen.

crt=linesDefines the number of lines of a mail message mail displays before paus-ing for input (this option starts pg to control the scrolling).

EDITOR=editorGives the full path name of the editor to be started with the e mailbox sub-command or the ˜e mail editor subcommand.

escape=CChanges the escape character C used for mail editor subcommands. Thedefault character is ˜ (tilde).

folder=pathnameGives the path name of a directory in which to store mail folders. Once thedirectory is defined, you can use the + (plus sign) notation to refer to itwhen using the file name ++ parameter with mailbox subcommands.

record=filenameDefines a file in which to record outgoing mail. The path name must begiven relative to the user’s home directory.

screen=linesDefines the number of lines of message headers displayed.

toplines=linesDefines the number of lines displayed by the top subcommand.

VISUAL=editorGives the full path name of the editor to be started with the v subcommandor the ˜v mail editor subcommand. The default editor is vi.

Page 322: Unix 1

mail

Bruce Hunter 2/21/01 page 322

UNIX Part I

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

Page 323: Unix 1

mail

Bruce Hunter 2/21/01 page 323

UNIX Part I

Quiz

Explain these entries in .mailrc

set folder=’Mail’set append dot autoprintalias mark [email protected] mustang-list [email protected]

Page 324: Unix 1

mail

Bruce Hunter 2/21/01 page 324

UNIX Part I

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 yoursystem.

24% ps -efUSER PID PPID C STIME TTY TIME CMDroot 1 0 0 Jan 16 - 23:34 /etc/initroot 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 [email protected] 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: <[email protected]>

You didn’t even type in an address, and now it has a address and it isn’teven your machine. sendmail has added an address, not any old addressbut a fully canonical address in Internet format. The received line shows itknows 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.

Page 325: Unix 1

mail

Bruce Hunter 2/21/01 page 325

UNIX Part I

/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 UNIXaccount, he’s on soft switch. How did it work? In the file /etc/aliases orits YP equivalent the is a list of aliases that mail uses. Here is just a smallprice of it.

gcanepa:[email protected]:gchin@fmsu01glenmcph:glenmcph@fmsu01gmiller:gmiller@fmsu01grathbun:grathbun@fmsu01grc:[email protected]:gsady@fmsu01gsiemers:gsiemers@fmsu01gsimko:[email protected]

Notice the first intial and last names. They are on soft switch. The aliasesfile substitutes the adddress to get it to sendmail. sendmail will do therest of the adress translation to soft switch. Here is rule one of sendmailfor the address translation.

S1## rules added for softswitch addressing#R$-@$j$* $1@$R.$D make FROM field mail hostR$- $1@$R.$D add domain to single addressesR$+@$-@$+ $1@$2.$D ssw reply resolution#

Page 326: Unix 1

mail

Bruce Hunter 2/21/01 page 326

UNIX Part I

.forward

What if you are temporarily transferred to another site for a few days? Howwill you get your mail?

Create a file called .forward in your $HOME. In it put your logname andnew, temporary address.

%cd% cat [email protected]

An extemely useful use of .forward is to send you UNIX mail to your Win-dows account.

[email protected]

Page 327: Unix 1

Bruce Hunter 2/21/01 page 327

SecurityUNIX Part I

Security

UNIX Part I

Rev 1/13/99ECT001118

section 9

Page 328: Unix 1

Bruce Hunter 2/21/01 page 328

SecurityUNIX Part I

“There is no security.” Spoken by the character Wolfgar in the film NightHawks.

These words are frightening, but with a little work and some knowledgethere is security. In the film, the antagonist, Wolfgar, was eventually killedand then there was security — for a while. That brings about another point.Security, like freedom, must be guarded and worked on constantly.

Page 329: Unix 1

Bruce Hunter 2/21/01 page 329

SecurityUNIX Part I

Security

What is security? You leave a dollar and a half in your desk drawer and thenext 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 openon your desk day after day? Why do you leave your computer terminal openfor anyone who comes along to access it, day after day?

Computer security is often treated as an afterthought, while in reality it hasthe greatest potential for theft or abuse of anything that you are apt to comeacross at work on any day.

Page 330: Unix 1

Bruce Hunter 2/21/01 page 330

SecurityUNIX Part I

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 ofits 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

Page 331: Unix 1

Bruce Hunter 2/21/01 page 331

SecurityUNIX Part I

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 ananagram of your name or any part of your life that is obvious.It should be character rich (upper/lower/numeric mix with at leastone non character non number) and nothingthat 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 watchingevery keystroke?

Don’t walk away and leave your screen unattended. Lock it.

Protect your file systems and files from casual access with reasonablepermissions.

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 yourfiles, give it to them with group permissions.

Page 332: Unix 1

Bruce Hunter 2/21/01 page 332

SecurityUNIX Part I

Logons and Passwords

A password was assigned when your account was created. That passwordis your key to the system and should be protected at all times. Your logonprompt 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 aTrojan 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

Page 333: Unix 1

Bruce Hunter 2/21/01 page 333

SecurityUNIX Part I

Changing Your Password

The first step is to pick a good password, one that is a character mix andsomething that even your best friend wouldn’t know. How about that pair ofskis 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 exceptthe newline. Even control characters work, and they are hard to see. Thecommand 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 totype it again to be sure that you didn’t make a typo.

You need to use the yppasswd command on systems with NIS (YP). Itworks the same as /bin/passwd.

Page 334: Unix 1

Bruce Hunter 2/21/01 page 334

SecurityUNIX Part I

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 bhunterbhunter:rzojv8/Uf2q92:100:0:Bruce H Hunter:\/usr/system/bhunter:/bin/csh87%

The garbage you see in the second field is the result of your password beingencrypted. On newer systems, your password encryption is in a shadowfile.

Page 335: Unix 1

Bruce Hunter 2/21/01 page 335

SecurityUNIX Part I

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?

Page 336: Unix 1

Bruce Hunter 2/21/01 page 336

SecurityUNIX Part I

File Permissions

UNIX has a unique file permissions protection system. Do a long listing ofany file system.

94% ls -altotal 4drwxr-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 foo95%

What is that stuff in the first field?

-rwxr-xr-xdrwxr-xr-x

regular file

directory

readwrite

executeno permission

user

group

other (everyone)

Page 337: Unix 1

Bruce Hunter 2/21/01 page 337

SecurityUNIX Part I

What Does rwx Mean?Permissions are set in binary bits, and expressed in octal.

Now let’s go back.

94% ls -altotal 4drwxr-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 foo95%

The dot directory is readable, writable, and “executable” by bhunter, read-able and “executable” by the group system and everyone else. Executablefor a file makes sense but for a directory? For a directory, x means it can betraversed. Turn it off, and no one can get to the directory.

102% cd junk103% mkdir noenter104% chmod 666 noenter105% ls -ld noenterdrw-rw-rw 2 bhunter system 512 Feb 18 09:47 noenter106% cd noenternoenter: Permission denied107%

R W X

100 010 001

Sum = 7 = 0111

124

base 2

octal

read write execute

2

Page 338: Unix 1

Bruce Hunter 2/21/01 page 338

SecurityUNIX Part I

Mode Bits

Permissions are also called permissions modes or mode bits. Here is whatthey 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 executedby 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 rootexecuted 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 beunlinked if the requesting process has write permission for the directory andis 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.

Page 339: Unix 1

Bruce Hunter 2/21/01 page 339

SecurityUNIX Part I

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 stickybash-2.00$ ls -ld sticky/drwxr-x--- 2 bhunter users 512 Jul 14 07:24 sticky/bash-2.00$ chmod +t stickybash-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 foobash-2.00$ ln foo barbash-2.00$ ls -ltotal 0-rw-r----- 2 bhunter users 0 Jul 14 07:27 bar-rw-r----- 2 bhunter users 0 Jul 14 07:27 foobash-2.00$ pwd/usr/users/a5fs/bhunter/junk/sticky

The link, bar, can be removed only by the owner or by root even if directorypermissions would otherwise permit it. Links can be made across directoriesbut not physical partitions.

bash-2.00$ ln sticky/foo snargbash-2.00$ lssnarg stickybash-2.00$ ls -ltotal 2-rw-r----- 3 bhunter users 0 Jul 14 07:27 snargdrwxr-x--T 2 bhunter users 512 Jul 14 07:55 sticky

Page 340: Unix 1

Bruce Hunter 2/21/01 page 340

SecurityUNIX Part I

Exercise

Explain the following permissions.

109% cd cron110% ls -latotal 32drwxr-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 atjobsdrwxrwx--- 2 bin cron 512 Aug 13 1993crontabs111% ls -l /bin/passwd-r-sr-xr-x 1 root system 46797 Dec 14 1992 /bin/passwd112% ls -l /etc/passwd-rw-rw-r-- 1 root system 229 Jan 07 1993 /etc/passwd113% ls -l /etc/shutdownlrwxrwxrwx 1 root system 18 Dec 14 1992 \

/etc/shutdown -> /usr/sbin/shutdown114% ls -l /etc/motd-r-xr--r-- 1 bin bin 880 Dec 14 1992 /etc/motd

Page 341: Unix 1

Bruce Hunter 2/21/01 page 341

SecurityUNIX Part I

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 hi129% hihello130% ls -l hi-rwxr-xr-x 1 bhunter system 16 Feb 18 10:13 hi131%

What did you learn? You can’t execute a file that doesn’t have execute per-missions, and you don’t get them automatically.

Page 342: Unix 1

Bruce Hunter 2/21/01 page 342

SecurityUNIX Part I

Changing Permissions

If you own a file or directory, you can change its permissions.

33% whoamibhunter34% chmod 000 /etc/motdchmod: /etc/motd: Operation not permitted.35% ls -l /etc/motd-r-xr--r-- 1 bin bin 880 Dec 14 1992 /etc/motd36%

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 hi131%

Page 343: Unix 1

Bruce Hunter 2/21/01 page 343

SecurityUNIX Part I

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 definedsymbolically or numerically. When you use the symbolic mode to specifypermission 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 specifyingthe ugo flags. The a flag is the default permission field. If thepermission field is omitted, the default is the a flag and the file creationmask (umask) is applied.

Page 344: Unix 1

Bruce Hunter 2/21/01 page 344

SecurityUNIX Part I

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 removesall 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 currentfile-mode bits have at least one of the execute bits set. It is ignoredif the file is not a directory and none of the execute bits are set in thecurrent file mode bits.

s Set User-ID or Set Group-ID permission to that of file. This flag is onlyvalid with the u and g flags of the permission field.

t The sticky bit, sets link permission for directories, saves text attributesfor files.

Page 345: Unix 1

Bruce Hunter 2/21/01 page 345

SecurityUNIX Part I

133% touch perms134% chmod a= perms136% ls -l perms---------- 1 bhunter system 0 Feb 18 12:00 perms137% chmod a+r perms138% ls -l perms-r--r--r-- 1 bhunter system 0 Feb 18 12:00 perms139% chmod u+w perms140% ls -l perms-rw-r--r-- 1 bhunter system 0 Feb 18 12:00 perms141% chmod +x perms142% ls -l perms-rwxr-xr-x 1 bhunter system 0 Feb 18 12:00 perms143% chmod o-r perms144% ls -l perms-rwxr-x--x 1 bhunter system 0 Feb 18 12:00 perms145%

Page 346: Unix 1

Bruce Hunter 2/21/01 page 346

SecurityUNIX Part I

chmod Numeric or Absolute Mode

The chmod command also permits you to use octal notation to set each bitin the permission mode. chmod sets the permissions to the permission codeyou provide. This permission code is constructed by combining (the logicalOR) 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 textattribute 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.

Page 347: Unix 1

Bruce Hunter 2/21/01 page 347

SecurityUNIX Part I

145% chmod 000 perms146% ls -l perms---------- 1 bhunter system 0 Feb 18 12:00 perms

147% chmod 755 perms148% ls -l perms-rwxr-xr-x 1 bhunter system 0 Feb 18 12:00 perms

149% chmod 644 perms150% ls -l perms-rw-r--r-- 1 bhunter system 0 Feb 18 12:00 perms

151% chmod 600 perms152% ls -l perms-rw------- 1 bhunter system 0 Feb 18 12:00 perms

Page 348: Unix 1

Bruce Hunter 2/21/01 page 348

SecurityUNIX Part I

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--.

Page 349: Unix 1

Bruce Hunter 2/21/01 page 349

SecurityUNIX Part I

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 mode664. As security tightens, some systems will default file creation to mode644 or even 600. This is done with a command called umask. It is used inyour .profile, .login, or .chsrc files. The syntax is

umask creationmask

Where the creation mask is a number to be ANDed against the defaultmask. 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 bitsdefined in the sys/mode.h file. Nine bits of the creationmask parameterare significant.”

If the default mask were 000, every file would be -rw-rw-rw. If you setyour mask to 022, every file you create will be -rw-r--r--.

159% cd160% grep umask .??*.loginx11:umask 022161% touch whatmask162% 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 xbits set.

165% cd junk166% mkdir null167% ls -ld nulldrwxr-xr-x 2 bhunter system 512 Feb 18 12:28 null168%

Page 350: Unix 1

Bruce Hunter 2/21/01 page 350

SecurityUNIX Part I

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 -ltotal 2-rw-r----- 3 bhunter users 0 Jul 14 07:27 snargdrwxr-x--T 2 bhunter users 512 Jul 14 07:55 sticky% umask027

Now add a umask line to .cshrc.

umask 022set history = 80set filecset notifyset cdpath=( $home )set prompt="$HOSTNAME""[\!]%"

Be safe and source .cshrc and then make a new file and list it.

fri2006[21]%source .cshrcfri2006[22]%touch umskfri2006[23]%ls -l umsk-rw-r--r-- 1 bhunter users 0 Jul 14 08:24 umskfri2006[24]%umask22

Page 351: Unix 1

Bruce Hunter 2/21/01 page 351

SecurityUNIX Part I

Terminal Locking

When you leave your system, for any reason, you should log out or locktheir terminal. Some software, like X Windows, has a built-in lock com-mands. The most frequently used is xlock. The xlock command locks anX server until the user enters a password at the keyboard. While xlock isrunning, 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 isdisabled, the screen is blanked, and a changing pattern is displayed. If a keyor a mouse button is pressed, a prompt asks for the password of the userwho started xlock.

Use the lock command If you are not in X Windows.

Page 352: Unix 1

Bruce Hunter 2/21/01 page 352

SecurityUNIX Part I

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 changeableonly by you.

What’s a set-UID bit?

How do you set it?

How do you keep others out of one of your directories?

Page 353: Unix 1

Bruce Hunter 2/21/01 page 353

SecurityUNIX Part I

.rhosts

The file $HOME/.rhosts is used by the BSD R-Protocol commands. Thisfile 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 beowned by the user. The permissions of the .rhosts file should be set tomode 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 hostand remote user.

.rhosts file supports the following host/name entries:

+@domain user_name+ # A Giant No NoHostName

-HostName+@NetGroup-@NetGroup

A + signifies that any host on the network is trusted and that can never bethe case!

The HostName entry is the name of a remote host and signifies that anyuser logging in from HostName is trusted. A -HostName entry signifies thatthe host is not trusted. +@NetGroup or -@NetGroup entry signifies that allhosts in the netgroup or no hosts in the netgroup, respectively, are trusted.

The @NetGroup parameter is used by YP (NIS) for grouping.

Page 354: Unix 1

Bruce Hunter 2/21/01 page 354

SecurityUNIX Part I

The Trojan Horse

Here is a Trojan horse script:

201% cat t_horse#!/bin/shclearecho -n "login: "read user_nameecho -n "password: "stty -echoread user_passwdstty echoecho $user_name $user_passwd >/tmp/gotchaclearecho "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/gotchadfool takeme201%

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.

Page 355: Unix 1

Bruce Hunter 2/21/01 page 355

SecurityUNIX Part I

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’tbe 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 andmake it work for them.

Page 356: Unix 1

Bruce Hunter 2/21/01 page 356

SecurityUNIX Part I

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?

Page 357: Unix 1

Bruce Hunter 2/21/01 page 357

SecurityUNIX Part I

Entering Passwords to a Script

The shells allow you to flip flop between full duplex and half duplex with thestty command changing the echo attribute.

#!/bin/shecho "Name: \c"read nameecho "Password; \c"stty -echoread passwordstty echo...

It is difficult to impossible to manipulate this attribute from languages likePERL but a script can be wrapped with a shell script that turns echo off.

% cat wrapper#!/bin/shstty -echo./noecho.pl

% cat ./noecho.pl#!/usr/intel/bin/perlprint "Name: ";$name = <>;print "\nHi $name"

% ./wrapperName:Hi Bruce

Page 358: Unix 1

Bruce Hunter 2/21/01 page 358

SecurityUNIX Part I

Now for a reverse approach. PERL will call a shell script that turns off echo.

The PERL script.

#!/usr/intel/bin/perlprint "Name: ";$name = <>;chomp $name;print "Password: ";$password = ‘getpw‘;print "\nname: $name password: $password\n"

The shell script getpw.

#!/bin/shstty -echoread passwordstty echoecho $password

The run and its results.

% !$getpw.plName: BrucePassword:name: Bruce password: Sol205*GS

Page 359: Unix 1

Bruce Hunter 2/21/01 page 359

SecurityUNIX Part I

Physical Security

There is a lot more to security than permissions and commands. Lock yourcar and leave the keys in the door lock. How secure are you? Leave tapesor 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 wherethey belong, be sure that they are there. If a terminal or workstation is in alocked area, be sure the door is locked and closed.

Page 360: Unix 1

Bruce Hunter 2/21/01 page 360

UNIX Networking

UNIX Part I

UNIXNetworking

UNIX Part I

Rev 1/13/99ECT001118

section10

Page 361: Unix 1

Bruce Hunter 2/21/01 page 361

UNIX Networking

UNIX Part I

"The network is the computer.”Scott McNeily

Page 362: Unix 1

Bruce Hunter 2/21/01 page 362

UNIX Networking

UNIX Part I

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 unixsystem

Attaching to systems with different architectures to get work donethat cannot be done elsewhere.

Accessing information that is not available from your system or inyour domain.

Page 363: Unix 1

Bruce Hunter 2/21/01 page 363

UNIX Networking

UNIX Part I

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 abag. The bag is containerized with other bags and shipped.

Networking is a similar process. You have a message to send, yourdata. The system stuffs it in an envelope, a packet, and writes thesender and recipient addresses on it. This is the Ethernet data.Inside the Ethernet envelope is a Internet envelope, and inside of itis a transport envelope, a packet. Inside of that is your data. It maybe split into many envelopes.

SenderSender_Address

RecipientRecipeint_Address

32c

Page 364: Unix 1

Bruce Hunter 2/21/01 page 364

UNIX Networking

UNIX Part I

Layers and Protocols

Layer Name Address Data Form

Transport Port Packets orSegments

Network Internet Datagrams

Data Link Ethernetor 802.3

Frames

Type

These three layers make the Ethernet-TCP/IP protocol stack. Theyprovide three levels of encapsulation, each operating at a differentlevel.

Page 365: Unix 1

Bruce Hunter 2/21/01 page 365

UNIX Networking

UNIX Part I

physical

data link

network

transport

session

presentation

application

wire or cable transmission hardware

data preparation and network linkage

internetwork routing

port-to port transporation

applications preparation

applications preparation

user applications

OSI/ISO Network Layers

Page 366: Unix 1

Bruce Hunter 2/21/01 page 366

UNIX Networking

UNIX Part I

Each layer has its own name, addres type, and data form.

Each delivers its encapsulated data to its corresponding layer on thenode 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.

Layer Name Address Type Data Form

Transport Port Packets or Segments

Network Internet Datagrams

Data Link Ethernet Frames

Page 367: Unix 1

Bruce Hunter 2/21/01 page 367

UNIX Networking

UNIX Part I

2 2

3 3

4 4

Physical

applications

virt

ualp

ath

Electrical Path

actu

alpa

thPeer-to-Peer Protocol Paths

TCP/IP-Ethernet

Page 368: Unix 1

Bruce Hunter 2/21/01 page 368

UNIX Networking

UNIX Part I

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 wireand have it received only by the addressee. This is done by eachnode reading the destination address and accepting it if the frame isits 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 isfree to send. In the event of a collision, it will back off and resendlater.

All of this takes place in the lowest layer, the physical layer and ishandled, in hardware, by the MAU (media access unit) and the NIC(network interface card.)

Page 369: Unix 1

Bruce Hunter 2/21/01 page 369

UNIX Networking

UNIX Part I

Internet/Ethernet Addresses

The internet and associated ethernet addresses are stored in thesystem’s arp table.

bash-2.00$ arp -aa8fs123 (132.233.123.160) at 0:0:3c:4:13:f2a9fs123 (132.233.123.161) at 0:0:3c:2:48:32giga2-123 (132.233.123.2) at 0:c0:80:8f:8d:74sub123-gw1.fm.intel.com (132.233.123.100) at0:e0:f7:41:9:41a11fs123 (132.233.123.165) at 0:0:3c:2:77:82giga1-123 (132.233.123.214) at 0:c0:80:89:18:afa13fs123 (132.233.123.166) at 0:0:3c:1:f4:b2a1fs123 (132.233.123.150) at 0:0:3c:1:dd:92a19fs123 (132.233.123.151) at 0:0:3c:4:40:e1a14fs123 (132.233.123.167) at 0:0:3c:1:ed:12flc2002 (132.233.123.23) at 8:0:20:80:18:5ea3fs123 (132.233.123.152) at 0:0:3c:4:7b:22a5fs123 (132.233.123.154) at 0:0:3c:2:59:2a4fs123 (132.233.123.156) at 0:0:3c:2:5a:a2

Page 370: Unix 1

Bruce Hunter 2/21/01 page 370

UNIX Networking

UNIX Part I

Internet AddressingThe use of Ethernet addresses is the only method to get local deliv-ery. To get from one network or subnetwork to another requiresinternetworking 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.

Data moving in packets across networks is received on the basis ofits Internet address. That address is a unique four-byte numberdivided into two parts to form three address classes: A, B, and C.The two parts are the network and hosts parts. The network part isunique to one network; the host part unique is only within that net-work.

Internet

Ethernet

Page 371: Unix 1

Bruce Hunter 2/21/01 page 371

UNIX Networking

UNIX Part I

Ethernet Encapsulation

Ethernet frames are encapsulated to the Ethernet standard. Theframe information holds the sender’s and recipient’s address, thetype, the data, and a cyclic redundancy check. This envelope is puton by the link layer as the data leaves a node, and it’s stripped off asit enters a node. The recipient address is read by every node on thesegment, and the entire frame is accepted or rejected on the basisof the address.

Page 372: Unix 1

Bruce Hunter 2/21/01 page 372

UNIX Networking

UNIX Part I

Concentrators / Hubs

Using Etehrnet cable as te prmary carrier is not without problems soin most mid- to large-scale systems it has been replaced with hubs.

Typically the hub is connected with twisted-wire pair to the workst-stions and fiber-optic cable to another concentrator connected to theservers.

Hub

Backbone or Fiberoptic link

Page 373: Unix 1

Bruce Hunter 2/21/01 page 373

UNIX Networking

UNIX Part I

Quiz

How may protocol layers (minimum) on any sent message or file?

How are messages accepted by a host from the wire?

Page 374: Unix 1

Bruce Hunter 2/21/01 page 374

UNIX Networking

UNIX Part I

A Simple Network

server

workstation

Every system on the network is called a node. The Ethernet cable acts as anantenna sending to all nodes.

Page 375: Unix 1

Bruce Hunter 2/21/01 page 375

UNIX Networking

UNIX Part I

s1 s2 R

sn1

sn2

backbone

Two subnets with multi-homed servers

The servers are connected to the subnet segments without the need of therouter. The router connects the segments and is capable of joining a halfdozen subnetworks and connecting to the backbone as well.

Page 376: Unix 1

Bruce Hunter 2/21/01 page 376

UNIX Networking

UNIX Part I

Quiz

Name the components of a contemporary network.

svr

r

Page 377: Unix 1

Bruce Hunter 2/21/01 page 377

UNIX Networking

UNIX Part I

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 toanother.

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 othermachine. With NIS (YP) running within a domain, and your file sys-tems mounted to all systems by NFS, you not only will have anaccount on all systems, but also your environment will appear to bethe same on all systems.

Page 378: Unix 1

Bruce Hunter 2/21/01 page 378

UNIX Networking

UNIX Part I

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

Page 379: Unix 1

Bruce Hunter 2/21/01 page 379

UNIX Networking

UNIX Part I

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:DirectoryHost:Directory |Directory } { User@Host:Directory |Host:Directory | Directory }

rcp is used to copy one or more files between the local host and aremote host, or between two remote hosts, or between files at thesame remote host.

If the path for a file or directory on a remote host is unspecified or isnot fully qualified, the path is interpreted as beginning at the homedirectory 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 theroot user.

2) the local host and user name is included in a$HOME/.rhosts file on the remote user account.

Page 380: Unix 1

Bruce Hunter 2/21/01 page 380

UNIX Networking

UNIX Part I

.rhosts

.rhosts is in every user’s home directory. Its permissions must be0600.

13% ls -l .rhosts-rw------- 1 bhunter uucp 14Mar 221993 .rhosts

The contents of .rhosts is the name and system of any userallowed to use the R commands,

14% cat .rhosts+@mcd bhunter15%

This entry allows this user access from any mcd system.

Page 381: Unix 1

Bruce Hunter 2/21/01 page 381

UNIX Networking

UNIX Part I

rlogin

rlogin connects your system with any remote system that youhave 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.

Page 382: Unix 1

Bruce Hunter 2/21/01 page 382

UNIX Networking

UNIX Part I

.rlogin Security

The remote host allows access if one or both of these conditions aremet:

1) The local host is included in the remote /etc/hosts.equivfile, the local user is not the root user, and the -l user flag isnot specified.

2) The local host and user name is included in the$HOME/.rhosts file in the remote user account.

Example: rlogin stalker

Page 383: Unix 1

Bruce Hunter 2/21/01 page 383

UNIX Networking

UNIX Part I

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 standardoutput 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 usernameUse username as the remote username instead of yourlocal username. In the absence of this option, theremote username is the same as your local username.

-n redirect the input of rsh to /dev/null.

Page 384: Unix 1

Bruce Hunter 2/21/01 page 384

UNIX Networking

UNIX Part I

Example of rsh

38% rsh jeager whobhunter hft/0 Jan 21 07:32bhunter pts/1 Jan 21 07:32bhunter pts/0 Jan 21 07:3239%

Note the complaint, in spite of the fact that the system did the rshanyway.

Page 385: Unix 1

Bruce Hunter 2/21/01 page 385

UNIX Networking

UNIX Part I

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.

Page 386: Unix 1

Bruce Hunter 2/21/01 page 386

UNIX Networking

UNIX Part I

telnet

telnet will log you on to any system that it can reach, providingthat you have an account there.

39% telnet fmsu04Trying...Connected to mcd.Escape character is ’^]’.

SunOS UNIX (mcd)login:msnerdPassword:

...

13% logout[1] 14813Connection closed.

Page 387: Unix 1

Bruce Hunter 2/21/01 page 387

UNIX Networking

UNIX Part I

telnet Syntax

telnet [ -d] [ -n tracefile]\[ -e terminal_type][host [port] ]

telnet Flags

-e terminal_typeoverrides terminal-type negotiation.

-n tracefilerecords network trace information in the file specifiedby the tracefile variable.

A few of the telnet subcommands are:

? [subcommand] requests help on telnet subcommands.

close closes the TELNET connection and returns to telnetcommand mode when the open subcommand is used to establishthe connection.

display [argument]displays all of the set and toggle valuesif no argument variable is specified; otherwise, it lists only thosevalues that match the argument variable.

Page 388: Unix 1

Bruce Hunter 2/21/01 page 388

UNIX Networking

UNIX Part I

open host [port] opens a connection to the specified host.The host specification can be either a host name or an Internetaddress 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 telnetprogram. A ^D in command mode also closes the connectionand exits.

Page 389: Unix 1

Bruce Hunter 2/21/01 page 389

UNIX Networking

UNIX Part I

ftp

The ftp command puts the user in a virtual login on a remotemachine, where the user has a set of limited commands that allowfile manipulation and transfer.

ftp example

43% ftp fmsu04Connected 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/foo200 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> quit221 Goodbye.44%

Page 390: Unix 1

Bruce Hunter 2/21/01 page 390

UNIX Networking

UNIX Part I

ftp syntax

ftp [-d] [-g] [-i] [-n] [-v] [HostName]

The /usr/bin/ftp command is the interface to the File TransferProtocol (FTP). This command uses FTP to transfer files betweenthe 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.

Page 391: Unix 1

Bruce Hunter 2/21/01 page 391

UNIX Networking

UNIX Part I

ftp Subcommands

If you execute the ftp command and do not specify the host nameof 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 theftp> 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 commanddisplays a list of known subcommands.

append localfile [remotefile]This appends localfile to a file on the remote host.

cd remotedirectoryThischanges the working directory on the remote host to thremotedirectory.

closeEnds the file-transfer session, but does not exit the ftp command.

cr strips the carriage return character from acarriage-return/line-feed sequence when receiving recordsduring ASCII-type file transfers.

Page 392: Unix 1

Bruce Hunter 2/21/01 page 392

UNIX Networking

UNIX Part I

dir [remotedirectory][localfile]Writes a listing of the content of the specified remote directoryto 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-nameparameters for these subcommands are not expanded.

help [subcommand] Displays help information.

lcd [directory] Changes the working directory on the localhost.

ls [remotedirectory] [localfile] writes an abbreviatedfile listing of a remote directory to a local file.

mdelete remotefiles expands the files specified by theremote files parameter at the remote host and deletes the remotefiles.

mdir [remotedirectories localfile] expands thedirectories specified by the remotedirectories parameter at theremote host and writes a listing of the contents of thosedirectories to the file specified in the localfile parameter.

mkdir [remotedirectory] Creates the directory specified in theremotedirectory parameter on the remote host.

mput [localfiles] expands the files specified in the localfilesparameter at the local host and copies the indicated local files tothe remote host.

Page 393: Unix 1

Bruce Hunter 2/21/01 page 393

UNIX Networking

UNIX Part I

Working There, Seeing It HereSometimes it is necessary to do your work on a system other thenthe 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 yoursystem

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, clientscan connect from any hostbash-2.00$ hostnamefrx056

Page 394: Unix 1

Bruce Hunter 2/21/01 page 394

UNIX Networking

UNIX Part I

NFS

NFS or Network File Systems allows a host to get files from anotherhost, usually a file server, as if the files and file systems weremounted on the local host. NFS is transparent. Users do not have toknow that it is there as long as it works well, which it almost alwaysdoes.

Do a df from your workstation to see it at work:

44% dfFilesystem 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/workfmsu01:/misc 819342 150688 81% - - /miscfmsu05:/usr/fmo/ssd 944238 90594 90% - - /usr/fmo/ssdfmsu01:/usr/spool/mail 408391 292557 28% - /usr/spool/mailfmsu01:/amoeba 408399 262086 35% - - /mproj/amoebafmsu01:/sam16u3 1893612 252410 86% - - /uproj/sam163fmsu05:/samgr 917358 63802 93% - - /gproj/sam16fmsu01:/g16v1 1836252 754996 58% - - /vproj/sam161fmsu05:/g16v 1278413 74744 94% - - /vproj/sam16fmsu01:/sam16u2 1835996 412575 77% - - /uproj/sam162fmsu01:/sam16u1 917870 206056 77% - - /uproj/sam161fmsu05:/sam16u 1968100 134997 93% - - /uproj/sam16fmsu05:/sam16 1278413 177015 86% - - /mproj/sam16fmsu01:/sam16m1 917870 49078 94% - - /mproj/sam161fmsu05:/g16 1278413 271993 78% - - /gproj/sam161fmsu05:/samgr2 1298653 499607 61% - - /gproj/sam162fmsu05:/e2 944238 739466 21% - - /e2fmsu01:/28u008s 508958 93717 81% - - /gproj/28u008sfmsu05:/usr/mfg 638430 37771 94% - - /usr/mfgfmsu05:/usr/local.gen/ibm 472111 57203 87% /usr/localfmsu05:/usr/system 1836252 337304 81% - - /usr/systemfmsu05:/tapeout2 1240333 264423 78% - - /usr/tapeout2fmsu01:/tapeout3 918382 172715 81% - - /usr/tapeout

Note that most of these systems are mounted from a file server.

Page 395: Unix 1

Bruce Hunter 2/21/01 page 395

UNIX Networking

UNIX Part I

They will appear local in spite of their being remotely mounted . Themechanism are daemons at either end. Here is part of the output ofps.

45% ps -efUSER PID PPID C STIME TTY TIME CMDroot 1 0 0 Jan 16 - 6:50 /etc/init

.root 6206 3095 0 Jan 16 - 0:13 /usr/etc/ypbindroot 6465 3095 0 Jan 16 - 0:02 /usr/etc/biod 6root 6724 6465 0 Jan 16 - 0:02 /usr/etc/biod 6root 6981 6465 0 Jan 16 - 0:01 /usr/etc/biod 6root 7238 6465 0 Jan 16 - 0:01 /usr/etc/biod 6root 7495 6465 0 Jan 16 - 0:02 /usr/etc/biod 6root 7752 6465 0 Jan 16 - 0:02 /usr/etc/biod 6root 8010 3095 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 8267 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 8524 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 8781 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9038 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9295 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9552 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 9809 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8root 10067 3095 0 Jan 16 - 0:00 /usr/etc/rpc.mountdroot 10326 3095 0 Jan 16 - 0:00 /usr/etc/rpc.statdroot 10584 3095 0 Jan 16 - 0:00 /usr/etc/rpc.lockd.

The nfsds are NFS daemons, and the biods are I/O daemons forNFS to provide buffering.

Remember how the servers were mounted to the workstations?

Page 396: Unix 1

Bruce Hunter 2/21/01 page 396

UNIX Networking

UNIX Part I

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 localmachine if the files were mounted locally. They know of the files byway of a file handle passed from the server to the workstation whenthe file was opened.

NFS Problems

The biggest problem that you will ever have with NFS is the networkor the server being down. You will get a stale file handle error mes-sage and perhaps some other console error like YP server notfound. Your SA probably knows already, but be sure.

AFS

AFS is vaguely similar to NFS and is used widely here at Intel. Whileit is more complex in its mounting it is low in its profile. Watch thesecd 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.

Page 397: Unix 1

Bruce Hunter 2/21/01 page 397

UNIX Networking

UNIX Part I

NIS (YP)YP provides a service to all systems that subscribe to it. It givestham maps of everything that YP is set up for. Typically YP providesmaps 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 serversname

46% ypwhichfmsu05

A good example of a necessary service from YP is the password filepasswd. If you were on a system that did not use YP, you would usethe passwd command, but on a YP system you use yppasswd.

Page 398: Unix 1

Bruce Hunter 2/21/01 page 398

UNIX Networking

UNIX Part I

YP Commandsypcat 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 willtake over. Although it is unlikely you will lose all YP servers withoutlosing the net (or at least the file servers), if you do you, will get thismessage

YP server not responding

Page 399: Unix 1

Bruce Hunter 2/21/01 page 399

UNIX Networking

UNIX Part I

Sendmail

/usr/mail is local only unless sendmail is active. What you seeas 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 routingagent to act between the other two agents. This agent is sendmail.

/ucb/mail bin/mail mh elm

sendmail

local (/bin/mail) TCP/IP UUCP

USER AGENTS

ROUTING AGENT

DELIVERY AGENTS

Page 400: Unix 1

Bruce Hunter 2/21/01 page 400

UNIX Networking

UNIX Part I

Do a ps to see sendmail at work.

47% ps -efUSER PID PPID C STIME TTY TIME CMDroot 1 0 0 Jan 16 - 6:52 /etc/initroot 1721 3095 0 Jan 16 - 0:00 /etc/writesrvroot 2032 1 0 Jan 16 - 2:11 /etc/syncd 60root 2740 1 0 Jan 16 - 2:38 /etc/cronroot 3095 1 0 Jan 16 - 0:00 /etc/srcmstrroot 3570 1 0 Jan 16 - 0:00 /usr/lib/errdemonroot 3785 3095 0 Jan 24 - 0:00 /etc/syslogdroot 4644 3095 0 Jan 16 -0:00 /usr/lib/sendmail -bd -q30mroot 5050 1 0 Jan 16 - 0:00 /etc/uprintfdroot 5163 3095 0 Jan 16 - 1:13 /usr/etc/portmaproot 5422 3095 0 Jan 16 - 0:00 /etc/inetdroot 5683 3095 0 Jan 16 - 0:40 /usr/sbin/snmpd

Page 401: Unix 1

Bruce Hunter 2/21/01 page 401

UNIX Networking

UNIX Part I

DNSDomain Name Service resolves host names to Internet addresses.Locally host names are resolved from the file /etc/hosts or YP'shosts maps. An organization with 25,000 nodes would need a25,000 entry-hosts, file and keeping it up to date is impossible.DNS solves the problem by having DNS servers inside and outsidethe local domain (or zone as it is called in DNS) service a request forname resolution to a local resolver.

Note: DNS is the backbone of Windows 2000 networking

Name: fmec.intel.com

com

intel

fmec

flash

ccad

it

oracisco

aol

epg

orgmil

. (dot)

intranet

internet

Page 402: Unix 1

Bruce Hunter 2/21/01 page 402

UNIX Networking

UNIX Part I

Quiz

Describe, briefly, the functions of

NIS

YP

DNS

NFS

What is

a RPC,

a file handle?

ypcat is used to get what?

Page 403: Unix 1

Bruce Hunter 2/21/01 page 403

UNIX Networking

UNIX Part I

Samba and Exceed

Intel’s engineering computing strategy at the user level is to usestrategic infrastructure release systems (SIR boxes) as the baseplatform. These systems run on high-end Intel processors, havelarge memory and local disks, run NT as the local OS and accessUNIX by way of Samba and Exceed. Running UNIX from these sys-tems is seamless and almost indistinguishable from running a free-standing UNIX system.

Page 404: Unix 1

2/21/01 page 404 B Hunter

Using the X-Windows SystemUNIX Part I

X Windows

ECT001118Revised 7/13/99UNIX Part Isection 11

Page 405: Unix 1

2/21/01 page 405 B Hunter

Using the X-Windows SystemUNIX Part I

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 youneed to get your jobs done quickly

Giving you a screen that is comfortable for size, location, color, and font tominimize fatigue.

Page 406: Unix 1

2/21/01 page 406 B Hunter

Using the X-Windows SystemUNIX Part I

Names and Actions

title bar

menu

drag

maximise

iconify

scroll

Page 407: Unix 1

2/21/01 page 407 B Hunter

Using the X-Windows SystemUNIX Part I

History

The X Windows System was started at Stanford and was known as W. Itstechnology was taken by Project Athena at the University of Massachusettsand 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 aworking window.

Page 408: Unix 1

2/21/01 page 408 B Hunter

Using the X-Windows SystemUNIX Part I

The Console Window

The console window is a virtual window for your X session. If you kill thiswindow, 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 trackprocesses.

xlock46% !xxlock47% !xxlock48% !xxlock49%

IconsIcons 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.

console

xterm

Page 409: Unix 1

2/21/01 page 409 B Hunter

Using the X-Windows SystemUNIX Part I

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, orright 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 downthe 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 doubleclicking 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

Page 410: Unix 1

2/21/01 page 410 B Hunter

Using the X-Windows SystemUNIX Part I

The mouse can be used to grab text by selecting the start of the text anddragging over the text until is all selected.

To copy the text to another place, click middle when the cursor is where youwant the text. If you are moving it to an editor, be sure the editor is in appendor insert mode.

Click middle on your mouse to dump text. In NT this is a copy (^C) andpaste (^V) .

The Root Window Menu

You can get a root menu by selecting (click left) in the root menu. Typically itis used to get another window.

Page 411: Unix 1

2/21/01 page 411 B Hunter

Using the X-Windows SystemUNIX Part I

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 Xdirectories:

. /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 pathslooked 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/x11Frwxrwxr-x 1 102 staff 4238 Jul 06 /usr/local/bin/x1142%

Page 412: Unix 1

2/21/01 page 412 B Hunter

Using the X-Windows SystemUNIX Part I

What does this file do? Here are the “punch” lines:

#! /bin/csh -fset XDIR=/usr/bin/X11# op-systems’s X directoryset LXDIR=/usr/local/X11/bin# local X directorysetenv DISPLAY “unix:0”echo “DISPLAY = $DISPLAY”xrdb -load <<XRDBMwm*configFile: ~/.mwmrc.aixXRDB$XINITRC >& /tmp/$USER.x11reset -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

Page 413: Unix 1

2/21/01 page 413 B Hunter

Using the X-Windows SystemUNIX Part I

~/.xinitrc.aix. Here is the basic parts of the script:

#!/bin/cshmwm &sleep 1if (-e $HOME/.bitmap) xsetroot -bitmap $HOME/.bitmap -fg green -bg SteelBlueaixterm -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 theaixterm lines that set up your intial windows. mwm is your window man-ager. It in turn invokes ~/.mwmrc.aix which creates your windows, setstheir characteristics and looks like:

Menu RootMenu

Page 414: Unix 1

2/21/01 page 414 B Hunter

Using the X-Windows SystemUNIX Part I

{“Root Menu” f.titleno-label f.separator“Windows” f.menu WindowsMenuno-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.refreshno-label f.separator“Restart...” f.restartno-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{

Page 415: Unix 1

2/21/01 page 415 B Hunter

Using the X-Windows SystemUNIX Part I

“Iconify” f.minimize“Lower” <Key>F2 f.lower“Raise” <Key>F3 f.raise“Move” f.move“Normalize” f.normalize“Maximize” f.maximizeno-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.minimizeShift<Key>Escape icon|window f.post_wmenuMeta<Key>space icon|window f.post_wmenuMeta<Key>Tab root|icon|window f.next_keyMeta Shift<Key>Tabroot|icon|window f.prev_keyMeta<Key>Escape root|icon|window f.next_keyMeta Shift<Key>Escape root|icon|window f.prev_keyMeta Ctrl Shift<Key>exclamroot|icon|window f.set_behaviorMeta<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 RootMenuMeta<Btn1Down>icon|window f.lowerMeta<Btn2Down>window|icon f.resizeMeta<Btn3Down>window f.move

}

This is where you will do any customizing.

Page 416: Unix 1

2/21/01 page 416 B Hunter

Using the X-Windows SystemUNIX Part I

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?

Page 417: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 417

Getting Help in UNIX

GettingHelp

UNIXWith

UNIX Part I

ECT001118Revised 1/13/99

section 13

Page 418: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 418

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 theSVR4 manual set (in pieces) published by Prentice Hall.

1. titute for a manual set (up to SVR4)

Page 419: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 419

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.

Page 420: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 420

ECEngineering

Computing

Sect. 12

Rev 0ECT001118

11/1/99

Page 421: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 421

What Is EC?

Engineering Computing is the engineering arm of IT. EC provides theresources to do Intel’s technical work. Each major campus has its EC orga-nization. For example Folsom’s is FMEC. Within each local EC organizationare 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 yoursite. For example for the Folsom campus

http://fmec.fm.intel.com

Page 422: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 422

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 thepreferred system is the SIR box. This system boots to NT and provide UNIXaccess 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 giveUNIX 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 thefreeware 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.

Page 423: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 423

SMSSMS 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 usesit, contact your key contact to request adding it to SMS.

Compute ServersThe compute servers are the back-end muscle of our computing. The oldersystems are AIX while the newer ones are HPs. An interesting addition area few hundred Intel systems running LINUX.

Windows 2000?

The base operating system for EC SIR workstations isNT. Not just ordinary NT but our own release includingpublic domain tools like sed, bash, vim, and gawk as wellas a full UNIX interface. It provides access to all of Intelbusiness organizations and communications as well agiving a high-performance front end to UNIX.

Page 424: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 424

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

Page 425: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 425

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 beused 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 foryour Exceed session. When you run Xsession to initiate an Xstart you willsee 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.

Page 426: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 426

Netbatch

Netbatch is a system for queueing jobs on the “best possible” computeserver at that time. A job is submitted to NetBatch. Netbatch queues the jobuntil an appropriate NetBatch machine is available to run it. The STDOUT, ifany, 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

Page 427: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 427

Hummingbird eXceed

Exceed is a UNIX interface from NT. As we set it up it gives a secure loginto the least loaded available UNIX compute server. It provides an X-Windowsession.

SAMBA

SAMBA mounts your UNIX file tree to your NT file tree. UNIX files anddirectories can be reached from NT.

Page 428: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 428

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 inthe same domain.

AFS is a distributed file system allowing a large set of machines to shareaccess to a set of files.

AFS uses a client/-server model, where all the data is stored on file servermachines. Files are transferred to client machines as necessary and cachedlocally, on disk.

AFS Features

ScalableIt can accommodate N * 105 users

SecureUses Kerberos authentication

Flexibility through ACLsMore flexibility than rwx

CachedData accessed many time is cached locally gaining speed and relieving thenetwork

Page 429: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 429

Hardware

Each AFS cell consists of file servers and data-base servers. Our FS to DBserver ratio is approximately 2:1. For example Folsom has 9 file servers and4 data-base servers.

The data-base servers are the home of the Volume Location Data Base orVLDB 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 pathnames 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 intosubsections named volumes. Each volume houses a subtree of related filesand directories.

NFS AFS

one site multiple sites

rwx protection ACL and Kerberos

local replicated

terabytes gigabytes

Page 430: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 430

TokensAFS doesn’t use UNIX UIDs for authentication. To access files which are notworld accessible using AFS, you must have a valid AFS token. You may seewhat 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 usingthe kpasswd or chpasswd commands. The kpasswd change takes effectthroughout the cell immediately. To make your life a little easier use yourUNIX password and change them both art the same time with chpasswd.

Page 431: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 431

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 NTusing 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 theparent 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 adirectory have no effect in AFS but the user bits do have meaning. Thesefile bits may be used to further restrict access to a file that the ACL allowsaccess 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 Forexample, 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

Page 432: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 432

The fs CommandThe general and administration AFS command is fs. This command iseffectively a suite of commands AFS commands are found in the fs manpage. 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

Page 433: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 433

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

Page 434: Unix 1

UNIX Part I

Bruce Hunter 2/21/01 page 434

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 executedmonitors which is used later to determine the action to perform Softwaremonitors are known as modules.

Modules are custom-designed scripts that monitor a particular resource on aTivoli 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. Therules 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 systemreporting the problem. It can send e-mail, page support personnel, and gen-erate trouble tickets.