Unix 1
Unix 1
Unix 1
UNIX I
Welcome to
UNIX
ECT001118
Rev 2
10/1/99
Sect. 1
What is UNIX?
Logging In
The Shells
vi
mail (E-Mail)
Security
EC
SAMBA
eXceed
Each section will build on the one proceeding. For some, it will be all you
ever need to know to get started in UNIX. For most, it is the stepping stone
to UNIX Part II and on to programming in, C and PERL.
The ultimate goal of this course and the other courses in the UNIX and pro-
gramming series is to increase your knowledge and therefore your produc-
tivity in UNIX.
Productivity
In each of the 14 parts of this class attention will be given to the areas that
increase your productivity.
Command chaining
aliases
history
command-line editing
abbriviation expansion in vi
vi shortcuts
Prerequisites
There are no prerequisites for UNIX part but being comfortable with the
keyboard and having knowledge of any operating system is. The material is
comprehensive, the pace fast. Feel free to ask questions at any time.
You will have frequent quizzes, exercises, and labs. They are all geared to
fix the material in you mind and give you the opportunity to try and familiar-
ize yourself with the commands and files.
Conventions
I have used the following type-font conventions to make reading the text
easier.
Courier bold is used for command names, screen output, screen frag-
ments like the “ed editor.”
Italic is used the first time a name is used, for emphasis, and for “fuzzy”
words such as wild cards like /pattern/. It is also used for special key
names like enter for the
Enter
enter key.
What
is
UNIX?
Welcome to
UNIX
Section 2
Rev 7/12/99
multiprocess
threaded (multi-processor)
multi-user
open source
portable
secure
networked
Today UNIX interfaces with NT and shares files through software like
Samba and Exceed.
The Kernel
Drivers
Hardware
The heart of any operating system is the hardware. UNIX surrounds it with
the device drivers. The kernel cannot be contacted directly. It controls the
drivers, the bus or channels, and therefore the hardware. Here’s another
view.
system calls
file systems
front ends
processes control
driver
scheduler
memory management
driver bottom ends
Here is another view of UNIX. System calls are the primitives that allow
higher-level activities to communicate, programmatically, with the kernel.
The scheduler schedules jobs to run on a priority basis. It also sets and
resets the priorities. Each time a job gets a time slice and runs its priorities
are reset.
The I/O systems (buffered and raw) are managed by the kernel and all
device input and output is handled by device drivers.
scheduling
initiation states
swapping
memory allocation
text
stack
heap
devices
inodes
directories
mounts
process control
interrupt handling
The Shell
Net-
File The Kernel work
System
Utilities
The shell is the user’s interface to the kernel and, therefore, UNIX. Also sur-
rounding the kernel are the hundreds of utilities and tools, the network, and
the file systems.
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.
‘76
V6
BSD
‘79
V7
4.2 83 4.3 86
XENIX
SVR2 ‘80
ULTRIX SunOS
‘82
Built-in mail
Built-in networking
NFS
YP
DNS
UNIX arranges its files in a tree hierarchy, like an inverted tree with its root
at the top. The base of every mounted file system is its root. File trees are
mountable.
/ (root)
/bin
/var
/usr mount
point
/spool
/include
/lp /mail
mount path
leaf
/ node
/sys
/errno.h
pwd where am I?
cd change directories
What is the name of the base of every unmounted UNIX file system?
When?
stdin stream 0
stdout stream 1
stderr stream 2
a.out
stdin STDIN
who wc -l
STDOUT stdout
stderr stderr
304% who|wc -l
3
Extending the concept of a pipe, you can redirect the data stream into a file
or a device.
306% who>who.file
307% ls -l who.file
-rw-r--r-- 1 bhunter system 189 Feb 01 14:55
who.file
308% cat who.file
bhunter hft/0 Jan 21 07:32
bhunter pts/1 Jan 21 07:32
bhunter pts/0 Jan 21 07:32
309%
309% sh
$ wc foobarsnarg 2> error.file
$
$ cat error.file
wc: 0653-755 Cannot open foobarsnarg.
$
310% wc -l<who.file
3
A redirection to a file wipes out its former contents. You can save the con-
tents and append to them with the >> symbol for redirect with append.
315% who>who.file
316% echo fmsu04>>who.file
317% rsh fmsu04 who>>who.file
318% cat who.file
bhunter hft/0 Jan 21 07:32
bhunter pts/1 Jan 21 07:32
bhunter pts/0 Jan 21 07:32
fmsu04
shilo ttyp0 Jan 31 16:57 (galadriel)
gsady ttyp3 Jan 28 16:09 (gaffa)
319%
321% sh
$ >who.file
$ wc -w who.file
0 who.file
$
Note that occasionally we have shifted from C shell (%) to Bourne shell($).
Borne shell does things with the data stream that C shell will not.
The UNIX shell is a major concept. The second process created by the ker-
nel as the system comes up is init (the first is the swapper.) init
becomes mother to each log-on shell as each user logs on to the system.
init (PID 1)
getty
login password
shell
When you execute a command your shell creates (forks) another shell just
like it. Only its process ID (PID) number is different.
fork(2)
Actually there is a little more if you consider the systems calls that the kernel
has to make. They are all a part of a process.
sleep
sh wait sh
SIGCHLD
fork
exec exit
What is init?
How many data are streams in a standard shell or process? What are they?
Stream 0
Stream 1
Stream 2
Stack
The text area is fixed; it cannot grow. The stack grows up. The heap grows
down.
The kernel has been described as a maze of tables held together by end-
less code. The scores of tables keep track of what is happening in the ker-
nel and out. The kernel watches and governs everything: files, mounts,
processes, memory, and so on. Many of these kernel tables can be format-
ted by UNIX commands. One of these, ps, formats the process table. The
first processes you see are started by rc when the system boots.
374% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 12:30 /etc/init
root 1721 3095 0 Jan 16 - 0:00 /etc/writesrv
root 2032 1 0 Jan 16 - 4:22 /etc/syncd 60
root 2740 1 0 Jan 16 - 5:09 /etc/cron
root 3095 1 0 Jan 16 - 0:00 /etc/srcmstr
root 3570 1 0 Jan 16 - 0:00 /usr/lib/errdemon
root 3616 3095 0 Jan 31 - 0:01 /etc/syslogd
root 4644 3095 0 Jan 16 - 0:00/usr/lib/sendmail -bd
root 5050 1 0 Jan 16 - 0:00 /etc/uprintfd
root 5163 3095 0 Jan 16 - 1:24 /usr/etc/portmap
root 5422 3095 0 Jan 16 - 0:00 /etc/inetd
root 5683 3095 0 Jan 16 - 1:14 /usr/sbin/snmpd
root 6071 3095 0 Jan 16 - 0:11 /etc/qdaemon
root 6206 3095 0 Jan 16 - 0:14 /usr/etc/ypbind
root 6465 3095 0 Jan 16 - 0:05 /usr/etc/biod 6
root 6724 6465 0 Jan 16 - 0:05 /usr/etc/biod 6
root 6981 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 7238 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 7495 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 7752 6465 0 Jan 16 - 0:04 /usr/etc/biod 6
root 8010 3095 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8267 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8524 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8781 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9038 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9295 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9552 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9809 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 10067 3095 0 Jan 16 - 0:00 /usr/etc/rpc.mountd
root 10326 3095 0 Jan 16 - 0:00 /usr/etc/rpc.statd
The ps command
Any time you need to look at the process
table execute ps from the shell.
376% ps -efl
F S USER PID PPID C PRI NI ADDR SZ WCHAN STIME TTY
TIME CMD
202803 S root 1 0 0 60 20 1004 256 Jan 16 - 1
2:30 /etc/init
240801 S root 1721 3095 0 60 20 45b1 120 1f63cc Jan 16 -
0:00 /etc/writesrv
.
.
bhunter 15160 15961 0 Jan 21 hft/0 0:00 csh -f /usr/local/bin/x11
root 15863 1 0 Jan 16 - 18:24 /usr/local/bin/laserv
bhunter 15961 1 0 Jan 20 hft/0 0:02 -csh
bhunter 16257 19584 0 Jan 21 pts/3 0:00 -sh
Lab
ps flags
The UNIX analogy of pipes and streams is a hydraulic analogy. If data flows
through a stream and you can pipe it, why can’t you filter it? So UNIX has fil-
ters to alter or filter out data. Some of them are
grep
sed
sort
tr
awk
cut
You will take just a brief look at a few filters now and look at them again in
depth later.
Regular Expressions
A regular expression, or RE, is a string that represents any string that it will
match. The RE /foo/ will match foo in foobar or in foofoo. Regular
expressions can be very complex.
/^[a-z]+:[^:]+:[0-9]+:[0-9]+:[a-z ]+:[a-z/]:/bin\/
c?sh$/
In the primitive UNIX editor, ed, you can do a global search on a regular
expression and have it print.
g/RE/p
This was used so often that it became the parent of grep. grep will filter out
any line that contains the pattern you are looking for:
Note that it got the whole line, tabs and all. Now use it in a pipe.
sed is an editor made from the ed editor. It is a streaming editor and it will
edit the data stream.
The df command puts out scores of lines that you don’ t want to see:
s substitute
p print
g global
d delete
Flag
-n run siltent
awk can pattern match, then even better than grep it can perform actions.
awk Flags
-F field delimiter
$1 Karen
$2 Lynn
$3 Bradford
All kinds of things need sorting. sort therefore is the sorting tool for pipe-
lines. It can be used free standing.
tr ’A-Z ’a-z’
d delete
s squeeze
c compliment
tr -d ’0-9’
tr -c ’A-z’ ’\012’
tr -s ’0-9’ ’0-9’
Lab
Take the file /etc/motd and make a dictionary from it. Use tr to separate
the words.
When done, it will look like this. Hint: use the -f flag in sort to get dictio-
nary order.
AIX
bos
file
for
IBM
in
information
lpp
of
Operating
pertinent
Please
README
release
see
System
the
this
to
usr \012 is a newline
Version
Welcome
Taking what you have seen so far let’s automate a task. You need to know
who is using a work server and how many active logins it is supporting. who
is a place to start but it generates data not information.
% who
gnorton pts/0 Jul 13 09:30 (10.1.16.123)
gnorton pts/2 Jul 13 09:41 (10.1.16.123)
gnorton pts/3 Jul 12 10:51 (10.1.16.123)
greddy pts/1 Jul 13 13:27 (10.1.8.106)
skhanzod pts/4 Jul 13 16:53 (10.1.7.129)
skhanzod pts/5 Jul 13 16:53 (10.1.7.129)
skhanzod pts/6 Jul 13 16:53 (10.1.7.129)
skhanzod pts/7 Jul 13 16:53 (10.1.7.129)
greddy pts/8 Jul 14 07:30 (10.1.8.106)
bhunter pts/9 Jun 30 07:43 (10.1.8.138)
jjbennet pts/10 Jul 14 10:27 (10.1.7.138)
bhunter pts/11 Jun 30 12:00 (10.1.8.138)
mmdellen pts/12 Jul 14 08:13 (10.1.12.118)
aguha1 pts/13 Jul 13 14:38 (10.1.7.115)
stbaartm pts/14 Jul 13 10:45 (10.1.7.117)
scfranks pts/15 Jul 13 10:22 (132.233.7.108)
jjbennet pts/16 Jul 14 10:33 (10.1.7.138)
scfranks pts/17 Jul 13 10:22 (132.233.7.108)
szaidi pts/18 Jul 12 11:39 (10.1.7.114)
gnorton pts/19 Jul 13 16:37 (10.1.16.123)
egayles pts/20 Jul 13 17:16 (fri2006)
mmdellen pts/21 Jul 14 08:13 (10.1.12.118)
mmdellen pts/23 Jul 14 08:13 (10.1.12.118)
gnorton pts/22 Jul 12 12:52 (10.1.16.123)
jmaddux pts/26 Jul 14 11:43 (10.1.7.118)
rannamal pts/25 Jul 10 17:34 (10.1.7.178)
szaidi pts/27 Jun 22 16:33 (10.1.7.114)
rannamal pts/24 Jul 10 17:34 (10.1.7.178)
rannamal pts/29 Jul 10 17:34 (10.1.7.178)
aguha1 pts/30 Jul 13 14:38 (10.1.7.115)
rannamal pts/31 Jul 10 17:34 (10.1.7.178)
rannamal pts/32 Jul 10 17:34 (10.1.7.178)
szaidi pts/33 Jun 22 16:33 (10.1.7.114)
% who | wc -l
102
Logging
Into
UNIX
login:
I UNIX Part I
ECT001118
section 3
Last Rev 7/12/99
Everything starts with logging onto the system. When you sit at a UNIX sys-
tem, whether it is a workstation, time- share system, a stand-alone system,
an X terminal, or even an ordinary terminal, you need to log on.
Older-style UNIX systems have the login prompt in the left side of the
screen, usually at the bottom. Otherwise the screen will always be blank.
Graphic systems may have an elaborate login screen with a central display
requesting your logname and password.
When you enter your logname, the system will ask for your password, Your
login name was visible when you typed it, but your password will not be vis-
ible.
login: jrambo
password:
You need a user account to get on to a UNIX system and you will get it from
your local support organization, like Folsom Engineering’s FMEC CSC..
They will assign a login name that uses your first initial and no more than
seven characters of your last name. For example
The SA will want to know what group you work with so she can assign a
group number and home directory. Your manager will also have to sign for
you to accept responsibility for your actions.
You should be given a security booklet, and you will be expected to read
and comply with it.
Your new account’s home directory will be populated with several customi-
zation files. They will be in your home directory and will all start with a dot.
50% cd
51% ls .??*
.Xdefaults .loginx10 .rhosts.O
.Xdefaults.aix .loginx11 .sh_history
.Xdefaults.aix.save .logout .twmrc
.Xdefaults.x10 .mailrc .twmrc.N
.Xdefaults.x11 .mh_profile .twmrc.NEW
.Xdefaults.x11~ .mwmrc .twmrc.ncd
.bash_history .mwmrc.aix .uwmrc
.cshrc .mwmrc.aix.save .xinitrc.aix
.cshrcx10 .netrc .xinitrc.aix.save
.exrc .newsrc .xinitrc.x10
.history .newsrc.bak .xinitrc.x11
.ispell_words .profile
.login .rhosts
Dot Files
There are two that you cannot survive without. They are
.login
and
.cshrc
.profile
for Bourne and Korn shell users. These files set up your environment. Here
are some environment variables:
TERM=xterm
SHELL=/bin/csh
HOME=/usr/system/bhunter
LOGNAME=bhunter
USER=bhunter
Path=/usr/ucb:/usr/bin:/bin:/usr/local/bin:/usr/system/bhunter/
bin:
TZ=PST8PDT
LOCPATH=/usr/lib/nls/loc
EXINIT=map z xp
MANPATH=/usr/local/man
EDITOR=vi
XENVIRONMENT=/usr/system/bhunter/.Xdefaults.aix
XINITRC=/usr/system/bhunter/.xinitrc.aix
DISPLAY=unix:0.0
In time, you will learn how to set and customize most of these.
#!/bin/csh
set mail = /var/spool/mail/bhunter
biff n
stty erase
set noglob; eval ‘tset -Q -s‘; unset noglob
set prompt = "\!% "
set EDITOR = vi
limit coredumpsize 0
set path = ( /usr/ucb /usr/bin /bin /usr/local/bin /usr/system\
bhunter/bin .)
set MANPATH = /usr/local/man
#!/bin/csh
stty -nl
alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
set history = 24
set prompt = ’\!% ’
setenv EXINIT "map z xp"
setenv FMHOME /usr/local/frame/3.0x
set path = ( $FMHOME/bin $path )
setenv MANPATH /usr/local/man
setenv EDITOR vi
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
# super user
You can change shells as you go,
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.
Command Syntax
% ls -ld /usr/system/bhunter
% ls
A few ls flags
or with a flag
-l long
or even two
-t by time
% ls -CF
-d list directories as directories
% ls -lit
and more
% ls -alit
% ls .profile
$ ls -ld /
UNIX file paths are separated with the / character. It is not the \ used by
Win32 and NT.
62% pwd
/usr/system/bhunter
63%
ls -l
There are no file name, file type, file mode trinities as in CMS or mandatory
file extensions as in other operating systems like foo.exe, foo.bat, or
foo.com.
passwd
or
yppasswd
Sol_205GS
chpasswd
All you have to do is type runx to invoke X from an Intel UNIX system.
Once in X Windows, you will see that your path is long and has several X
directories:
This is not the path you have set up in your home dot files. Those paths
looked like:
Quiz
Files
UNIX Part I
section 4
ECT001118
Rev. 7/12/99
They have at least one directory entry and can have more.
A UNIX Directory
They have only two pieces of data for each file, the name and I-node
number.
I-Nodes
I-nodes are also unique to UNIX. They contain all the file information except
for the file’s name.
UNIX arranges its files in a tree hierarchy, like an inverted tree with its root
at the top. The base of every mounted file system is its root. File trees are
mountable.
/ (root)
/bin
/var
/usr mount
point
/spool
/include
/lp /mail
mount path
leaf
/ node
/sys
/errno.h
Going down the tree are child nodes; going up are parent nodes.
parent ..
current .
position
child
child
of
child
The current directory is dot (.) and the parent is dot dot (..).
/dev is home to the device driver entry points, /var where everything that
grows is kept, /usr is a throwback to old UNIX and is a closet for nearly
everything, /etc is the SA’s tool box, /tmp is a temporary storage place
and, /unix is the kernel.
/usr/bin/ls
cd man
or
cd /usr/lib/man
Both are relative paths. To get to where UCB (Berkeley) mail is you can
cd ../ucb
UNIX file systems are mounted. Use the mount command to see what is
mounted.
31% /etc/mount
node mounted mounted over vfs date options
-------- --------------- --------------- ------ ------------ ---------------
/dev/hd4 / jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/hd2 /usr jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/hd9var /var jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/hd3 /tmp jfs Jan 16 07:54 rw,log=/dev/hd8
/dev/lv00 /usr/work jfs Jan 16 07:55 rw,log=/dev/hd8
fmsu01 /misc /misc nfs Jan 16 07:56 rw,hard,bg,intr
fmsu05 /usr/fmo/ssd /usr/fmo/ssd nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /usr/spool/mail /usr/spool/mail nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /amoeba /mproj/amoeba nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /sam16u3 /uproj/sam163 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu05 /samgr /gproj/sam16 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /g16v1 /vproj/sam161 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu05 /g16v /vproj/sam16 nfs Jan 16 07:56 rw,hard,bg,intr
fmsu01 /sam16u2 /uproj/sam162 nfs Jan 16 07:56 rw,hard,bg,intr
.
.
or the df command
32% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 928 77% 864 84% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1444 88% 343 8% /var
/dev/hd3 8192 7880 3% 29 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 149786 81% - - /misc
fmsu05:/usr/fmo/ssd 944238 83861 91% - - /usr/fmo/ssd
fmsu01:/usr/spool/mail 408391 290661 28% - - /usr/spool/mail
fmsu01:/amoeba 408399 247000 39% - - /mproj/amoeba
fmsu01:/sam16u3 1893612 486009 74% - - /uproj/sam163
fmsu05:/samgr 917358 73783 91% - - /gproj/sam16
fmsu01:/g16v1 1836252 746162 59% - - /vproj/sam161
.
.
Use the pwd (print working directory) command to see where you are (in a
UNIX file structure).
19% pwd
/usr
20% cd lib
21% pwd
/usr/lib
22% ls
INnet lex libodm.a nmf
Mail.rc lib.b liboldX.a nterm
X11 lib300.a libplot.a objrepos
acct lib300S.a libpp.a phones-file
aliases lib300s.a libprint.a profiled
asw lib4014.a libqb.a ps
bind lib450.a librpcsvc.a pse.exp
boot libIM.a librts.a qm.exp
bugfiler libMrm.a libs.a ras
bugformat libPW.a libsm.a refer
calprog libSIMshr.a libsnmp.a remote-file
cfgodm.ipl libUil.a libsrc.a sa
cflow1 libX11.a libsys.a security
cpp libXdmcp.a libtermcap semutil
crt0.o libXext.a libtermcap.a sendmail
dag libXfx.a libtli.a sendmail.cf
deroff libXm.a liby.a sendmail.
If you have to leave your current directory for a little while and then come
back ...
bash-2.00$ pwd
/usr/users/a5fs/bhunter/weeklys
bash-2.00$ pushd /fes/bhunter
/fes/bhunter ~/weeklys
bash-2.00$ pwd
/fes/bhunter
bash-2.00$ ls
C NT PERL class.foils
bash-2.00$ popd
~/weeklys
27% cd ..
28% pwd
/usr/system
The ls command gives a listing, but it has dozens of ways to do it. Most
UNIX commands have flags, and ls is no exception.
command_name -flag
33% cd /usr/include
34% ls -l
total 1848
drwxr-xr-x 2 bin bin 512 Dec 14 1992 DPS
drwxr-xr-x 2 bin bin 512 Dec 14 1992 IN
lrwxrwxrwx 1 root system 24 Dec 14 1992 Mrm -> /usr/
lpp/X11/include/Mrm
-r--r--r-- 1 bin bin 630 Dec 14 1992 NLchar.h
-r--r--r-- 1 bin bin 4156 Dec 14 1992 NLctype.h
-r--r--r-- 1 bin bin 51772 Dec 14 1992 NLregexp.h
-r--r--r-- 1 bin bin 929 Dec 14 1992 NLxio.h
lrwxrwxrwx 1 root system 24 Dec 14 1992 X11 -> /usr/
lpp/X11/include/X11
lrwxrwxrwx 1 root system 23 Dec 14 1992 Xm -> /usr/
lpp/X11/include/Xm
-r--r--r-- 1 bin bin 768 Dec 14 1992 a.out.h
-r--r--r-- 1 bin bin 2212 Dec 14 1992 acl.h
-r--r--r-- 1 bin bin 439 Dec 14 1992 aio.h
.
35% ls -F
DPS/ dbug.h isode/ nlist.h syms.h
IN/ dbxstclass.h jcode.h notice.h sys/
Mrm@ defenv.h jctype.h odmi.h sysexits.h
NLchar.h diag/ jfs/ paths.h syslog.h
NLctype.h dirent.h langinfo.h pcsim@ tar.h
NLregexp.h dumprestor.h ldfcn.h piostruct.h tcpip_audit.h
NLxio.h errno.h limits.h poll.h term.h
X11@ exceptab.h linenum.h procinfo.h termio.h
Xm@ execargs.h loader.h protocols/ termios.h
a.out.h fatal.h locale.h pwd.h time.h
acl.h fcntl.h login.h regex.h tiuser.h
aio.h filehdr.h lprio.h regexp.h ttyent.h
aixfont.h float.h lvm.h reloc.h typchk.h
aixif/ fnmatch.h lvmrec.h resolv.h uil@
aouthdr.h fp.h macros.h rpc/ uinfo.h
ar.h fp_fort_c.f malloc.h rpcsvc/ ulimit.h
arpa/ fp_fort_t.f math.h scnhdr.h unctrl.h
asl.h fptrap.h mbstr.h search.h unistd.h
/ a directory
* an executable
@ a link
Time can be saved by moving around the file system quickly and effort-
lessly. Relative path names are faster than absolute path names. ~ saves
time.
Which is faster?
cd /usr/users/a5fs/bhunter/perl
or
cd ~/perl
Use other directories constantly? Put them in your environment then use
them.
How do you find out more about ls or any command? You do a man(ual) on
it.
69% man ls
ls Command
Purpose
Syntax
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 ... ]
ls -f [ -d ] [ -i ] [ -s ] [ -C | -m | -x |
] [ Directory ... ]
Description
Here is some information, but keep going until you see the key word Flags.
Flags
The page goes on, of course. Send it to the printer if you need a paper copy
.
Here are some of the signals, line kills, etc taken from one system’s stty
command.
intr = ^C
quit = ^\
erase = ^H
kill = ^U
eof = ^D
eol = ^@
eol2 = ^@
start = ^Q
stop = ^S
susp = ^Z
dsusp = ^Y
reprint = ^R
discard = ^O
werase = ^W
lnext = ^V
UNIX uses all of the keyboard characters other than the alpha and numerics
to do special things. A ^D will end mail, a ^C will kill most programs, a ^Z will
stop a program, a newline will enter a command, a ~ will trigger mail into
acting on a command, a ! will let vi execute a shell command, a ! triggers
C shell into looking at history — the list goes on and on. Characters like this
are called metacharacters.
325% ls *.h
inode.h
326% ls *.c|wc -l
235
327% ls [st]*.c
s.c sscan.c stcmp.c structa.c time.c
s10.c ssr.c sti.c structp.c timeseq.c
s10_10.c sst.c str.c subs.c top.c
seek.c st.c strcopy.c substring.c true.c
seekOLD.c st2.c stringi.c swap.c twoD.c
simpstruct.c st_a.c strings.c sys.c
srt.c st_funct.c struct.c tD.c
ss.c statauto.c struct2.c testi.c
328%
328% ls st*.c
st.c st_funct.c sti.c stringi.c struct2.c
st2.c statauto.c str.c strings.c structa.c
st_a.c stcmp.c strcopy.c struct.c structp.c
329%
329% ls [A-Z]*
README TD.c
330%
279% cd
280% cd junk
281% mkdir snarg
282% ls -ld snarg
drwxr-xr-x 2 bhunter system 512 Feb 01 13:44 snarg
283% date
Tue Feb 1 13:44:52 PST 1994
284%
rmdir
291% rm ./snarg/bar
292% rmdir snarg
293%
Note: with UNIX commands no news is good news. Errors are reported
immediately, success is taken for granted.
What’s on a directory?
What’s in a file?
What’s in an inode?
What is the name of the base of every unmounted UNIX file system?
How would you get the file size, make time, and file names of all the .c files
in /usr/system/source?
Lab
cd to /usr/include.
List all the header files that start with an uppercase letter. [A-Z]*
List only header files that start with the letters u through z. [u-z]*
There must to be a hundred ways to create a file in UNIX. Files are made by
touch
applications
the compilers
452% cat>nitt
Now is the time
for all good men
to come to the aid
of their country.
^D
Did it work?
ed
edit
red
ex
Simple editors:
pico
xedit
Streaming editors:
Heavy-duty editors:
vi
emacs
vi is the visual editor and is the de facto UNIX editor. To start vi. type in vi
followed by the file name.
% vi foo
Type an a or an i to get started, then type your file. (: don’t make mistakes :)
Now type esc (escape), then :
Type in wq and you’ll write and quit the editor. You will learn (much, much)
more later.
% mv ~/foo /tmp
will move the file foo from your $HOME to /tmp with the same name.
~
"bar" [New file] 4 lines, 10 characters
461% mv bar /tmp
462% ls /tmp
aixfile mpeng.x11 xresources.19054.bak
bar xresources.14543.bak xresources.19129.bak
bhunter.x11 xresources.16629.bak xresources.21812.bak
err.out xresources.18053.bak
errlog xresources.18348.bak
463% rm /tmp/bar
464%
466% ls
a.file bs ione rev true.f
w.3.backup
adduser.out bu nitt reverse truef
app find90 perl.f snarg vi.fmt
bhh.shell foo r t w.3
294% cd snarg
295% touch foo bar snarg
296% ls
bar foo snarg
297%
297% rm snarg
298% ls
bar foo
299% rm bar
300% ls
foo
301% rm foo
302% rm foobar
rm: foobar: A file or directory in the path name does
not exist.
303%
In almost any operation system a removed file is
gone! Some allow them to be restored with spe-
cial utilities if the file structures haven been
changed (much.) UNIX would allow a file to be
reconstructed but one problem, a multi-process
system like UNIX and NT never sits still and
quickly recovers and reuses the space the file
previously occupied.
rm *.c
you would have no more C code. It’s a good way to get out of supporting the
code:)
341% ls
bar.c error.file foo.h who.file
bar.h foo.c snarg.c
Now let’s type real fast and get rid of those C files.
343% rm * .c
rm: .c: A file or directory in the path name does not
exist.
344% ls
345%
myprog>*
rm *
will wipe out everything. Now let’s take everything we know and apply it. Do
an
rm -i ?
remove * ?
You have a huge project directory, your project is done, and you have had
everything archived to tape. Its time to get your disk space back.
% cd projdir
% pwd
/u/hugeproj/projdir
% rm -rf mydir
When the smoke clears, mydir is just a memory, even if it had 300 meg in it
just a while back.
If your are in a training login create a directory under your own name.
Move to your directory and redirect the man page for true into a file called
true.f.
Make a directory called lab from the directory you just created.
cd to lab.
Remove snarg.
Remove lab.
Frequently you have to deal with files that may be the same, or may not.
How do you know? Your first indication is the character count from ls -l of
wc. There are two files, true.f and truef. They were named from the
true man page. Try ls.
514% ls -l true*
-rw-r--r-- 1 bhunter system 837 Dec 30 08:51 true.f
-rw-r--r-- 1 bhunter system 839 Feb 03 08:00 truef
515%
515% wc true*
47 114 837 true.f
47 114 839 truef
94 228 1676total
516%
cmp tells you if two files are different. It’s a binary answer; they are or are not
the same. The syntax is
cmp tells you that line 4, the 33rd byte, is different. Now use vi and set
number.
When the diff command is run on regular files, and when comparing text
files that differ during directory comparison, the diff command tells what
lines must be changed in the files to make them agree. The syntax is
The < tells you the line is in file1; the > is file2.
The name and I-node number of the file are stored in the directory. That is
all the information there; there is no more. You can see a directory’s con-
tents with od.
349% pwd
/usr/system/bhunter/junk/snarg
350% od -xc .
0000000 0533 2e00 0000 0000 0000 0000 0000 0000
005 3 . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0 \0
0000020 8100 2e2e 0000 0000 0000 0000 0000 0000
\0 . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0
0000040
351% ls
352%
There is almost nothing there (there is . and ..)! Now add a new file and
look at it. The file you see is a link. Note the I-node numbers. foo’s is
0XA053A which is 656698 in base 10.
357% ls -l fd*
brw-rw-rw- 1 root system 11, 0 Jan 24 14:10 fd0
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0.18
brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 fd0.9
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 fd0h
brw-rw-rw- 1 root system 11, 2 Dec 14 1992 fd0l
Note the link count of 2 on fd0.18 and fd0h. Could they be the same
device? Let’s use the -i (I-node) flag of ls to find out.
% ln foo bar
Sometimes a file or a directory isn’t where you think it is. A link is created
to make a directory entry that points to it. On many distributions perl is in /
usr/local/bin, but it appears to be in /usr/bin. Note: on EC systems it
is in /usr/intel/bin.
370% ls -l /usr/bin/perl
lrwxrwxrwx 1 root system 19 Dec 14 1992
/usr/bin/perl -> /usr/local/bin/perl
371%
Links are created with the ln command. The syntax for a soft link is
ln -s real_file link_name
A user uses netstat often. She wants to execute it without the long path
name.
ln -s /usr/etc/netstat ~/bin/netstat
Now with ~/bin in her $PATH, she need never have to type the path name
(for that command) again.
Before you attempt to look at an existing file, you may want to figure out
what kind of file it is. The command is file.
49% pwd
/usr/system/bhunter/c.c
50% file README ab.c a.out
README: ascii text
ab.c: c program text
a.out: executable (RISC System/6000 V3.1
or object module not stripped
51%
51% cd
52% cd scripts
53% file *
2args: commands text
EWS: ascii text
alchk: commands text
args: English text
articl: [nt]roff, tbl, or eqn input text
base8: commands text
cleanx: commands text
deroff: ascii text
dmon: commands text
dmonb: commands text
enter: ascii text
etr: c program text
.
.
rwall: commands text
sed: directory
shf: English text
subst: commands text
t: empty
th: commands text
until: ascii text
yorn: ascii text
54%
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.
wc
It’s great if the file is not too long. There is always more for longer ones.
But first of all, how long is the file? wc, word count gives the length of a file in
characters, lines, and words. Here the -w (word count) flag is used.
265% wc -l PLAN
207 PLAN
266%
Class Type
Interactive with multiple labs
Outline
What Is UNIX?
Fully networkable
UNIX at Berkeley
--More--(26%)
You will move forward a line if you hit enter in more. Strike the space bar
and you move a page. Enter Q and you’ll quit.
more flags
-w Keeps the current file open, even when it reaches the EOF .
+number
Starts at the line specified by the number.
+g Displays the last screen in the file and allows you to scroll
backward through the file rather than forcing an exit.
K Spacebar
Displays K more lines of text when the space bar is pressed.
!command or %command
Invokes a shell.
K:p Skips to the Kth previous file given in the command line.
The most intuitive way to read a long file is with vi. vi can be invoked read
only with
vi -R
tail
NFS internals
using NFS
YP internals
using YP cat
270%
$ cd
$ cd c.c
$ od a.out
0000000 000737 000007 026511 065606 000000 005022 000000 000062
0000020 000110 010002 000413 000001 000000 001020 000000 000104
0000040 000000 000020 000000 000050 000000 001000 000000 000000
0000060 000000 000060 000004 000002 000004 000004 000007 000005
0000100 000002 000003 030514 000000 000000 000000 000000 000000
0000120 000000 000000 000000 000000 000000 000000 027160 060544
0000140 000000 000000 000000 000000 000000 000000 000000 000024
0000160 000000 000754 000000 000000 000000 000000 000000 000000
0000200 000000 000010 027164 062570 072000 000000 000000 001000
0000220 000000 001000 000000 001020 000000 001000 000000 004442
0000240 000000 000000 000015 000000 000000 000040 027160 060544
0000260 000000 000000 000000 000000 000000 000000 000000 000760
0000300 000000 002020 000000 000000 000000 000000 000000 000000
0000320 000000 000010 027144 060564 060400 000000 000000 000000
0000340 000000 000000 000000 000104 000000 003000 000000 004644
0000360 000000 000000 000013 000000 000000 000100 027142 071563
0000400 000000 000000 000000 000104 000000 000104 000000 000020
$ od -x a.out
0000000 01df 0007 2d49 6b86 0000 0a12 0000 0032
0000020 0048 1002 010b 0001 0000 0210 0000 0044
0000040 0000 0010 0000 0028 0000 0200 0000 0000
0000060 0000 0030 0004 0002 0004 0004 0007 0005
0000100 0002 0003 314c 0000 0000 0000 0000 0000
0000120 0000 0000 0000 0000 0000 0000 2e70 6164
0000140 0000 0000 0000 0000 0000 0000 0000 0014
0000160 0000 01ec 0000 0000 0000 0000 0000 0000
0000200 0000 0008 2e74 6578 7400 0000 0000 0200
0000220 0000 0200 0000 0210 0000 0200 0000 0922
0000240 0000 0000 000d 0000 0000 0020 2e70 6164
0000260 0000 0000 0000 0000 0000 0000 0000 01f0
0000300 0000 0410 0000 0000 0000 0000 0000 0000
0000320 0000 0008 2e64 6174 6100 0000 0000 0000
0000340 0000 0000 0000 0044 0000 0600 0000 09a4
0000360 0000 0000 000b 0000 0000 0040 2e62 7373
0000400 0000 0000 0000 0044 0000 0044 0000 0010
Don’t start a big job if you have nowhere to put the results! First see if you
have any room on the disk with df.
255% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 928 77% 864 84% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1440 88% 343 8% /var
/dev/hd3 8192 7868 3% 30 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 149781 81% - - /misc
fmsu05:/usr/fmo/ssd 944238 84130 91% - - /usr/fmo/ssd
fmsu01:/usr/spool/mail 408391 290868 28% - /usr/spool/mail
fmsu01:/amoeba 408399 247002 39% - - /mproj/amoeba
fmsu01:/sam16u3 1893612 468626 75% - - /uproj/sam163
fmsu05:/samgr 917358 44177 95% - - /gproj/sam16
fmsu01:/g16v1 1836252 746162 59% - - /vproj/sam161
fmsu05:/g16v 1278413 77031 93% - - /vproj/sam16
fmsu01:/sam16u2 1835996 572904 68% - - /uproj/sam162
fmsu01:/sam16u1 917870 76799 91% - - /uproj/sam161
fmsu05:/sam16u 1968100 185982 90% - - /uproj/sam16
fmsu05:/sam16 1278413 141670 88% - - /mproj/sam16
fmsu01:/sam16m1 917870 48603 94% - - /mproj/sam161
fmsu05:/g16 1278413 587612 54% - - /gproj/sam161
fmsu05:/samgr2 1298653 423523 67% - - /gproj/sam162
fmsu05:/e2 944238 739466 21% - - /e2
fmsu01:/28u008s 508958 93717 81% - - /gproj/28u008s
fmsu05:/usr/mfg 638430 35947 94% - - /usr/mfg
The key is to look for % used. The size is under Total KB.
Use the du command to see where all the space has gone within a mounted
file system .
256% cd
257% du
120 ./bin
4 ./LAN
352 ./mail
18 ./p-h
16 ./AIX
10 ./ez
4 ./lpr
2 ./local/bin
4 ./local/local/bin
6 ./local/local
10 ./local
.
.
26 ./stalker
2 ./root_owned
2 ./hunter
2 ./restore
56 ./x400
1790 ./intro
36 ./weeklys/93
54 ./weeklys
8 ./travel
8 ./chandler
12 ./justification
18 ./c.class.comments
22 ./sed
41012 .
258%
The 41012 . is the home directory total with 41 K blocks — that’s 20 Meg!
Looking at text?
The bottom?
Lab
Find the command true. What kind of file is it? Put it on the screen if you
can.
Find from. Are the any strings in it? What are they?
If you are having any problem finding a file don’t waste your time. Use the
find command and let the machine do the looking for you.
Are two files the same or different? Use diff to find out.
df your file system before taking on a large disk intensive task. You may not
have enough room so find out before wasting the computer’s, the network’s
and your time.
The Shells
tcsh
csh
sh
ksh
zsh
bash
pdksh
UNIX Part I
Section 5
Rev 7/12/99
ECT001118
Shell?
The UNIX shells are both your command interpreter and a programming
language as well. They carry your environment and pass it on to other pro-
cesses. They offer:
rsh
Ease of use
Job Control
h
cs
Command linkage
bash
Automated start up of other automated tasks
Considerations for your choice of which shell to use at any specific time:
Speed
Features
You have a log-in shell and can change your current shell at any time.
pdksh
h
ks
tc
zsh
sh
Conventions
This class will deal with three of the shells. When looking at examples, you
will be able to tell the difference by the prompts.
% C shell, tcsh
The shell also has escapes. They commonly are set to:
These are not uniform from one UNIX version to another and are change-
able by the user.
In this section you will learn about the shell and shell process, its environ-
ment, and its environmental variables. You will learn how to set shell vari-
ables and what the environmental variables do for you. You will be exposed
to Bourne, Korn, and C shells. You will even get into shell programming.
Here more than any one place are the keys to productivity.
Globbing
Here are a few usage examples. First the * as a filler for several characters.
162% ls b*.c
bell.c bit.c bitA.c bit_a.c bita.c bitf.c
bitup.c bufio.c
163%
163% ls ???.c
arg.c c_l.c fib.c get.c ln1.c ln4.c s10.c sst.c str.c
ati.c ch5.c fin.c idx.c ln2.c max.c srt.c st2.c sys.c
bit.c fac.c for.c lab.c ln3.c odd.c ssr.c sti.c top.c
164%
164% ls *.h
inode.h
168% !165
ls inode.[ch]
inode.c inode.h
169%
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
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
Now for ranges. Get files with names starting with the first half of the lower-
case and all the uppercase files as well.
174% ls [A-Za-l]*
README bitf.c error.c flag.c inode.h
TD.c bitup.c ex15.c flags.c io.c
ab.c bufio.c ex21.c for.c jump.c
arg.c c_l.c ex23.c fout.c jumpt.c
argold.c case.c ex25.c functproto.c lab.c
ascii.c case_lab.c ex27.c get.c letter.c
asum.c cast.c f.c gstring.c ln1.c
ati.c ch5.c fac.c h.c ln2.c
bell.c char_val.c fapp.c hello.c ln3.c
bit.c define.c fib.c idx.c ln4.c
bitA.c dowhile.c fiban.c idxp.c lname.c
bit_a.c echo.c fibon.c infunct.c
bita.c envp.c fin.c inode.c
175%
And now the other half of the lower-case alphabet plus the numbers.
176% ls [m-z0-9]*
020494 porrage.c seekOLD.c str.c testi.c
malo.c pound.c simpstruct.c str.o time.c
max.c pr.c srt.c strcopy.c timesequal.c
max2.c prbarry.c ss.c stringi.c top.c
me.c prptr.c sscan.c strings.c true.c
nexts.c prt1.c ssr.c struct.c twoD.c
odd.c psum.c sst.c struct2.c uf.c
onesc.c ptrtostruct.c st.c structa.c ufp.c
orperms.c recoil.c st2.c structp.c userfunct.c
pay_lab.c reply.c st_a.c subs.c usrf_ret.c
payroll.c s.c st_funct.c substring.c w_struct_sun.c
perms.c s10.c statauto.c swap.c while.c
pl.c s10_10.c stcmp.c sys.c write_struct.c
po.c seek.c sti.c tD.c
Quiz
a fast shell?
command compatibility?
Give the full command line to see only perl code (.pl) in /usr/local/
src/perl
The line
$ ls [aieou]*.c
You just went from csh to sh and did some work. Now you want to go back.
How?
434% env
TERM=xterm
SHELL=/bin/csh
HOME=/usr/system/bhunter
LOGNAME=bhunter
USER=bhunter
PATH=/usr/ucb:/usr/bin:/bin:/usr/local/bin
TZ=PST8PDT
LOCPATH=/usr/lib/nls/loc
LANG=En_US
EXINIT=map z xp
FMHOME=/usr/local/frame/3.0x
MANPATH=/usr/local/man
EDITOR=vi
XENVIRONMENT=/usr/system/bhunter/.Xdefaults.aix
XINITRC=/usr/system/bhunter/.xinitrc.aix
DISPLAY=unix:0.0
WINDOWID=12582925
TERMCAP=co#80:li#24:
You can set your own environmental variables to help speed up your work.
FMHOME=/usr/system/sframe; PATH=:.:$FMHOME/bin:$PATH;
export FMHOME PATH
WORK=/fes/bhunter
export WORK
These environmental variables are the only variables that the shell automat-
ically passes to each program that it starts.
Environmental Variables
For any one user or environment, there may be more, or fewer, but these
are typical.
/etc/passwd
The shell gets its first bits of information (environmental variables) from the
file /etc/passwd (or the YP version or part of the login process.) Here is
one passwd record:
bhunter:rzojv8/Uf2q92:100:0:Bruce H Hunter:
/usr/system/bhunter:/bin/csh
LOGNAME bhunter
UID 100
GID 0
HOME /usr/system/bhunter
SHELL /bin/csh
while ( envp[i][0] )
puts (envp[i++]);
}
Preset Variables
noglob
Here is the output of the compiled C program. It looks like the output of the
env command, doesn’t it?
485% a.out
TERM=xterm
SHELL=/bin/csh
HOME=/usr/system/bhunter
LOGNAME=bhunter
USER=bhunter
PATH=/usr/local/frame/3.0x/bin:/usr/local/frame/3.0x/
bin:/usr/
local/frame/3.0x/bin:/usr/ucb:/usr/bin:/bin:
/usr/local/bin:/usr/system/bhunter
/bin:/usr/bin/X11:/usr/local/X11/bin:/usr/lpp/X11/bin:
/usr/lpp/X11/Xamples/bin:.
TZ=PST8PDT
LOCPATH=/usr/lib/nls/loc
NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/prime/
%N
ODMDIR=/etc/objrepos
LANG=En_US
EXINIT=map z xp
FMHOME=/usr/local/frame/3.0x
MANPATH=/usr/local/man
EDITOR=vi
XENVIRONMENT=/usr/system/bhunter/.Xdefaults.aix
XINITRC=/usr/system/bhunter/.xinitrc.aix
DISPLAY=unix:0.0
WINDOWID=12582925
TERMCAP=co#80:li#24:
You can create ordinary (non-environmental) variables on the fly. These are
not part of the shell’s ENV.
443% set
argv ()
cwd /usr/system/bhunter/intro
hi Hello
history 24
home /usr/system/bhunter
path /bin /usr/ucb /usr/bin /bin /usr/local/bin
prompt !%
shell /bin/csh
status 0
term xterm
there world
user bhunter
Setting Variables
Variables are set differently in ksh and sh than they are in csh
$ a=1234
$ b=5678
$ c=90
$ echo $a$b$c
1234567890
$ echo $a
1234
$
467% set a = A
468% echo $a
A
As you have already seen, a variable can be set and kept in the shell’s envi-
ronment. We say that they are exported.
export
In sh you export a variable so all of its children will receive it as part of the
ENV.
TERM=VT100
export TERM
setenv
In csh you do the same thing for environmental variables with setenv.
Quiz
HOME
PATH
path
noglob
TERM
Lab
1) Set a, b, c, and d to 1 - 4.
No one shell is “better” than another. High points of Bourne shell are
A history mechanism
Aliases
All the new shells have C shell’s features plus much more.
Korn, pdksh, zsh, and bash have all the features of sh and csh with no
disadvantages., They are fast, and have fantastic command line editing,
know ~, file name and command completion, integer manipulation, as well
For example type in just enough of the name to show it is unique and the
shell will complete it. To go to the directory weeklies type
cd ~/we->
History
If you were in vi editing a file myprog.pl a few commands back just type
!v
vi myprog.pl
The up and down arrow keys will move forward and back through history.
Command-line editing
To edit the command line use the right and left arrow keys to walk through
the line, delete will remove a character, and typing a character will insert it.
In all these shells ^Z will suspend a job, fg will foreground it and bg will
background it. For example if you had just suspended a foreground job, and
it was the only one under job control,
bg %1
sh
Old, command compatible, fast, feature poor
csh
Old, slow, fewer features that the newer shells, UNIX command returns
incompatible with csh.
ksh
Fast, more features. Portability issues with free UNIX.
command completion
aliases
command-line editing
Switching Shells
All you have to do to change shells is invoke one from the other.
486% sh
$
$ ^D 487%
Killing a shell
To kill the new shell, hit it with a end-of text signal (^D) or try logout or
exit.
Lab
cd to your home directory. Change to Bourne shell. cd to /tmp. Kill your
Bourne shell (^D) and run pwd. Where are you?
Where and when are your shell variables set? A few are set by the login pro-
cess and passed to your login shell by init. Most are set in your start-up
files like
.profile sh ksh
.bashrc bash
.login csh
.cshrc csh
.x* X
487% cd
488% ls .??*
.Xdefaults .loginx11 .profile
.Xdefaults.aix .logout .rhosts
.Xdefaults.aix.save .mailrc .rhosts.O
.Xdefaults.x10 .mh_profile .sh_history
.Xdefaults.x11 .mwmrc .twmrc
.Xdefaults.x11~ .mwmrc.aix .twmrc.N
.bash_history .mwmrc.aix.save .twmrc.NEW
.cshrc .netrc .twmrc.ncd
.cshrcx10 .newsrc .uwmrc
.exrc .newsrc.bak .xinitrc.aix
.history .nfs3B2
.ispell_words .nfs79 .xinitrc.x10
.login .nfs89 .xinitrc.x11
.loginx10 .nfs99
.nn:
LAST NEXTG select select.bak
489% ls .??*|wc -l
47
490%
.profile
The Bourne, bash, pdksh and Korn shells get their environments from
.profile when you log in. Here is a typical .profile.
PATH=/usr/ucb:/bin:/usr/bin:/usr/local/bin:/usr/intel/
bin
MAIL=/usr/spool/mail/$USER
export TERM MAIL PATH
biff n
# FMHOME line added by the FrameMaker setup program
FMHOME=/usr/system/sframe
PATH=:.:$FMHOME/bin:$PATH
export FMHOME PATH
PATH is your shell’s search path. MAIL is where your mail is stored, and
biff gives mail notification. tset sets the terminal characteristics.
sh files
.login
C shell executes .login just once, when you log on. This script is more or
less typical.
#!/bin/csh
set mail = /var/spool/mail/bhunter
biff n
stty erase ^H
set prompt = "\!% "
set EDITOR = vi
limit coredumpsize 0
set path = ( /usr/ucb /usr/bin /bin /usr/local/bin
/usr/system/bhunter/bin .)
set MANPATH = /usr/local/man
.cshrc
The file .cshrc is read (and reread) every time you fork a new shell, which
is just about every time you hit a carriage return from the terminal’s com-
mand line.
#!/bin/csh
alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
set history = 24
set prompt = ’\!% ’
setenv EXINIT "map z xp"
setenv FMHOME /usr/local/frame/3.0x
set path = ( $FMHOME/bin $path )
setenv MANPATH /usr/local/man
setenv EDITOR vi
C Shell Files
$HOME/.login is read by the login shell before the .cshrc file at login.
/etc/passwd contains the source for home directories, UDI, GID, and
the shell for the file parameter.
ksh Files
bash Files
/bin/bash
~/.bashrc
/etc/profile
~/.profile
When an interactive shell that is not a login shell is started, bash reads and
executes commands from ~/.bashrc, if that file exists. When invoked as a
login shell, bash first attempts to read and execute commands from /etc/
profile and then ~/.profile then reads and executes commands from
~/.bashrc.
zsh files
$ZDOTDIR/.zshrc
$ZDOTDIR/.zlogin
$ZDOTDIR/.zlogout
${TMPPREFIX}* (default is /tmp/zsh*)
/etc/zshenv
/etc/zprofile
/etc/zshrc
/etc/zlogin
/etc/zlogout
Quiz
Does the parent shell know about its child’s new variables?
When the Bourne shell reads command lines from a string between two
grave accents (‘‘), the standard output from these commands may be used
to set a variable. For example
nusers=‘who|wc -l‘
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
Examples:
sh "cp $f ${f}.bak"
n=‘who | wc -l‘
Quiz
$ a=don\t ; echo $a
sh Parameter Substitution
Shell variables are dereferenced, that is, the variable is read from its place
in storage when proceeded by a $.
echo $HOME
echo $1
will print the first command-line argument passed to a shell. They are used
in scripts. Positional parameters may be assigned values by set in csh or
simply with a = in sh. Keyword parameters are assigned values:
name=value
${parameter}
The value, if any, of the parameter is substituted. The braces are required
only when parameter is followed by a letter, digit, or underscore that is not to
be interpreted as part of its name. It is used to clarify a variable or prevent
possible confusion. Here the character b, not the variable b, is concate-
nated to the variable a and stored in str.
a=1
b=2
str=${a}b
$ ls
PLAN mail.doc shell.doc.lck
$ echo $?
0
$
:
(
date
whoami
tty
hostname
)>$0.$$
The execution
Shell Scripts
The following parameters are used by the sh shell and passed to pro-
grams.
sh Blank Interpretation
The line
is read as a command followed by just two arguments. In use grep will take
now is the time and pass it to grep as an RE so grep will scan the file
foo.file for it.
UNIX can redirect the data stream. Every UNIX process has three open
stream minimally:
The numbers are file descriptors and are used in conjunction with the redi-
rection metacharacters.
sh redirection
> redirect to
These direct the data stream to or from files (or devices). sh also has these
<&digit
Use the file associated with file descriptor digit as standard input. Similarly,
it’s used for the standard output using >&digit.
<&- The standard input is closed. Also the other way using >&-.
If any of these is preceded by a digit as well, the file descriptor which will be
associated with the file is that of the digit (instead of the default 0 or 1).
For example
... 2>&1
associates file descriptor 2 with the file currently associated with file descrip-
tor the STDERR joins STDOUT so
testprog 2>&1
The order in which redirections are specified is significant. The shell evalu-
ates redirections left-to-right. For example
first associates file descriptor 1 with file bar. It associates file descriptor 2
with the file associated with file descriptor 1 (namely, file bar). If the order of
redirections are reversed, file descriptor 2 is associated with the terminal
(assuming file descriptor 1 had been) and file descriptor 1 is associated with
file bar.
157% sh
$ cat foo bar 1>good+bad 2>&1
$ cat good+bad
In C shell you can combine STDOUT and STDERR and send both to one file a
little more easily. Again, you have a file foo, but no bar, so an error will
generate.
Redirection Examples
551% date>>date.f
The results:
A classic:
Here the STDERR is captured into an error file, while the STDOUT is dis-
played.
$ cat error.f
cat: 0652-050 Cannot open bar.
csh set
syntax:
set [ -aefhkntuvx- [ argument ... ] ]
As an example
set -x
#!/bin/sh -x
echo "Name : "
read name
dir=`pwd`
echo "$name, you are in $dir"
exit 0
27% !$
myscript
+ echo Name :
Name :
+ read name
Arnold
+ pwd
dir=/usr/system/bhunter/junk
+ echo Arnold, you are in /usr/system/bhunter/junk
Arnold, you are in /usr/system/bhunter/junk
+ exit 0
28%
30% myscript
Name :
Rambo
Rambo, you are in /usr/system/bhunter/junk
31%
118% du />/tmp/du.udg
^Z
Suspended
Use the command jobs to get the job number and status.
119% jobs
[1] + Suspended du / > /tmp/du.udg
120%
120% bg %1
[1] du / > /tmp/du.udg &
121% jobs
[1] Running du / > /tmp/du.udg
122% fg %1
1. SIGSTOP
#!/usr/intel/bin/zsh
# name: long_job
n=0
while [ $n -ne 10 ]
do
echo $n
(( n += 1))
sleep 10
done
% long_job
0
1
2
^Z
zsh: done long_job
% jobs
[1] + done long_job
% fg %1
[1] + continued long_job
3
4
5
6
7
8
9
%
You will probably want to protect a job in background from being accidently
killed by a signal 1, the hangup or SIGHUP signal.
Now if it has to be killed, it will take a little more work and the command
kill. Use the PID number
% kill 1135
% kill %1
Quiz
$ . .profile
% . .login
You do a
% du ..
from your home directory. It’s taking time and you need your screen. What
are your options?
alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
bg [%jobno] ...
Runs the current or specified jobs in the background.
bg %1
history [ -hr ] [ n ]
Displays the history list. If n is given, it displays only the n
most recent events.
jobs
Lists the active jobs under job control. (not in sh)
-l
Lists process IDs, in addition to the normal information.
nohup command
Runs command with the hangup signal ignored.
source name
Reads commands from the file name and executes it in the
current shell.
source .login
unset pattern
removes variables whose names match pattern.
Source
The sh equivalent to source is dot. Just place a dot in front of
your command and it executes in the current (Bourne) shell.
$ . .profile
Preset Variables
Some shell variables have predefined meanings although they are not part
of the ENV:
ignoreeof
When set ignores EOF from terminals. This protects against
accidentally killing a C shell by typing a ^D.
noclobber
Restricts output redirection so that existing files are not
destroyed by accident. > redirections can only be made to
new files.
notify If set notifies you immediately as jobs are completed, rather than
waiting until just before issuing a prompt.
# superuser
or
533%
% cd perl
% for f in ‘ls‘
for> do
for> ls -ld $f
for> file $f
for> done
-rw-r----- 1 bhunter users 97 Jul 14 09:56
:shell script
-rwxr-x--- 1 bhunter system 310 Nov 11 1998 class.pl
class.pl: shell script
-rwxr-x--- 1 bhunter users 465 Jul 09 15:42 concreate
...
If you cannot remember which shell you are in any particular window check
the window for its terminal name first
% tty
/dev/pts/62
$ echo $0
bash
History
history allows you to use words from previous command lines in the com-
mand line you are currently typing. This can be used for spelling corrections
and typos and the repetition of complicated commands or arguments. Com-
mand lines are saved in a history list. The size of the history list is set by the
history variable. The most recent command is retained in any case. A
history substitution begins with a !
544% history
529 man ksh
530 pwd
531 vi sh.man
532 top
533 pwd
534 cd intro
535 top
536 vi sh.man
537 vi csh.man
538 sh
539 vi ~/.cshrc
540 echo *
541 glob *
542 ls
543 grep prompt ~/.??*
544 history
545%
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!
!n Repeats command-line n.
Word Designators
You can retrieve parts of the last command line with word designators. Here
the user lints a program io.c and then repeats the argument using the
!* designator.
You can harmlessly print an old command line to the screen without it exe-
cuting with
!hist_number:p
as in
71% history
59 args
60 chmod +x args
61 args
62 cat args
63 args one two three
64 cd
65 cd c.c
66 ls
67 lint io.c
68 cc io.c
69 rm .aout
70 rm a.out
71 h
72% !60:p
chmod +x args
73%
^string^replacement
79% !60:p
chmod +x args
80% ^args^hosts
chmod +x hosts
81%
bash runs history and editing very much like Windows32 and NT.
To replace a character place the cursor to the right of it, delete it, and type in
the new character.
HIt escape and then use your vi editing commands to manipulate the cur-
sor and make changes, additions, and deletions.
Aliases
Shells maintains a list of aliases that you can create, display, and modify
using the alias and unalias commands. You already saw alias back a
few pages. Here it is again in these lines from .cshrc
alias h history
alias l look
alias top "ls -lt | sed -n ’1,20p’"
set history = 24
set prompt = ’\!% ’
Aliases can be removed with unalias. Aliases are a help in cutting down
on typing repetitive commands, but they can be overdone. With too many
aliases your csh will get slower than it already is. Minimize aliases. Take
advantage of having your own ~/bin and put small scripts there to do your
everyday commands. Aliases work in all shells except sh.
The Bourne shell named function was created to give sh aliases. Note the
syntax.
dir() {
ls -al | more
}
Another example
top() {
ls -lt | sed ’10, $d’
}
!^ first word
!$ last word
!2 word 2
!* all words
as in:
or:
Get rid of aliases you don’t use. They slow down the shell and just about kill
csh.
Use aliases as macros for frequently used commands and pipelines, e.g
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?
HISTSIZE=10
export HISTSIZE
To set the ksh prompt to your current history number assign it to the
prompt variable.. It will behave very much like C shell.
[53]
!48
!x
!14:*
Nothing boosts user productivity more then task automation through the use
of scripts. The shells are not alone as scripting languages. There is also
PERL and Tcl but shell scripts are faster an most often easier to write.
Advantages of shell scripts:
More robust
Easier to debug
More versatility
Any commands that you type at the terminal can be put into a text file and
executed. These are called scripts. Scripts are used to shorten repetitive
tasks. They can be very simple.
They can be a little more complex with flow control and other programming
constructs.
#!/bin/sh
for dir in ‘ypcat passwd|awk -F: ’$3>99 { print $6 }’‘
do
echo $dir
du $dir|sed -n ’$p’
done
exit
# udu
# shows each users total disk usage
Here is a short but nasty script used, once, a long time ago.
#!/bin/sh
a=3
b=4
c=‘expr $a + $b‘
echo "$a + $b = $c"
Shell Arrays
ksh, bash, etc. have arrays. Typeset the array with the -u flag.
#!/bin/ksh
typeset -i i=0
typeset -i len
typeset -u names
Use echo to write to the STDOUT. To suppress the newline use echo -n
(old) or \c.
Lab
name? : McLane
hi McLane
The shells have built-in command line arguments called positional parame-
ters. Let’s write a script called foo.
#!/bin/sh
echo $1
%myprog bar
bar
If you write
#!/bin/sh
echo "you have $# arguments"
194% argc 1
you have 1 arguments
195% sh argc 1 2 3 4 5 6 7 8 9 0
you have 10 arguments
Lab
Write a script to take command line arguments of the user’s first and last
name and then output them as Last, First.
A Lab Answer
#!/bin/sh
echo "Name: $2, $1"
Comments
Comments start with a #. The rest of the line is ignored. The exception is
the first line.
#!/bin/csh
# does nothing
cd ; ls # added after command(s)
Scripts are not automatically runnable. They must have theri permissions
changes to make them run:
% chmod +x myprog
or
exit
Shells automatically do an exit when they parse the last line. You can and
should use a deliberate exit if you exit anywhere else.
#!/bin/csh
exit 0
# comments
• 0 OK
Decisions, the if
The if is looking for a true/false answer. 0 is true. if can be used to test the
exit code of a command.
if test -f lock.file
then
echo "File locked"
exit -1
else
continue
fi
#!/bin/ksh
typeset -i i=0
typeset -i len
typeset -u names
Note: This script also runs in bash if you comment out the typeset -u.
test
The if and while most frequently use the test command to supply the
boolean true or false needed. Here are most of its flags.
-c FileName True exit value if FileName exists and is a char’ special file.
-k FileName True if the FileName exists and has sticky bit is set.
Integer1 -eq Integer2 True if the Integer1 and integer2 are equal.
-ne, -gt, -ge, -lt, and -le can be used in place of -eq.
-o Binary OR operator
Lab
Take the last lab and add a test to reject the commands use if two argments
are not pased.
A Lab Answer
#!/bin/sh
if [ $# -ne 2 ]
then
echo "use name_lab first_name last_name"
exit 1
fi
echo "Name: $2, $1"
The case is a multi-way branch like a long drawn out if, elsif,...
elsif, else. The variable after the keyword case is the switch. The
word switch is not used but is pattern matched on the argument(s) closed on
the right by a single parentheses. The switch *) is the default.
#!/bin/sh
while /bin/true
do
echo -n "$n "
n=‘expr $n + 1‘
done
or a command results
while sleep 60
do
ps | grep -v grep | grep -s myprog
if $?
then
or a test
n = 1
while [ $n -ne 11 ]
do
echo -n "$n "
n=‘expr $n + 1‘
done
Lab
bash-2.00$ !cd.lab
10
9
8
7
6
5
4
3
2
1
Lab Answers
#!/bin/sh
i=10
while [ $i -ne 0 ]
do
echo $i
i=‘expr $i - 1‘
done
echo bang!
#!/usr/intel/bin/zsh
typeset -i i=10
while [ $i -ne 0 ]
do
echo $i
((i = $i - 1))
done
echo bang!
for n in 1 3 5 7 9
do
echo $n
done
#!/bin/sh
while ‘/bin/true‘
do
echo "continue y/n: \c"
read a
case $a in
y)
continue
;;
n)
break
;;
*)
echo "can’t type?, try again"
continue
;;
esac
done
exit 0
Lab
1
22
333
4444
55555
666666
7777777
8888888
999999999
00000000000
Write a script to read the file one line at a time and print the lines as long as
they have no zeros.
A Lab Answer
#!/bin/sh
i=1
for line in ‘cat foo‘
do
i=‘expr $i + 1‘
# 1234567890
if [ "$line" = "0000000000" ]
then
break
fi
echo "$i $line"
done
i=‘expr $i + 1‘
but in ksh and pdksh you can “declare” integers and do math operations as
long as the are in double sets of parentheses.
#!/usr/intel/bin/zsh
integer i=1
integer len=0
integer w=0
integer h=0
integer volume=0
integer sum=0
while [ $i -lt 3 ]
do
echo "length in feet: \c"
read len
echo "width in inches: \c"
read w
echo "heigth in inches: \c"
read h
echo "$w' by $h' by $len’"
((volume=$len*$h*$w*12))
((volume=$volume/1728))
print "$volume cu ft"
((i=$i+1))
((sum=$sum+$volume))
done
((sum=$sum+$sum))
echo "concrete required $sum cubic feet"
zsh Math
zsh has bash’s math features but is even more relaxed in it’s notation not
requiring type definitions.
#!/usr/intel/bin/zsh
C shell is fragile
C shell fails on constructs that the Bourne shell family scripts thrive on
Command Grouping
Each command executes in its own (sub)shell. This can lead to ugly, time-
consuming scripts.
#!/bin/sh
echo date > f.file
date >> f.file
echo users >> f.file
who>> f.file
By using parentheses to group commands you can get them all to execute
in one shell.
#!/bin/sh
(
echo date
date
echo users
who
)> f.file
#!/bin/csh
( echo dated; date ; echo users ; who )> f.file
Here Documents
#!/bin/sh
ed profile<<END
/biff y/s//biff n/
w
q
END
#!/bin/csh
ed profile<<END
/biff y/s//biff n/
w
q
END
sh Named Functions
Here is a part of a .profile with ll set as a named function.
:
ll(){
ls -l $1
}
Bourne shell has named functions. They can be used to make aliases or as
true functions.
#!/bin/sh
geta () {
while [ 1 ]
do
echo -n "enter yes or no : "
read ans
answ=‘echo $ans|cut -c 1‘
case $answ in
y|Y)
return
;;
n|N)
echo "exiting"
exit 1
;;
*)
echo "enter y or n"
;;
esac
done
}
echo -n "do you want to continue : "
geta
echo "script body"
exit 0
As you can see from this foil, there is a lot more to shell programming than
what you have covered so far.
Good Books
vi
With a little sed and vim
Editing and
Regular Expressions
UNIX Part 5
Rev 6/3/99
ECT001118
Agenda
First we will do a crash course on vi to get the look and feel of it. You will
then study the ed editor since its commands are the basis for vi. Then you
will go through the vi command set. There will be labs at reasonable inter-
vals to reinforce what you have learned and to test yourself. A few advance
techniques will complete the course.
Goals:
Learning to use the UNIX editors for
viewing text
inserting text
appending text
deleting text
moving text
copying text
Advanced Techniques
Skillful and knowledgeable use of vi will save you from all of these
repeated operations. The advantages of vi over a simple editor like xedit
or pico:
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
Ease of reading or writing full or partial files from within the text
Same instruction set as used in: sed, grep, egrep, ex. ed. etc.
Fully customizable
Full featured
Universal
Before examining the intricacies of vi and its parent editor ed, you are
going to take a brief crash course in vi. This part of the class will have you
doing the exercises as the instructor lectures. It is a continuous lecture/lab..
xedit
and in or out of X
ed
ex
vi and vim
emacs
pico
The UNIX default editor is vi. It is easy to use and does a lot. It runs in three
modes. When you are entering text, it is in append mode. To leave append
mode, you type an escape. The editor then will be in vi mode where the
majority of letter keys have a special meaning. For example, the x removes
a letter, while the a puts you back in append mode. The third mode is ex or
ed mode, named after ed, the primitive editor. By entering a slash (/), you
go to the bottom line to execute a search. By entering a colon, you go to the
bottom line to enter a command. Typical commands are w for write, q for
quit, or wq for both.
You will use the most frequently used vi commands in the next set of exer-
cises.
Modes:
vi mode
input mode
ex mode
Enter
% vi exercise_1
This is a mistoook.
Enter a return again. Now hit <escape>. You are in command mode. Enter a
/. You’ll be on the bottom line, the “twenty-fifth line.” Type in ooo. You’ll be
at the ooo in mistoook. Type in x and one o will be removed. Type in cw to
change the rest of the word. Now type in ake. Then hit escape.
Type in b (back) once or twice to get back to the word a. Type i (insert) and
then type in not followed by a space. Now hit escape again. Type in o.
You’ll be down one line and in insert mode. Type a return. Now type
/ing
Type k to move up a line. Type j to move back. Type jjj and you will be
on the bottom. Hit / again then enter. You are back at ing. Type $ and you
are at the end of the line. Now type 0, and you are at the beginning.
Type w and move forward a word. Type 2w and move forward two. Now go
to the command line again, type :and hit return. Enter w and save the file.
Hit / and enter again and you are back at ing. Type h and move left one
character at a time. Type l and move right one character at a time.
Now type : from vi mode and go to the bottom command line and type
:wq
a append
i insert
k move up a line
l move right
h move left
x delete a character
cw change a word
r replace a character
:q quit
If the cursor is not at the beginning of the first line, put it there with an H
command. Let’s put the system date on the first line. Put a blank line above
the top line. Type in O in vi command mode. Now hit escape. Go to the
command line. Type
:r !date
/^$
What was that?? The ^ is the beginning of the line; the $ the end. It is a line
that begins with its own end — a blank line. Now remove it with a dd. Go to
the end of the file with a
:$
Add
~
~
~
"ex.1" line 6 of 6 --100%--
:1,$s/^/ /
There is a tab on the rignt side of the expression. This says "From line 1 to
the end of file ($) put a tab at the beginning of the line." Now get all the line
numbers with this command-line expression.
:set number
Move to line 6.
:6
:set nonumber
:se nonu
/learn
Move the cursor to the v in vi. Type 2fv to find the second v.
/\.$
This regular expression says "Look for a dot as the last character on the
line." Note the backslash escape for the dot. Now find the lines that don’t
end with a dot.
/[^.]$
This says “Look for a not-dot character at the end of the line.”
In vi mode:
a append
i insert
k move up a line
H J K L
h move left
l move right
x delete a character
cw change a word
r replace a character
fa find a
:q quit
In ex command mode:
? search backwards
Learn ed?
UNIX system administators should know ed. If the system won’t come up
into multi-user mode all you have is ed!
ed
grep
ex sed
perl
egrep awk
vi
vim
The Creation of vi
BSD architect Bill Joy1 created vi, when he was at Berkeley. He knew that
a visual editor was necessary for UNIX’s success. The start was the cre-
ation of the ex editor. The next major goal was to create a visual editor, one
that you could see what it is that you are working on. Up until this time a
visual editor could only be on fixed proprietary systems where the terminals
were all of one kind, such as DEC VT-100 terminals. UNIX is created to use
any kind of a ASCII terminal.
/etc/termcap
To create a visual editor, first a system had to be created where each user
session could place the terminal characteristics of the users’ terminals
within each user’s environment. To do this, Bill Joy created termcap. Here
is a brief section of a termcap file showing the VT 100 entry.
Pattern Matching
Pattern matching is used to locate lines in text and set up operations like the
search and replace and the global search and replace. The ed editor sup-
ports pattern-matching characters that you can use as regular expressions
(REs) to construct pattern strings. You can use these patterns in addresses
to specify lines. For example do this to search for the pattern teh.
/teh
Search for the pattern teh and replace it (only one occurrence).
s/teh/the/
Search for the pattern teh and replace it with the globally
1,$s/teh/the/g
You can combine REs into patterns that match strings containing that same
sequence of characters. For example,
AB\*CD
AB*CD
and
[A-Za-z]*[0-9]*
ns
Re
sio
gu
ns
Re
es
lar
sio
gu
pr
Ex
es
Ex
lar
pr
pr
lar
Ex
es
Ex
gu
pr
sio
lar
es
Re
ns
gu
sio
Re
ns
REs
ns
Re
sio
gu
ns
Re
es
lar
sio
gu
pr
Ex
es
lar
Ex
pr
pr
Ex
lar
es
Ex
pr
gu
sio
lar
es
Re
ns
gu
sio
Re
ns
/^[Cc]at$/
is the word
Cat
or
cat
all by itself on a line. ^ is the beginning of the line, $ the end, and [Cc] is c
in either case. The (2) characters a and t stand for themselves.
/^[Cc]at[a-z][a-z]*$/
is a longer word starting Cat or cat but with more letters. The * means
0-infinity. A word like caterpillar would satesify the expression.
/^[^ ]/
Will look for a line that does not start with a blank.
The expressions above are pattern matches and can be used for searches
or addresses. The next is a substitution.
Characters 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
(there are128) !@#$%^&*()_+|~[]{};:’"<>,./?
\n\r\t\v\f\a
[ ] [ ]
Tag ^[a-z][a-z]*:\([^:][^:]*\):[0-9][0-9]
1. specific
\(foo\).*\(bar\)
This tags the expressions foo and bar. The character sequence \(Pat-
tern\) marks a subpattern that matches the same string it would match if it
were not enclosed.
Tag Numbers
Tagged expressions have tag numbers. In the example above foo is \1
and bar is \2. The characters \number match the same string of charac-
ters that a subpattern matched earlier in the pattern. number is a digit. The
pattern \number (for example \2) matches the string matched by the
occurrence of the subpattern specified by number, counting from left to right.
The expression
/\(foo\).*\(bar\)/\1\2/
tags
/\(foo\).*\(bar\)/\1\2/
tag numbers
A . (period) addresses the current line. This is the default for most ed sub-
commands and does not need to be specified.
Numbered Addresses
Number addresses reach the specified line number of (or in) the buffer.
Marked Addresses
‘x addresses the line marked with a lowercase ASCII letter, represented by
x, by the k subcommand (ed mode) or m subcommand in vi mode.
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).
61% ed foo
69
1,$p
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
/33
333
+,$-1p
4444
55555
666666
7777777
16 Description
17 The true command returns a zero exit value. The false command
18 returns a nonzero exit value. These commands are most often
19 used as part of a shell script.
20
21 Examples
22
23 To construct a loop that displays the date and time once each
24 minute, use the following code in a shell script:
25
26 while true
27 do
28 date
29 sleep 60
30 done
31
32 Implementation Specifics
...
36 Related Information
37
38 How to Create and Run a Shell Script in AIX Version 3.2 System
39 User's Guide: Base and Devices provides information on creating
40 and executing shell procedures.
17,19
.,.+2
or
1,$w mytxt.bak
.,$d
.,.+4 co $
/^From /, /^$/d
.r mytxt
$-5,$d
1,/^$/mo $
Explain:
1,$s/teh/the/g
s/^[ ][ ]*//
s/ *$//
s/ */ /g
/^ *\([A-Z][a-z]*\)/\1/
#!/usr/local/bin/perl5
while(<DATA>) {
s/0000$/0,000/;
while(/0000/) {
s/0000,/0,000,/
}
print
}
__END__
30000000
100000000000
100000000000000000
The results
1009% big_no.pl
30,000,000
100,000,000,000
100,000,000,000,000,000
Note that the line has been parsed from right to left.
Look at the file first in vi. Titles may look like this
A^H F^HFI^HIL^HLE^HE
:1,$s/\b.//g
:1,$s/^H.//g
Like ed, vi runs in command mode and in append or insert mode, but it has
two command modes. One is the ex mode issuing commands from the
“25th line,” the command line at the bottom of the screen. The other is vi
mode done by issuing short mnemonic commands (subcommands) from the
screen, the text itself.
:w
The bottom line will then tell you what you wrote.
vi Mnemonics
vi has a huge set of mnemonics it uses in vi (command) mode. The mne-
monics use most of the characters on the ASCII keyboard. It uses them
plain, shifted, or as control characters. The numbers are used as repeat
characters.
q w e r t y u i o p [ ]
a s d f g h j k l ; ’
z x c v b n m , . /
Your right fingers should rest on the characters jkl; and the h is there with
a small movement of the index finger. The leftmost key, h, moves the the
cursor down, its left neighbor, k, moves the cursor up.
h j k l
That it! Almost all the other keys have mnemonic values like u up, d down,
etc. For trickier movements use:
Repeats
Let’s say your cursor is at the top of the screen, you want it near the bottom.
Use a number before the j, such as 25j, and you are at the bottom. A num-
ber before a mnemonic is a repeat.
Deletions
Oops!, you made a mistake! We all do, and that is why vi has so many
commands for removing and changing text and characters.
dw Removes a word.
Changes
Sometimes you delete messes; other times you change them.
cw Changes the word from the cursor position to the last character in
the word. White space is seen as word separators.
Undoing a Command
A U, as an ex command, will undo the last edits and restore what was there
first.
Repeating a Command
The last command will be repeated if you type a dot (This will happen if you
want it or not because once in a while you will hit dot accidently.)
Change case
Say that you misstype the and get teh. Now put the cursor over the e and
hit x, and the e is gone. Without moving the cursor type in p, and the e is
back but now after the h .
teh
to
the
with just two keystrokes. Later you’ll see how to do it with one by mapping a
key (like z) to the x and p commands.
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
1
22
333
4444
55555
666666
7777777
88888888
0000000000
999999999
end
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
yw yank a word
yy yank a line
Once yanked into the buffer, the buffer can be put with the p subcommand
(for after the cursor) and a P subcommand (for before the cursor.) Want to
repeat a line? Put the cursor at the end of the foo file and do a yy followed
by a p.
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
end
Here’s one more exercise with named buffers. Put the cursor on one of the
ends and yank it to buffer with
"ayy
Now go to the top of the text and put the buffer in front of line 1.
"aP
end
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end end
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.
as in
:1,15d
The : is the prompt for the bottom (ex) command line. This last example
Removes lines 1 through 15.
Moving Text
.,.m N
as in
1,15m $
Let’s start to repair our mutilated foo file. You type in 1m $ to move the first
line to the last on the ex command line and get
end
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end end
:$-1d
and
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
Now the tree is OK; let’s mess it up again and copy all of it to its end.
:1,$copy $
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
1,$co $
or
1,$t $
:/^1$/
which is a pattern that becomes the address. Now let’s add begin to the top
of the file.
begin
1
22
333
4444
55555
666666
7777777
88888888
999999999
0000000000
end
:/begin/,/end/w snarg
You can fork a shell1 from vi and execute about anything you like. The sim-
plest way is to create a shell and do whatever you have to do outside of vi.
:!sh
When you have done your task, strike ^D and your back in your vi session.
You can execute a command from shell and put its results in you’re vi file.
For example this will do a listing of the current directory and read it in at the
current cursor position.
:r !ls
:$-20,$w temp
which writes the file temp and puts the last 20 lines of your vi buffer in it.
:r foo
:wq
:x
or from vi mode
ZZ
U/u
Quitting vi
The command
:q
will quite vi unless you have made changes. If so, and you are serious
about quitting without saving, do this:
:q!
:^G
~
~
~
“bar” The cursor is at line 1 of 12 --8%-- .
Customizing vi
Like all things UNIX, you can customize vi to your preferences. You can
set characteristics
:set all
noautoindent nomodeline noslowopen
autoprint nonumber nosourceany
noautowrite nonovice tabstop=8
nobeautify nooptimize taglength=0
closepunct=’".,;)]} tags=tags /usr/lib/tags
directory=/var/tmp partialcharacter=- term=xterm
noedcompatible prompt noterse
noerrorbells noreadonly timeout
noexrc redraw ttytype=xterm
flash remap warn
hardtabs=8 report=5 window=40
noignorecase scroll=20 wrapscan
linelimit=1048560 sections=NHSHH HUuhsh+c wrapmargin=0
nolisp shell=/bin/csh nowriteany
nolist shiftwidth=8 wraptype=word
magic noshowmatch
mesg showmode
map z xp
The following subcommands allow you to run a UNIX command within the
vi editor. Enter these subcommands in command mode.
:sh
Enters the shell to allow you to run more than one UNIX command.
You can return to the editor by pressing the ctrl-D key sequence.
:!command
Runs the specified UNIX command and then returns to the editor.
Special Tricks
1
22
333
4444
55555
666666
7777777
88888888
999999999
end
^I1$
^I22$
^I333$
^I4444$
^I55555$
^I666666$
^I7777777$
^I88888888$
The last 9 deletions are stored in numbered buffers. To retrieve any of these
buffers use
"Np
"4p
If you take our foo file and delete the lines 1 through 9, starting at line 1,
and then execute "9p, then "8p, .... and you get
end
999999999
88888888
7777777
666666
55555
4444
333
22
1
You can see that buffer 1 was the first, buffer, 2 the second and so. Now
buffer 1 holds the oldest deletion, and buffer 9 the latest.
To simply do a save ( if you expect a power glitch, the server, or the net to
burp) do a
:w
If you do get hit with a crash a copy of your file is saved by UNIX and can be
retrieved by invoking vi with the r flag and file name.
% vi -r filename
It’s a good idea to enter only one sentence to the line. Place a newline after
every period. It makes way for further editing.
What is vi?
$ed foo
.ad l
.ad l
What is vi?
~
~
~
~
~
!nroff
What is vi?
$ed foo
: set tabstop=4
With this setting, the tabs will be at modulo 6 rather then the default of 8.
Abbreviations
Do you type the same large word over and over? If you do, you can get vi
to type it for you by setting the ab attribute.
: ab SA system administration
Now when ever you type SA, vi will expand it to system administra-
tion.
EXINIT
You can semi permanently set vi with the EXINIT environmental variable
in your .cshrc, .login, or .profile. Then you will have the vi settings
you like or need every time you bring up vi.
.exrc
43% pwd
/usr/system/bhunter
44% cat .exrc
set wrapmargin=10
set tabstop=6
map z xp
ab SA System Administration
45%
vim has lots of enhancements over vi like multi level undo, multi windows
and buffers, command line editing, filename completion, on-line help, visual
selection, automatic man pages for UNIX commands, and so on.
What more could vi do? How about if you could use the mouse to help you
edit? In vim you can. Start with set.
:set mouse=a
Now the mouse works with the editor. Move it and you won’t need the h, j,
k, and l keys to move the mouse. Mark text and the following functions
work:
By learning vi you already know about half of what you need to run sed.
sed is the streaming editor. Remember the vi line for a global replace of
teh to the?
:1,$s/teh/the/g
Let’s run a document through sed and make the same corrections.
That’s it? Yes it’s that easy. Now in vi remove the first line of text.
:1d
What can sed do that vi can’t or would have a hard time with? How about
search for and then remove everything on one line.
sed ’/Intel/ \a
vendor of choice’ venddor_list.dat
There is a whole lot more but that will have to wait for UNIX Part II.
spell
look
ispell
vim
nroff / troff
Devices
UNIX reaches all of its physical devices through device drivers built into the
kernel. The entry point to devices is found in the directory /dev. Let’s look at
a terminal.
216% ls -l /dev/tty*
crw-rw-rw- 1 root system 1, 0 Feb 18 14:24 /dev/tty
crw-rw-rw- 1 root system 16, 0 Oct 21 13:42 /dev/tty0
This doesn’t look like any files or directories you have seen so far. That’s
because it’s not a regular file; it’s a special file.
link count
315% ls -l fd*
lrwxrwxrwx 1 root root 15 Jan 19 19:08 fd ->
../proc/self/fd
brw------- 1 root root 2, 0 May 5 1998 fd0
brw------- 1 root root 2, 12 May 5 1998 fd0D360
brw------- 1 root root 2, 16 May 5 1998 fd0D720
brw------- 1 root root 2, 28 May 5 1998 fd0H1440
brw------- 1 root root 2, 12 May 5 1998 fd0H360
brw------- 1 root root 2, 16 May 5 1998 fd0H720
brw------- 1 root root 2, 4 May 5 1998 fd0d360
brw------- 1 root root 2, 8 May 5 1998 fd0h1200
brw------- 1 root root 2, 20 May 5 1998 fd0h360
brw------- 1 root root 2, 24 May 5 1998 fd0h720
brw------- 1 root root 2, 1 May 5 1998 fd1
brw------- 1 root root 2, 13 May 5 1998 fd1D360
brw------- 1 root root 2, 17 May 5 1998 fd1D720
brw------- 1 root root 2, 29 May 5 1998 fd1H1440
brw------- 1 root root 2, 13 May 5 1998 fd1H360
brw------- 1 root root 2, 17 May 5 1998 fd1H720
brw------- 1 root root 2, 5 May 5 1998 fd1d360
brw------- 1 root root 2, 9 May 5 1998 fd1h1200
brw------- 1 root root 2, 21 May 5 1998 fd1h360
brw------- 1 root root 2, 25 May 5 1998 fd1h720
Note the major number 2. They all have the same device driver. The minor
numbers and names reflect three different disk densities.
Your Terminal
If you start at a bare screen, you are on a tty. If you are in a window you
are on a pseudo terminal.
218% ls -l /dev/*tty*
crw-rw-rw- 1 root system 1, 0 Feb 18 14:24 /dev/tty
crw-rw-rw- 1 root system 16, 0 Oct 21 13:42 /dev/tty0
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp0 -> /dev/pts/0
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp1 -> /dev/pts/1
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp2 -> /dev/pts/2
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp3 -> /dev/pts/3
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp4 -> /dev/pts/4
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp5 -> /dev/pts/5
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp6 -> /dev/pts/6
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp7 -> /dev/pts/7
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp8 -> /dev/pts/8
lrwxrwxrwx 1 root system 10 Dec 14 1992 /dev/ttyp9 -> /dev/pts/9
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypa -> /dev/pts/10
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypb -> /dev/pts/11
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypc -> /dev/pts/12
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypd -> /dev/pts/13
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttype -> /dev/pts/14
lrwxrwxrwx 1 root system 11 Dec 14 1992 /dev/ttypf -> /dev/pts/15
219%
It’s not real; it’s virtual. Its driver is a pseudo driver, which does all its work in
software.
% tty
/dev/pts/9
stty
Terminal’s hardware and termio characteristics are set with stty. To see
your terminal’s setting use
223% stty -a
speed 9600 baud; 47 rows; 80 columns;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D;
eol = ^@eol2 = ^@; start = ^Q; stop = ^S; susp = ^Z;
dsusp = ^Y reprint = ^R discard = ^O; werase = ^W; lnext
= ^V -parenb -parodd cs8 -cstopb hupcl cread -clocal -
parext -ignbrk -brkint -ignpar -parmrk -inpck -istrip -
inlcr -igncr icrnl -iuclc -ixon -ixany -ixoff -imaxbel
isig icanon -xcase echo echoe echok -echonl -noflsh
-tostop echoctl -echoprt echoke -flusho -pending iexten
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel
224%
%pe@wd
will be accepted as
%pwd
% stty erase ^H
Your UNIX environment knows its terminal type because you set it when you
logged on. It should have been in one of your dot files.
tset -n -I
export TERM
The tset command first determines the type of terminal used, then per-
forms necessary initialization and mode settings. The flags used here
-n
on systems with the Berkeley 4.3 tty driver, specify that the new tty driver
modes should be initialized for this terminal while
-I
229% env
TERM=xterm
SHELL=/bin/csh
or
How do you know what terminal you’re on? Ask the machine with tty.
232% tty
/dev/pts/4
233%
Your terminal knows about how to handle its cursor from termcap.
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.
Quiz
You just got an old VT-100. How do you set up your environment for it?
You need to put a Wyse 50 on as a terminal. How do you find out if it can be
accepted by more or vi?
Lab
Try it.
Echo ’#’
Printers
We may be heading for a paperless society, but for now a lot of trees are
becoming paper for printers. To send someting to a printer, you must first
spool it with a spooling system, and there are no less than three printer
queuing (spooling) systems used in UNIX today.
What’s in a name?
f2: pole F2
2: second floor
3: fm3
fm: Folsom
lpr is the BSD printer command. If you have a printer called lw1, you send
a file to it with
Did your job get to the printer queue? You can test the queue with lpq
244% lpq
Queue Dev Status Job Files User PP % Blks Cp Rnk
------- ----- --------- --- ------- - ---- -- ----- -- ---
lps20 dplps READY
lps20: no entries
245%
If you mess up and send a binary file to the printer, you can get rid of it with
lprm. There may be a problem, however, if your job went to a print server.
What’s a Spooler?
request
spooler
printer
The UNIX System V print spooler has its own set of commands. The print
command is lp and the device (printer) destination flag is -d, so the com-
mand line
$ lp -depson /etc/motd
lpsched
enabled
and
accepted
by the print administrator before you can use it. How do you know if it has?
Use the command lpstat with the -p flag and the printer name.
$ lpstat -p epson
printer epson is idle. enabled since \
Fri Feb 18 19:17:11 1994. available.
This message means that the printer epson is ready to print, and there are
no jobs ahead of yours.
lp
enable
disable
spooler (lpsched)
accept
reject
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
you definitely are going to have to use lp. The command lpr is usually
aliased to lp but is not the BSD lpr command.
ls -l /usr/bin/lp*
You can see that lpr is really lp, and the lpstat command is there.
cancel cancels printer requests made by lp. The command line argu-
ments may be either request ids, as returned by lp, or printer names. For a
complete list, use lpstat. Specifying a printer cancels the request that is
currently printing on that printer. .
Note that you get a message when you send a print request to the printer
$ cancel epson-3
Quiz
What are the printing, queue-observation, and cancel commands for
1) System V
2) BSD
3) AIX
Lab
Find out which spooler system you have on your system.
Floppy Drives
Nearly all workstations and UNIX PCs nearly all have floppy drives. The
5 1/4 inch format used to be a standard but is now largely replaced by the
smaller 3 1/2. A floppy drive with diskette can be mounted as a UNIX file
system, but is used most often as an archive media like a tape or CD.
UNIX has multiple commands to write to any disk, or diskette, or tape. A few
are
tar
cpio
To retrieve a file
tar example
One quick way to get the name of your floppy device is with a listing of
/dev. Here is a look at AIX.
42% ls -l /dev/fd*
brw-rw-rw- 1 root system 11, 0 Feb 15 07:52 /dev/fd0
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/fd0.18
brw-rw-rw- 1 root system 11, 2 Jan 16 07:54 /dev/fd0.9
brw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/fd0h
brw-rw-rw- 1 root system 11, 2 Dec 14 1992 /dev/fd0l
Note the link counts and minor numbers. fd0h is, really fd0.18 while fd0
is really fd0.9. Are these block devices only?
43% ls -l /dev/rfd*
crw-rw-rw- 1 root system 11, 0 Dec 14 1992 /dev/rfd0
crw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/rfd0.18
crw-rw-rw- 1 root system 11, 2 Jan 16 07:54 /dev/rfd0.9
crw-rw-rw- 2 root system 11, 1 Dec 14 1992 /dev/rfd0h
crw-rw-rw- 1 root system 11, 2 Dec 14 1992 /dev/rfd0l
The rfd’s are raw devices, used for formatting. Another source is looking at
the man page for diskette formatting. AIX is being currently phased out.
Here is the top of a Linux workserver’s floppy disk lisitng.
315% ls -l fd*
brw------- 1 root root 2, 0 May 5 1998 fd0
brw------- 1 root root 2, 12 May 5 1998 fd0D360
brw------- 1 root root 2, 16 May 5 1998 fd0D720
brw------- 1 root root 2, 28 May 5 1998 fd0H1440
The most common, and therefore portable, format is “high” density 135
tracks per inch (80 tracks) with 18 sectors for a capacity of 1.4M.
tar is the most convenient for small transfers but IBM’s version insists on
using a hyphen
fdformat
NAME
fdformat - Low-level formats a floppy disk
SYNOPSIS
fdformat [ -n ] device
DESCRIPTION
fdformat does a low level format on a floppy disk. device is usually one
of the fol-
lowing (for floppy devices, the major = 2, and the minor is shown for
informational
purposes only):
/dev/fd0d360 (minor = 4)
/dev/fd0h1200 (minor = 8)
/dev/fd0D360 (minor = 12)
/dev/fd0H360 (minor = 12)
/dev/fd0D720 (minor = 16)
/dev/fd0H720 (minor = 16)
/dev/fd0h360 (minor = 20)
/dev/fd0h720 (minor = 24)
/dev/fd0H1440 (minor = 28)
/dev/fd1d360 (minor = 5)
/dev/fd1h1200 (minor = 9)
/dev/fd1D360 (minor = 13)
/dev/fd1H360 (minor = 13)
/dev/fd1D720 (minor = 17)
/dev/fd1H720 (minor = 17)
/dev/fd1h360 (minor = 21)
/dev/fd1h720 (minor = 25)
/dev/fd1H1440 (minor = 29)
Obviously a lot more needs to be explained so look for another man page.
The related command is fd. It goes on to explain the formats the fd drivers
can do.
fdnH1440 1440K 80 18 2 28
fdnH1600 1600K 80 20 2 124
fdnH1680 1680K 80 21 2 44
fdnH1722 1722K 82 21 2 60
fdnH1743 1743K 83 21 2 76
fdnH1760 1760K 80 22 2 96
fdnH1840 1840K 80 23 2 116
fdnH1920 1920K 80 24 2 100
The points to consider here are about man page lookup. First point: There
are 9 separate manuals 1 - 8 and n. Not everyting is in man 1. Second look
at the bottom of each man page for See Also. It is a pointer to the next man
page. To get a special man page like fd from section 4 use this format:
% man 4 fd
UNIX Part I
Section 8
Rev ed 7/13/99
ECT001118
Getting mail notification without disturbing whatever else you are doing.
Mail is a loose term email, even looser. In the computer sense mail and
email are utilities by which we can send and receive mail. Traditional-UNIX
users use the command mail, but even that is an over simplification. UNIX
mail is done with the mail command. UNIX “mail” is set up to send mail
over UUCP
In reality, there is a lot more than the mail command. There is a three-level
hierarchy of agents.
user agents
routing agent
delivery agents
All mail processed by these user agents are processed by the routing agent,
usually sendmail. Final delivery is managed by the delivery agent, which is
/bin/sh on the local machine, uux for UUCP, or the Ethenet for Internet
delivery.
To make understanding mail easer, we will deal deal with the user agent
/usr/ucb/mail for a while.
fm.intel.com
mail deastman
Mr. Eastman will get it only if he’s on the local machine on in the local
domain. Beyond that you will need more than the person’s log-on name.
Here is an internet-style address.
mail [email protected]
This is the “canonical” form. Note that the user’s name is separated from the
system name with an @. The system from the organization and the organiza-
tion from the major domain is separated with a dot.
46%
46% mail bhunter
Subject: greetings
Good morning!
.
Cc:
47% hostname
jaeger
48%
Good morning!
49%
Note that although the mail was sent “locally,” actually it has traveled
through a mail master.
52% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 900 78% 871 85% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 748 93% 364 8% /var
/dev/hd3 8192 7868 3% 36 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 153712 81% - - /misc
fmsu01:/usr/spool/mail 408391 288293 29% - - /usr/spool/mail
fmsu01# ls -l
total 2
-rw------- 1 root 28 Feb 17 07:50 dfAA18373
-rw------- 1 root 0 Feb 17 07:50 lfAA18373
-rw------- 1 root 682 Feb 17 07:50 qfAA18373
-rw-r--r-- 1 root 0 Feb 17 07:50 xfAA18373
fmsu01# pwd
/var/spool/mqueue
The qf (or queue) files are the header files. The df files are the data. These
files stay just momentarily and are them combined and sent to your own
mail file in/var/spool/mail. Here is a qf file. It will be translated into a
mail header.
Your mail file in /var/spool/mail belongs to you. You are the owner.
28% cd ../mail
29% pwd
/var/spool/mail
30% ls -l bhunter
-rw------- 1 bhunter 36922 Feb 16 17:02 bhunter
31% whoami
bhunter
32%
Quiz
Where is your mail stored?
Lab
Receive it.
What was the Internet address of the host that sent it?
42% from
From: "Enterprise Change R" <[email protected]> Wed
Feb 16 15:45:29 1994
43%
biff must be built into one of your dot files. xbiff must be part of your X
dot files.
Sending mail is simple. Let’s go back to the greeting message. The user is
on the local machine or in the local domain. Type in mail username
Subject: greetings
Type it in and then send your message. Terminate it with a dot on a line all
by itself.
Good morning!
.
Cc:
The Cc line can be ignored if you don’t want courtesy copies. You can also
send a prepared mail (file) with redirection to the mail command.
mail deastman<ww7
You will receive no notification if the mail message you send is delivered
successfully. If not it will come back, you will be notified, and the message
will be in $HOME/deadletter or back in your mail.
57% mail
Mail [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 Chang
e Summary Pt2"
N 2 [email protected] Wed Feb 16 15:47 322/15027
"WW08 ENCO Chang
e Summary (TCR"
N 3 [email protected] Wed Feb 16 16:51 60/2088
"FOLSOM (FWR) FACI
LITIES WORK "
N 4 [email protected] Wed Feb 16 16:52 122/3287 "CA
Materials Tec
hnology Analy"
N 5 [email protected] Wed Feb 16 17:02 86/4228
"Hotel Rooms are S
carce 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 look
at a message.
& 6
Message 6:
From [email protected] Thu Feb 17 08:08:26 1994
From: Bruce H Hunter <[email protected]>
Date: Thu, 17 Feb 1994 08:08:23 -0800
To: [email protected]
Subject: greetings
Good morning!
&
&d
& d
Message 5:
From [email protected] Wed Feb 16 17:02:25 1994
Date: 16 Feb 1994 16:52:16 GMT
From: "Bulletin Administra" <[email protected]>
Subject: Hotel Rooms are Scarce in Arizona
Comment: MEMO 1994/02/16 17:00
Apparently-To: <[email protected]>
Quitting Mail
&
At EOF
& q
Saved 1 message in mbox
Held 4 messages in /usr/spool/mail/bhunter
58%
If you quit before reading all your mail, the last message will be saved in
your $HOME/mbox. It is a good idea to clean it out from time to time.
58% cd
59% ls -l mbox
-rw------- 1 bhunter system 24158 Feb 17 08:48 mbox
62% cat /dev/null>mbox
63%
336% ls -l mbox
-rw------- 1 bhunter system 6357 Dec 9 07:37 mbox
337% sh
$ >mbox
$ ls -l mbox
-rw------- 1 bhunter system 0 Dec 22 08:25 mbox
$
The mail command will display messages with a status for each message.
The status can be any of the following:
N A new message.
22% mail
Mail [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 "CA
Materials Technology Analy"
&
!command
Executes the workstation shell command specified by command.
+Name
Refers to a file in your folder directory.
folders
Lists the names of the folders in your folder directory.
top [messagelist]
Displays the top few lines of the messages specified by messagelist.
The mail program treats lines beginning with the ~ (tilde) character as
subcommands. The following describes the subcommands used while
in the mail editor. The editor recognizes subcommands only if you enter
them at the beginning of a new line.
˜! command
Executes the shell command and returns to the message.
˜c addresslist
Adds names in addresslist to the list of people to receive
copies of the message. The ˜c subcommand can only be used to add
to, not change or delete, the contents of the Cc: list.
˜e Starts the alternate editor (like vi) using the message text as the
input file. This editor can be defined with the set EDITOR= statement.
When you exit the editor, you return to the mail editor, where you may
add text, or send the message by exiting mail.
˜f [messagelist]
Includes a messagelist in the current message to
forward the message to another user. This subcommand reads each
message in the messagelist and appends it to the current end of the
message, but does not indent the appended message.
˜q Quits the editor without sending the message. Saves the mesage in
the dead.letter file in your home directory, unless the nosave
option is set.
˜r file
Reads the contents of a file into the current message.
˜s string
Changes the subject field to the phrase specified in string.
˜t addresslist
Adds the addresses in addresslist to the To: field of the message.
˜v Starts the vi editor using the message text as the input file. This editor
can be defined with the set VISUAL= statement. When you exit that
editor, you return to the mail editor, where you may add text to the
message or send the message by exiting mail.
˜w file
Writes the message to file.
˜: subcommand
Executes subcommand and returns to the mail.
1) Look at a message?
2) Delete a message?
5) Save a message?
You just spelled and formatted a document. How do you send it to user
cbarret?
ignore fieldlist
Adds the header fields in fieldlist to the list of fields to be ignored. Ignored
fields are not displayed when you look at a message with the type or print
subcommand. Use this subcommand to suppress machine-generated
header fields.
unset optionlist
Disables the values of the options specified in optionlist. This action is the
inverse of the set optionlist entry.
ask
Prompts for the subject of each message sent. If you do not wish to create
a subject field, press Enter at the prompt.
askcc
Prompts for the addresses of people who should receive copies of the mes-
sage. If you do not wish to send copies, press Enter at the prompt.
autoprint
Sets the delete subcommand to delete the current message and display
the next message.
debug
Displays debugging information. Messages are not sent while in debug
mode. This is the same as specifying the -d flag on the command line.
dot
Interprets a period entered on a line by itself as the end of a message you
are sending.
hold
Holds messages the you have read but have not deleted or saved in the
system mailbox instead of in your personal mailbox. This option has no
effect on deleted messages.
ignore
Ignores interrupt messages from your terminal and echoes them as @ (at
sign) characters.
ignoreeof
Sets mail to refuse to quit at the ^D key sequence as the end of a mes-
sage.
metoo
Includes the sender in the alias expansion. By default, expanding the alias
removes the sender. When this option is set in your .mailrc file, sending
a message using an alias that includes your name sends a copy of the mes-
sage to your mailbox.
noheader
Suppresses the list of messages in your mailbox when you start mail.
Instead, only the mailbox prompt (&) is displayed. To get a list of messages,
use the h subcommand.
nosave
Prevents retention of interrupted letters in t $HOME/dead.letter.
quiet
Suppresses the printing of the banner when mail starts. The banner is the
line that shows the name of the mail program.
verbose
Displays the actual delivery of messages on the terminal.
You can use a set entry to assign values to the following options. For
example, enter
set screen=20
crt=lines
Defines the number of lines of a mail message mail displays before paus-
ing for input (this option starts pg to control the scrolling).
EDITOR=editor
Gives the full path name of the editor to be started with the e mailbox sub-
command or the ˜e mail editor subcommand.
escape=C
Changes the escape character C used for mail editor subcommands. The
default character is ˜ (tilde).
folder=pathname
Gives the path name of a directory in which to store mail folders. Once the
directory is defined, you can use the + (plus sign) notation to refer to it
when using the file name ++ parameter with mailbox subcommands.
record=filename
Defines a file in which to record outgoing mail. The path name must be
given relative to the user’s home directory.
screen=lines
Defines the number of lines of message headers displayed.
toplines=lines
Defines the number of lines displayed by the top subcommand.
VISUAL=editor
Gives the full path name of the editor to be started with the v subcommand
or the ˜v mail editor subcommand. The default editor is vi.
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
set folder=’Mail’
set append dot autoprint
alias mark [email protected]
alias mustang-list [email protected]
24% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 23:34 /etc/init
root 644 095 0 Jan 16 - 0:00 sendmail -bd -q30m
The primary job for sendmail is address translation. Mail yourself a mes-
sage.
mail bhunter
Return-Path: <[email protected]>
You didn’t even type in an address, and now it has a address and it isn’t
even your machine. sendmail has added an address, not any old address
but a fully canonical address in Internet format. The received line shows it
knows that the mail came from jaeger, but the machine of record is trng,
which is also the name of the mail domain. sendmail uses a set of pre-
defined rules for the address translation.
mail gcanepa
and he gets it. This isn’t unusual until you realize that he has no UNIX
account, he’s on soft switch. How did it work? In the file /etc/aliases or
its YP equivalent the is a list of aliases that mail uses. Here is just a small
price of it.
gcanepa:[email protected]
gchin:gchin@fmsu01
glenmcph:glenmcph@fmsu01
gmiller:gmiller@fmsu01
grathbun:grathbun@fmsu01
grc:[email protected]
gsady:gsady@fmsu01
gsiemers:gsiemers@fmsu01
gsimko:[email protected]
Notice the first intial and last names. They are on soft switch. The aliases
file substitutes the adddress to get it to sendmail. sendmail will do the
rest of the adress translation to soft switch. Here is rule one of sendmail
for the address translation.
S1
#
# rules added for softswitch addressing
#
R$-@$j$* $1@$R.$D make FROM field mail host
R$- $1@$R.$D add domain to single addresses
R$+@$-@$+ $1@$2.$D ssw reply resolution
#
What if you are temporarily transferred to another site for a few days? How
will you get your mail?
Create a file called .forward in your $HOME. In it put your logname and
new, temporary address.
%cd
% cat .forward
[email protected]
An extemely useful use of .forward is to send you UNIX mail to your Win-
dows account.
Security
UNIX Part I
section 9
Rev 1/13/99
ECT001118
These words are frightening, but with a little work and some knowledge
there is security. In the film, the antagonist, Wolfgar, was eventually killed
and then there was security — for a while. That brings about another point.
Security, like freedom, must be guarded and worked on constantly.
What is security? You leave a dollar and a half in your desk drawer and the
next morning it’s gone. You have been violated. There is a lack of security.
What if you had locked the desk? That’s security.
Would you leave your pay, in cash, laying on your desktop day after day?
Then why do you leave the company’s intellectual and trade secrets open
on your desk day after day? Why do you leave your computer terminal open
for anyone who comes along to access it, day after day?
UNIX has many ways to protect the user’s work from access, theft, or tam-
pering. UNIX can be configured to an orange book C2 security level. Part of
its security is up to the system administrators, but most of it is up to you.
Areas that can be secured are
Logon access
Password protection
File protection
Directory protection
You can do more to secure the system from access than all the other protec-
tion features. What can you do?
Don’t walk away and leave your screen unattended. Lock it.
Protect your file systems and files from casual access with reasonable
permissions.
A password was assigned when your account was created. That password
is your key to the system and should be protected at all times. Your logon
prompt looks like
login:
You type in your log-on name and then you are asked for the password.
password:
Echo is turned off while you type it, so it can’t be seen. If your screen seems
“funny”, key in a dummy name and password. You may be the victim of a
Trojan horse.
If at any time you feel that your password has been compromised, change it.
chpasswd
The first step is to pick a good password, one that is a character mix and
something that even your best friend wouldn’t know. How about that pair of
skis that you sold two years ago? They were Solomon GSs, 205 cm long.
sol*205GS
You won’t forget it, and no one will guess it. Any character is good except
the newline. Even control characters work, and they are hard to see. The
command you need to use for a system that is not on NIS (YP) is passwd.
Type
% passwd
and it will ask you for the new password. Enter it, and you will be asked to
type it again to be sure that you didn’t make a typo.
You need to use the yppasswd command on systems with NIS (YP). It
works the same as /bin/passwd.
or
The garbage you see in the second field is the result of your password being
encrypted. On newer systems, your password encryption is in a shadow
file.
94% ls -al
total 4
drwxr-xr-x 2 bhunter system 512 Feb 02 07:35 .
drwxrwxr-x 3 bhunter system 512 Feb 16 10:33 ..
-rw-r--r-- 1 bhunter system 0 Feb 02 07:35 foo
95%
regular file
read
write
execute
no permission
-rwxr-xr-x
drwxr-xr-x
other (everyone)
group
user
directory
4 2 1 octal
Sum = 7 = 0111 2
94% ls -al
total 4
drwxr-xr-x 2 bhunter system 512 Feb 02 07:35 .
drwxrwxr-x 3 bhunter system 512 Feb 16 10:33 ..
-rw-r--r-- 1 bhunter system 0 Feb 02 07:35 foo
95%
102% cd junk
103% mkdir noenter
104% chmod 666 noenter
105% ls -ld noenter
drw-rw-rw 2 bhunter system 512 Feb 18 09:47 noenter
106% cd noenter
noenter: Permission denied
107%
Permissions are also called permissions modes or mode bits. Here is what
they mean. The first description is for files, the second for directories.
d a directory
Set UID means that a user can execute a command as if it were executed
by the owner.
Here the set UID bit is set so anyone can execute /bin/passwd as if root
executed it. It is needed so the file /etc/passwd can be written to.
The sticky bit enables a link/unlink attribute for a directory. Files can only be
unlinked if the requesting process has write permission for the directory and
is either the owner of the file or the directory.
For files the sticky bit enables the save text attribute for an executable file.
The program is not unmapped after usage. It allows user delete only.
The link, bar, can be removed only by the owner or by root even if directory
permissions would otherwise permit it. Links can be made across directories
but not physical partitions.
109% cd cron
110% ls -la
total 32
drwxr-xr-x 4 bin cron 512 Dec 14 1992 .
drwxrwxr-x 15 bin bin 1024 Feb 14 04:06 ..
drwxrwx--- 2 bin cron 512 Dec 14 1992 atjobs
drwxrwx--- 2 bin cron 512 Aug 13 1993
crontabs
111% ls -l /bin/passwd
-r-sr-xr-x 1 root system 46797 Dec 14 1992 /bin/passwd
112% ls -l /etc/passwd
-rw-rw-r-- 1 root system 229 Jan 07 1993 /etc/passwd
113% ls -l /etc/shutdown
lrwxrwxrwx 1 root system 18 Dec 14 1992 \
/etc/shutdown -> /usr/sbin/shutdown
114% ls -l /etc/motd
-r-xr--r-- 1 bin bin 880 Dec 14 1992 /etc/motd
125% cat hi
:
echo "hello"
126% ls -l hi
-rw-r--r-- 1 bhunter system 16 Feb 18 10:13 hi
127% ./hi
./hi: The file access permissions do not allow the\
specified action..
128% chmod +x hi
129% hi
hello
130% ls -l hi
-rwxr-xr-x 1 bhunter system 16 Feb 18 10:13 hi
131%
What did you learn? You can’t execute a file that doesn’t have execute per-
missions, and you don’t get them automatically.
33% whoami
bhunter
34% chmod 000 /etc/motd
chmod: /etc/motd: Operation not permitted.
35% ls -l /etc/motd
-r-xr--r-- 1 bin bin 880 Dec 14 1992 /etc/motd
36%
128% chmod +x hi
130% ls -l hi
-rwxr-xr-x 1 bhunter system 16 Feb 18 10:13 hi
131%
Syntax
chmod [ -R ] [ -f ] [ [ u ] [ g ] [ o ] | [ a ]
] { {- | + [r ] [ w ] [ x ] [ X ] [ s ] [ t ] ] }
| { = [ [ r ] [ w ] [ x ] [ X ] [ s ] [ t ] ] } } {
File ... | Directory ... }
chmod modifies the read, write, and execute permissions of files and modi-
fies the search permission codes of directories. Permissions can be defined
symbolically or numerically. When you use the symbolic mode to specify
permission modes, the first set of parameters selects the permission field,
as follows:
u File owner
o All others
a All — user, group, and others. This has the same effect as specifying
the ugo flags. The a flag is the default permission field. If the
permission field is omitted, the default is the a flag and the file creation
mask (umask) is applied.
= Clear the selected permission field and sets it to the mode specified.
If you do not specify mode following =, the chmod command removes
all permissions from the selected field.
r Read permission.
w Write permission.
s Set User-ID or Set Group-ID permission to that of file. This flag is only
valid with the u and g flags of the permission field.
t The sticky bit, sets link permission for directories, saves text attributes
for files.
The chmod command also permits you to use octal notation to set each bit
in the permission mode. chmod sets the permissions to the permission code
you provide. This permission code is constructed by combining (the logical
OR) of the following values:
1000 Sets the link permission to directories or sets the save text
attribute for files.
umask creationmask
"creationmask specifies the value of the file mode creation mask. The cre-
ationmask parameter is constructed by logically ANDing file permission bits
defined in the sys/mode.h file. Nine bits of the creationmask parameter
are significant.”
If the default mask were 000, every file would be -rw-rw-rw. If you set
your mask to 022, every file you create will be -rw-r--r--.
159% cd
160% grep umask .??*
.loginx11:umask 022
161% touch whatmask
162% ls -l whatmask
-rw-r--r-- 1 bhunter system 0 Feb 18 12:26 whatmask
This is the case of ordinary files. Directories are different. They have the x
bits set.
165% cd junk
166% mkdir null
167% ls -ld null
drwxr-xr-x 2 bhunter system 512 Feb 18 12:28 null
168%
% ls -l
total 2
-rw-r----- 3 bhunter users 0 Jul 14 07:27 snarg
drwxr-x--T 2 bhunter users 512 Jul 14 07:55 sticky
% umask
027
umask 022
set history = 80
set filec
set notify
set cdpath=( $home )
set prompt="$HOSTNAME""[\!]%"
Be safe and source .cshrc and then make a new file and list it.
fri2006[21]%source .cshrc
fri2006[22]%touch umsk
fri2006[23]%ls -l umsk
-rw-r--r-- 1 bhunter users 0 Jul 14 08:24 umsk
fri2006[24]%umask
22
When you leave your system, for any reason, you should log out or lock
their terminal. Some software, like X Windows, has a built-in lock com-
mands. The most frequently used is xlock. The xlock command locks an
X server until the user enters a password at the keyboard. While xlock is
running, all new server connections are refused. The screen saver is dis-
abled, the mouse cursor is turned off, the ability to toggle to another shell is
disabled, the screen is blanked, and a changing pattern is displayed. If a key
or a mouse button is pressed, a prompt asks for the password of the user
who started xlock.
This file should be hidden in the local user’s home directory and must be
owned by the user. The permissions of the .rhosts file should be set to
mode 600. The format of the $HOME/.rhosts file is:
HostNameField [UserNameField]
When a remote command executes, the local host uses the local system’s
/etc/hosts.equiv and $HOME/.rhosts file to validate the remote host
and remote user.
+@domain user_name
+ # A Giant No No
HostName
-HostName
+@NetGroup
-@NetGroup
A + signifies that any host on the network is trusted and that can never be
the case!
The HostName entry is the name of a remote host and signifies that any
user logging in from HostName is trusted. A -HostName entry signifies that
the host is not trusted. +@NetGroup or -@NetGroup entry signifies that all
hosts in the netgroup or no hosts in the netgroup, respectively, are trusted.
login:
and then
password:
Did it work?
If you doubt your login prompt, enter a newline to it and the password: re-
quest.
-rwxr-xr-x
drwxr-xr-x
Quiz
The shells allow you to flip flop between full duplex and half duplex with the
stty command changing the echo attribute.
#!/bin/sh
echo "Name: \c"
read name
echo "Password; \c"
stty -echo
read password
stty echo
...
% cat wrapper
#!/bin/sh
stty -echo
./noecho.pl
% cat ./noecho.pl
#!/usr/intel/bin/perl
print "Name: ";
$name = <>;
print "\nHi $name"
% ./wrapper
Name:
Hi Bruce
#!/usr/intel/bin/perl
print "Name: ";
$name = <>;
chomp $name;
print "Password: ";
$password = ‘getpw‘;
print "\nname: $name password: $password\n"
#!/bin/sh
stty -echo
read password
stty echo
echo $password
% !$
getpw.pl
Name: Bruce
Password:
name: Bruce password: Sol205*GS
Physical Security
There is a lot more to security than permissions and commands. Lock your
car and leave the keys in the door lock. How secure are you? Leave tapes
or floppy disks around, and how secure are you? The answer is obvious.
Lock up all tapes, diskettes, printouts, and so on. If there is a library where
they belong, be sure that they are there. If a terminal or workstation is in a
locked area, be sure the door is locked and closed.
UNIX
Networking
UNIX Part I
section10
Rev 1/13/99
ECT001118
Examples:
Protocols
You’re going to send a letter. You write it and put it in an envelope.
Then you write the following information on the envelope.
sender
sender address
recipient
recipient address
Then you mail it. At the post office, your letter is sorted and put in a
bag. The bag is containerized with other bags and shipped.
Sender 32c
Sender_Address
Recipient
Recipeint_Address
application
user applications
presentation
applications preparation
session
applications preparation
transport
port-to port transporation
network
internetwork routing
data link
data preparation and network linkage
physical
wire or cable transmission hardware
Each layer has its own name, addres type, and data form.
virtual path
applications
4 4
actual path
3 3
2 2
Physical
Electrical Path
TCP/IP-Ethernet
The Ethernet
The lowest layer is the Ethernet layer. It uses a 48-bit (6-byte)
address. The job of the Ethernet is to get the data out on the wire
and have it received only by the addressee. This is done by each
node reading the destination address and accepting it if the frame is
its own.
CSMA/CD
The Ethernet operates on the principal of CSMA/CD, carrier sense,
multiple access with collision detection. It senses if there is any traf-
fic on the wire (media) which acts as an antenna. If there is not it is
free to send. In the event of a collision, it will back off and resend
later.
All of this takes place in the lowest layer, the physical layer and is
handled, in hardware, by the MAU (media access unit) and the NIC
(network interface card.)
Internet/Ethernet Addresses
The internet and associated ethernet addresses are stored in the
system’s arp table.
bash-2.00$ arp -a
a8fs123 (132.233.123.160) at 0:0:3c:4:13:f2
a9fs123 (132.233.123.161) at 0:0:3c:2:48:32
giga2-123 (132.233.123.2) at 0:c0:80:8f:8d:74
sub123-gw1.fm.intel.com (132.233.123.100) at
0:e0:f7:41:9:41
a11fs123 (132.233.123.165) at 0:0:3c:2:77:82
giga1-123 (132.233.123.214) at 0:c0:80:89:18:af
a13fs123 (132.233.123.166) at 0:0:3c:1:f4:b2
a1fs123 (132.233.123.150) at 0:0:3c:1:dd:92
a19fs123 (132.233.123.151) at 0:0:3c:4:40:e1
a14fs123 (132.233.123.167) at 0:0:3c:1:ed:12
flc2002 (132.233.123.23) at 8:0:20:80:18:5e
a3fs123 (132.233.123.152) at 0:0:3c:4:7b:22
a5fs123 (132.233.123.154) at 0:0:3c:2:59:2
a4fs123 (132.233.123.156) at 0:0:3c:2:5a:a2
Internet Addressing
The use of Ethernet addresses is the only method to get local deliv-
ery. To get from one network or subnetwork to another requires
internetworking and, therefore, an Internet address.
Data in the Internet moves from net to net. Once on a local network,
it is delivered on the basis of its Ethernet address.
Internet
Ethernet
Ethernet Encapsulation
Ethernet frames are encapsulated to the Ethernet standard. The
frame information holds the sender’s and recipient’s address, the
type, the data, and a cyclic redundancy check. This envelope is put
on by the link layer as the data leaves a node, and it’s stripped off as
it enters a node. The recipient address is read by every node on the
segment, and the entire frame is accepted or rejected on the basis
of the address.
Concentrators / Hubs
Using Etehrnet cable as te prmary carrier is not without problems so
in most mid- to large-scale systems it has been replaced with hubs.
Hub
Quiz
A Simple Network
server
workstation
Every system on the network is called a node. The Ethernet cable acts as an
antenna sending to all nodes.
backbone
s1 s2
R
sn1
sn2
The servers are connected to the subnet segments without the need of the
router. The router connects the segments and is capable of joining a half
dozen subnetworks and connecting to the backbone as well.
Quiz
svr
With all three commands, you must have a login on the other
machine. With NIS (YP) running within a domain, and your file sys-
tems mounted to all systems by NFS, you not only will have an
account on all systems, but also your environment will appear to be
the same on all systems.
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
file_name
or
machine_name:file_name
as in
rcp is used to copy one or more files between the local host and a
remote host, or between two remote hosts, or between files at the
same remote host.
.rhosts
.rhosts is in every user’s home directory. Its permissions must be
0600.
13% ls -l .rhosts
-rw------- 1 bhunter uucp 14Mar 221993 .rhosts
This entry allows this user access from any mcd system.
rlogin
rlogin connects your system with any remote system that you
have permission to reach.
A few flags:
-l user changes the remote user name to the one you specify;
otherwise, your local user name is used at the remote host.
.rlogin Security
The remote host allows access if one or both of these conditions are
met:
rsh
rsh syntax
rsh [-l username] [-n] hostname [command]
rsh hostname [-l username] [-n] [command]
hostname [-lusername] [-n] [command]
Options or Flags
-l username
Use username as the remote username instead of your
local username. In the absence of this option, the
remote username is the same as your local username.
Example of rsh
38% rsh jeager who
bhunter hft/0 Jan 21 07:32
bhunter pts/1 Jan 21 07:32
bhunter pts/0 Jan 21 07:32
39%
Note the complaint, in spite of the fact that the system did the rsh
anyway.
telnet
and
ftp
telnet does a login to another host while ftp (file transfer proto-
col) transfers files between hosts.
telnet
telnet will log you on to any system that it can reach, providing
that you have an account there.
...
13% logout
[1] 14813
Connection closed.
telnet Syntax
telnet [ -d] [ -n tracefile]\
[ -e terminal_type][host [port] ]
telnet Flags
-e terminal_type
overrides terminal-type negotiation.
-n tracefile
records network trace information in the file specified
by the tracefile variable.
ftp
The ftp command puts the user in a virtual login on a remote
machine, where the user has a set of limited commands that allow
file manipulation and transfer.
ftp example
43% ftp fmsu04
Connected to mcd.
220 mcd FTP server (SunOS 4.1) ready.
331 Password required for bhunter.
Password:
230 User bhunter logged in.
ftp> get /etc/motd
/etc/motd: Operation not permitted.
ftp> get /usr/system/bhunter/foo
200 PORT command successful.
150 ASCII data connection for /usr/system/bhunter/
foo (132.233.71.32,1053) (82 bytes).
226 ASCII Transfer complete.
92 bytes received in 0.12 seconds (0.7123 Kbytes/s)
ftp> quit
221 Goodbye.
44%
ftp syntax
ftp [-d] [-g] [-i] [-n] [-v] [HostName]
The FTP protocol allows data transfer between hosts that use dis-
similar file systems.
ftp Subcommands
If you execute the ftp command and do not specify the host name
of a remote host, the ftp command immediately displays the
ftp>
prompt and waits for an ftp subcommand. The ftp command inter-
preter, which handles all subcommands. They are entered at the
ftp> prompt To end an ftp session when you are running interac-
tively, use the quit or bye subcommand or ctrl-D key sequence.
![command [parameters]
This invokes an interactive shell on the local host.
?[subcommand]
This displays a help message describing the subcommand.
If you do not specify a subcommand parameter, the ftp command
displays a list of known subcommands.
cd remotedirectory
Thischanges the working directory on the remote host to th
remotedirectory.
close
Ends the file-transfer session, but does not exit the ftp command.
dir [remotedirectory][localfile]
Writes a listing of the content of the specified remote directory
to the specified local file.
another architecture
more memory
In the window for that system set the DISPLAY variable to your
system
On your system
bash-2.00$ xhost +
1346-207 access control disabled, clients
can connect from any host
bash-2.00$ hostname
frx056
NFS
NFS or Network File Systems allows a host to get files from another
host, usually a file server, as if the files and file systems were
mounted on the local host. NFS is transparent. Users do not have to
know that it is there as long as it works well, which it almost always
does.
44% df
Filesystem Total KB free %used iused %iused Mounted on
/dev/hd4 4096 944 76% 857 83% /
/dev/hd2 196608 11648 94% 12384 25% /usr
/dev/hd9var 12288 1720 86% 335 8% /var
/dev/hd3 8192 7880 3% 28 1% /tmp
/dev/lv00 200704 194248 3% 16 0% /usr/work
fmsu01:/misc 819342 150688 81% - - /misc
fmsu05:/usr/fmo/ssd 944238 90594 90% - - /usr/fmo/ssd
fmsu01:/usr/spool/mail 408391 292557 28% - /usr/spool/mail
fmsu01:/amoeba 408399 262086 35% - - /mproj/amoeba
fmsu01:/sam16u3 1893612 252410 86% - - /uproj/sam163
fmsu05:/samgr 917358 63802 93% - - /gproj/sam16
fmsu01:/g16v1 1836252 754996 58% - - /vproj/sam161
fmsu05:/g16v 1278413 74744 94% - - /vproj/sam16
fmsu01:/sam16u2 1835996 412575 77% - - /uproj/sam162
fmsu01:/sam16u1 917870 206056 77% - - /uproj/sam161
fmsu05:/sam16u 1968100 134997 93% - - /uproj/sam16
fmsu05:/sam16 1278413 177015 86% - - /mproj/sam16
fmsu01:/sam16m1 917870 49078 94% - - /mproj/sam161
fmsu05:/g16 1278413 271993 78% - - /gproj/sam161
fmsu05:/samgr2 1298653 499607 61% - - /gproj/sam162
fmsu05:/e2 944238 739466 21% - - /e2
fmsu01:/28u008s 508958 93717 81% - - /gproj/28u008s
fmsu05:/usr/mfg 638430 37771 94% - - /usr/mfg
fmsu05:/usr/local.gen/ibm 472111 57203 87% /usr/local
fmsu05:/usr/system 1836252 337304 81% - - /usr/system
fmsu05:/tapeout2 1240333 264423 78% - - /usr/tapeout2
fmsu01:/tapeout3 918382 172715 81% - - /usr/tapeout
Note that most of these systems are mounted from a file server.
They will appear local in spite of their being remotely mounted . The
mechanism are daemons at either end. Here is part of the output of
ps.
45% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 6:50 /etc/init
.
root 6206 3095 0 Jan 16 - 0:13 /usr/etc/ypbind
root 6465 3095 0 Jan 16 - 0:02 /usr/etc/biod 6
root 6724 6465 0 Jan 16 - 0:02 /usr/etc/biod 6
root 6981 6465 0 Jan 16 - 0:01 /usr/etc/biod 6
root 7238 6465 0 Jan 16 - 0:01 /usr/etc/biod 6
root 7495 6465 0 Jan 16 - 0:02 /usr/etc/biod 6
root 7752 6465 0 Jan 16 - 0:02 /usr/etc/biod 6
root 8010 3095 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8267 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8524 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 8781 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9038 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9295 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9552 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 9809 8010 0 Jan 16 - 0:00 /usr/etc/nfsd 8
root 10067 3095 0 Jan 16 - 0:00 /usr/etc/rpc.mountd
root 10326 3095 0 Jan 16 - 0:00 /usr/etc/rpc.statd
root 10584 3095 0 Jan 16 - 0:00 /usr/etc/rpc.lockd
.
The nfsds are NFS daemons, and the biods are I/O daemons for
NFS to provide buffering.
NFS Problems
The biggest problem that you will ever have with NFS is the network
or the server being down. You will get a stale file handle error mes-
sage and perhaps some other console error like YP server not
found. Your SA probably knows already, but be sure.
AFS
AFS is vaguely similar to NFS and is used widely here at Intel. While
it is more complex in its mounting it is low in its profile. Watch these
cd and pwd results.
% cd /fes/bhunter
% pwd
/fes/bhunter
% cd ~
% pwd
/usr/users/a5fs/bhunter
NIS (YP)
YP provides a service to all systems that subscribe to it. It gives
tham maps of everything that YP is set up for. Typically YP provides
maps for
passwd
hosts
group
services
netmasks
rpc
ypservers
46% ypwhich
fmsu05
YP Commands
ypcat see a YP map
Problems with YP
There are multiple YP servers, so if one goes down, another will
take over. Although it is unlikely you will lose all YP servers without
losing the net (or at least the file servers), if you do you, will get this
message
Sendmail
47% ps -ef
USER PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan 16 - 6:52 /etc/init
root 1721 3095 0 Jan 16 - 0:00 /etc/writesrv
root 2032 1 0 Jan 16 - 2:11 /etc/syncd 60
root 2740 1 0 Jan 16 - 2:38 /etc/cron
root 3095 1 0 Jan 16 - 0:00 /etc/srcmstr
root 3570 1 0 Jan 16 - 0:00 /usr/lib/errdemon
root 3785 3095 0 Jan 24 - 0:00 /etc/syslogd
root 4644 3095 0 Jan 16 -0:00 /usr/lib/sendmail -bd -q30m
root 5050 1 0 Jan 16 - 0:00 /etc/uprintfd
root 5163 3095 0 Jan 16 - 1:13 /usr/etc/portmap
root 5422 3095 0 Jan 16 - 0:00 /etc/inetd
root 5683 3095 0 Jan 16 - 0:40 /usr/sbin/snmpd
DNS
Domain Name Service resolves host names to Internet addresses.
Locally host names are resolved from the file /etc/hosts or YP's
hosts maps. An organization with 25,000 nodes would need a
25,000 entry-hosts, file and keeping it up to date is impossible.
DNS solves the problem by having DNS servers inside and outside
the local domain (or zone as it is called in DNS) service a request for
name resolution to a local resolver.
mil
org
com
cisco
ora
intranet
epg flash
it
ccad
fmec
Quiz
NIS
YP
DNS
NFS
What is
a RPC,
a file handle?
X Windows
ECT001118
Revised 7/13/99
UNIX Part I
section 11
X and Productivity
The X Windows system is a major productivity tool. It lets you do more by:
Gives access to systems with the power, space, speed, or architecture you
need to get your jobs done quickly
Giving you a screen that is comfortable for size, location, color, and font to
minimize fatigue.
title bar
maximise
menu
iconify
scroll
drag
History
The X Windows System was started at Stanford and was known as W. Its
technology was taken by Project Athena at the University of Massachusetts
and renamed X.
Starting X
To bring up X from a UNIX login session, type in
% runx
or
% startx
or whatever your sits or local administrator has called the start-up script.
You will see a blue-grey background when X starts. This is your root win-
dow.
After a few moments, you will have a console window and perhaps even a
working window.
console
xlock
46% !x
xlock
47% !x
xlock
48% !x
xlock
49%
Icons
Icons show a window that has been "put away for a while" and is repre-
sented with an icon. Double click on the icon to bring the window back.
xterm
There are three buttons at the top. The left-most button is used to pull down
the window menu. The right-most button is used to maximize the window.
The one next to it is used to iconify the window.
If you have an icon sitting in the root window, you can activate it by double
clicking on it.
Windows are resized by moving the pointer to the border and clicking on it,
then moving the border. Clicking on an object is called selecting.
console
To copy the text to another place, click middle when the cursor is where you
want the text. If you are moving it to an editor, be sure the editor is in append
or insert mode.
Click middle on your mouse to dump text. In NT this is a copy (^C) and
paste (^V) .
This is not the path you have set up in your home dot files. Those paths
looked like:
Now let’s fall back and see how it all happened. The key path directory is
/usr/local/bin This is where you picked up the x11 command.
41% ls -l /usr/local/bin/x11
Frwxrwxr-x 1 102 staff 4238 Jul 06 /usr/local/bin/x11
42%
#! /bin/csh -f
set XDIR=/usr/bin/X11# op-systems’s X directory
set LXDIR=/usr/local/X11/bin# local X directory
setenv DISPLAY “unix:0”
echo “DISPLAY = $DISPLAY”
xrdb -load <<XRDB
Mwm*configFile: ~/.mwmrc.aix
XRDB
$XINITRC >& /tmp/$USER.x11
reset -I
• Runs .xinitrc.
Most of the action occurs when this script invokes your local xinitrc file
#!/bin/csh
mwm &
sleep 1
if (-e $HOME/.bitmap) xsetroot -bitmap $HOME/.bitmap -fg green -
bg SteelBlue
aixterm -g =80x37+0+163 -fn Rom14 -fg yellow -bg blue -i &
aixterm -g =80x69+200+0 -fn Rom10 -fg yellow -bg NavyBlue -i &
aixterm -g =80x25+0+163 -fn Rom10 -fg green -bg black &
aixterm -g =80x25+0+560 -fn Rom10 -fg green -bg black &
aixterm -g =132x25+0-100 -fn Rom10 -fg white -bg NavyBlue -i &
aixterm -g =80x25 -fn Rom12 -fg white -bg blue -i &
xbiff &
aixterm -name console -e ~mcdcad/bin2/console.aix
The key lines here are the line that runs mwm in the background and the
aixterm lines that set up your intial windows. mwm is your window man-
ager. It in turn invokes ~/.mwmrc.aix which creates your windows, sets
their characteristics and looks like:
Menu RootMenu
Getting
Help
With
UNIX
UNIX Part I
section 13
ECT001118
Revised 1/13/99
The Manual
man keyword
You can buy you own set of BSD manuals published by Berkeley and the
SVR4 manual set (in pieces) published by Prentice Hall.
EC
Engineering
Computing
ECT001118
Rev 0
11/1/99
Sect. 12
What Is EC?
Engineering Computing is the engineering arm of IT. EC provides the
resources to do Intel’s technical work. Each major campus has its EC orga-
nization. For example Folsom’s is FMEC. Within each local EC organization
are individual support groups:
You will become the most familiar with the help desk, CSS.
http://fmec.fm.intel.com
EC Computers
Workstations
Systems like Suns are provided for special purpose jobs like design but the
preferred system is the SIR box. This system boots to NT and provide UNIX
access through Exceed and SAMBA.
The SIR system comes loaded with almost everything an engineering cus-
tomer could want. The NT Resource Kit is loaded as well as most of the
freeware that we are used to on UNIX such as bash and the major GNU fil-
ters and other freeware.
SMS
SMS is a menu-driven NT window that allows you to pick and install soft-
ware for your system. If it is not in SMS and your organization regularly uses
it, contact your key contact to request adding it to SMS.
Compute Servers
The compute servers are the back-end muscle of our computing. The older
systems are AIX while the newer ones are HPs. An interesting addition are
a few hundred Intel systems running LINUX.
Windows 2000?
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
runx
runx is a simple script used to start your X-Windows session. To use it sim-
ply type
runx
chpasswd
chpasswd is a tool to change your UNIX and AFS password. It should be
used in preference to yppasswd. To use it type
chpasswd
rics
rics is a script that will seek out the best possible UNIX compute server for
your Exceed session. When you run Xsession to initiate an Xstart you will
see it in the command window.
The -idt flag is the user’s group. Yours will probably be different.
Netbatch
NetBatch Terminology
Related Commands:
nbs
nbq
nbstat
nbqstat
nbqrm
nbslot
Hummingbird eXceed
SAMBA
SAMBA mounts your UNIX file tree to your NT file tree. UNIX files and
directories can be reached from NT.
AFS uses a client/-server model, where all the data is stored on file server
machines. Files are transferred to client machines as necessary and cached
locally, on disk.
AFS Features
Scalable
It can accommodate N * 105 users
Secure
Uses Kerberos authentication
Cached
Data accessed many time is cached locally gaining speed and relieving the
network
NFS AFS
Hardware
Each AFS cell consists of file servers and data-base servers. Our FS to DB
server ratio is approximately 2:1. For example Folsom has 9 file servers and
4 data-base servers.
The data-base servers are the home of the Volume Location Data Base or
VLDB which is used to find the data on the file servers.
Volumes
UNIX divides the hard disks into partitions. AFS divides the partitions into
subsections named volumes. Each volume houses a subtree of related files
and directories.
Tokens
AFS doesn’t use UNIX UIDs for authentication. To access files which are not
world accessible using AFS, you must have a valid AFS token. You may see
what tokens you currently hold using the tokens command
% tokens
--End of list--
You may change your AFS password from any machine within the cell using
the kpasswd or chpasswd commands. The kpasswd change takes effect
throughout the cell immediately. To make your life a little easier use your
UNIX password and change them both art the same time with chpasswd.
File protections are not the same in AFS as in UNIX. AFS adds to the stan-
dard UNIX file protection, with an access control list or ACL (a little like NT
using NTFS.)
ACLs are the main mechanism used to control access to files and directo-
ries. Only directories have ACLs in AFS; access to files in a directory is con-
trolled by the ACL of the directory.
When you create a new subdirectory, it automatically inherits the ACL of the
parent at that time. Changing the parent ACL later will not change the child.
ACLs override most all of the UNIX mode bits. The UNIX mode bits set on a
directory have no effect in AFS but the user bits do have meaning. These
file bits may be used to further restrict access to a file that the ACL allows
access to.
fs sa /afs/scmpd/foo/bar jrambo di
The fs Command
The general and administration AFS command is fs. This command is
effectively a suite of commands AFS commands are found in the fs man
page. Commands in bold are user commands.
. fs Commands are
:
apropos search by help text
Tivoli
The Tivoli was developed by Tivoli Systems, Inc., It runs monitors on syn-
chronously or asynchronously. Tivoli retains data from previously executed
monitors which is used later to determine the action to perform Software
monitors are known as modules.
The event console is used for event management and is rules based. The
rules shows how Tivoli should handle events. Tivoli permits support EC per-
sonnel to log information about events, act on them, and then close them.
Troubleshooting the event is done by opening windows on the UNIX system
reporting the problem. It can send e-mail, page support personnel, and gen-
erate trouble tickets.