You are on page 1of 90

History of Unix

1970-1974, Simple is beautiful


early stage of AT&T Bell Lab, on PDP-11 machine
Unix is not an acronym, but a weak pun on MULTICS
1976, first licensed release, Version 6
1978, first portable version, Version 7
1979, Berkeley 3BSD
1983, System V as industry standard
1984, Microsoft releases Xenix
1986, BSD 4.3, AT&T Version 9
1987, SVR4, Mach,
1993, Linux
1
Unix Philosophy
Easy to program by combining small building blocks
Not (naive) user friendly
A clean minimal design (nothing extra, nothing unnecessary)
Open access:
Provide tools and mechanism to combining the tools
Minimal restrictions to the ways of doing things
A user can be very creative (and frustrated).
Major Unix OS features:
Kernel
Shell
File System

2
Unix Operating System Structure
OS mediates between the user and the computer
User

Application Programs

Shell
Kernel

Hardware
3
Kernel
Manages memory and allocates it to each process
Schedules work done by the CPU
Organizes transfer of data from one part of machine to
another
Accepts instructions from shell and carries them out
Enforces access permission on the file system

4
Shell
Command interpreter
Create customized environments
Write shell scripts
Define command aliases
Manipulate command history
File and command completion
Edit the command line

5
File System
Logical method for organizing and storing large amounts of
information.
Easy to manage.
File: basic storage unit.
Types:
ordinary file (stores info)
directory (holds other files and directories)
special file (represents physical devices like printers, terminals, etc)
pipe (temporary file for command linkage)

6
UNIX: Multi-user Multi-tasking
More than one user can run at the same time and more than
one task can run at the same time
Unix is multiuser multitasking, Window NT is, Windows is not.
In Unix, each program is started as a process.
A process is a program in execution.
Usually only one copy of a program, but there may be many
processes running the same program.
To each interactive user (window):
only one process in foreground
may have several processes in background

7
Processes
kernel Process 0: Kernel bootstrap. Start process 1.
kernal mode
user mode Process 1: create processes to allow login.
/etc/init

fork fork
inetd exec exec
lpd
/etc/getty /etc/getty condition terminal for login
httpd
exec exec
/bin/login /bin/login check password
exec exec
shell shell command interpreter

8
Unix Process
Init process
last step in booting procedure
create other processes to allow the users to login
Getty process
conditions for terminal connection
wait for user-id
display login on the screen
Login process
check password with the uid
execute .profile or .login (depends on default shell)
display shell prompt
Shell process (command line interpreter)
Shell prompt ($, %)
9
UNIX Process
Process environment
Process id, parent-process-id, process-group-id
Opened files
Working directory
File creation mask
User ID, Group ID
Resource limits
Environment variables
Code
A child process inherits parents environment.

10
Processes
use ps to see the processes that you are running.
$ ps
PID TTY TIME CMD
221 pts/4 4:15 netscape
201 pts/4 0:05 bash
215 pts/4 1:15 emacs-19

use & to execute a task in background


Example: $ sort infile > outfile &
use ctrl-Z to suspend the foreground task, and then use bg.
use fg to move a background task to foreground.
11
Shell: Command Interpreter
Bourne Shell: the default shell (sh)
original unix shell
does not have interactive features of newer shells
widely used for writing shell scripts
standard on Unix System V
C Shell (csh): available on BSD Unix and most other systems
with syntax similar to the C language
with many enhancement over the Bourne shell.
Korn Shell (ksh): AT&Ts answer to C Shell
combine features of Bourne and C shells
very efficient
Other shells: tcsh, bash
12
Day-to-Day Use
Command Function Meaning
cat Display a file conCATenate
cp Copies a file CoPy
mv Renames a file or moves it MoVe
rm Delete files ReMove
lprSends a file to a printer Line Printer
lp (Sys V)
ls Lists the contents of a directory LiSt
chmod Changes the mode of permissions Change MODe
pwd Shows the current directory Print WorkingDir
cdChange current directory Change Dir
mkdir Create a directory MaKe DIR
rmdir Delete a directory ReMove DIR
psShows the processes on the system Process Status
man Shows info. about commands Manual
df Shows file system status Disk File
duShows the space allocation of files Disk Utilization
grep Search for patterns in files

13
Standard Command Format
command [options] [arguments]
wc [-c | -m | -C] [-lw] [file ]
stuff in brackets is optional

boldface words are literals (must be typed as is)

italicized (or <> enclosed) words are args (replace appropriately)

Options modify how the command works


command [options] [--] [<file> ]
options ::= -option white-space options*
option ::= noargoption* argoption | noargoption+
noargoption ::= letter
argoption ::= letter whitespace string

$cc -po zap zap.c

14
Some Examples
ls [-alRF] file-list
a for listing all files including the dot files
l for long format (file type, permissions, #links, owner, group, etc)
R for recursive, list subdirectories.
F for listing directories with a trailing /
ps [<options>]
List the information about running processes
Example:
%ps -el # print the info about all processes (e) in the long format (l)

15
On-line Documentation
For shell command, system programs, and library functions.
%man [n] <command>
e.g.
%man wait %man 1 wait
%man man %man 1 man
%man -k <keywords>
Man(ual) page format
Name
Synopsis
Description (options, defaults, detail desc., examples)
Files
See Also
Bugs

16
I/O Redirection
Redirection and Pipe
> redirects standard output (screen) to a file
E.g. ls > dirlist
< redirects standard input (keyboard) from a file
E.g. sort < infile > outfile
| pipe output of program 1 to input of program 2
E.g. who | wc
Or getit < in | check_it | process_it | format_id > out
>> appends output to a file
E.g. ls -l >> oldfile
Exercise: find out the definition of tee.

17
Sequential vs. Concurrent Process
Sequential:
%date
%ps -ef OR %date; ps -ef; who
%who
Concurrent:
%pgm1 & prgm2 > file1 & pgm3
%make > log &
%sort +1 pdir; ((pr dir | lpr) & sort +1 local))

18
File Name Expansion
Each shell program recognizes a set of special characters
called meta characters.
The metacharacters are used to create patterns to match
filenames and command names.
Bourne and Korn shell meta/wildcard characters
* matches any string (including null)
? matches any one character
[a-dA-D] matches any one character in the range
[!AB123] matches a char not in this range
\ escape
~<username> : (not bourne shell) the home dir of the user.
19
File Name Expansion
Assume we have the following files under the current
directory: 120, 235, 2#5, a.out, c22, c*2, Doc.1, Doc.2, Doc.3,
one.c,two.c, three.c

ls *.c ls c*2
ls [a-z]*[!0-9] a.* *.*
ls ??? cd ~foo
ls *

20
Filters
Most UNIX utilities are filters
A filter is a program which
reads input (text) from standard input only
writes output to standard output only
writes error to standard error only
may use temporary files for intermediate results
Filters can be combined to work together using pipes
Pipe: takes stdout of one command and uses it as stdin of
another command
ls | wc

21
Command Alias
Assign your own name for a command
Syntax is shell dependent
alias ll ls -l C shell
alias ll=ls -l Korn, Bourne shell
Displaying the value of an alias
alias ll (displays value)

22
Unix File Systems
File: a collection of data
Just a sequence of bytes
no record or block structure required
Directory
A file that contains information for files
distinction between a directory and a file
system can alter the contents of a directory
rooted tree file structure (inverted tree)
directories can contain both files and other directories info.

23
Unix File System Road Map

/dev /etc /var /bin /tmp /usr /mnt /home


passwd
hosts spool adm include etc bin lib 5bin sue john
... printer messages
mail(in) wtmp
mail(out)
uucp

Special files: /dev/* represents I/O devices.

24
File Systems and the I-nodes
Each disk drive contains one or more file systems
Each file system occupies a number of cylinder groups.
Each file system has a superblock, an i-node table and files
The superblock is at a fixed location relative to the beginning of
the file system. It contains a description of the file system.
One can find the location of the I-node table thru superblock.
Each entry of the I-node table is an I-node, which uniquely
represents a file in the file system.

superblock I-node table file1 file2 free file3 free


An I-node contains uid, gid, time of creation, modification, and
access, permissions, location of the file on the disk.
25
Inodes
od -b .

0000000 4 ; . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 277 ( . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000040 390 = b l a h \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000060

A filename is a link (links name in directory hierarchy to the


inode, thus to the file contents)

26
Directories
Directory is an ordinary file
can be read as ordinary files (by any program that reads text)
cant be created or written as ordinary files (only system can)
od - octal dump
cat foo
hi there
welcome to unix
od -c myfile (output in decimal by byte pairs)
0000000 h i t h e r e \n w e l c o m e
0000020 t o u n i x \n
0000031
1st 7-digits are position in file, ordinal number of next character (in
octal)

27
Symbolic Links
Can have many links to the same file (inode)
rm - does not remove inode, removes directory entry (link)
Only when all links are gone is the file (inode) removed

ln -command for creating symbolic links


ln oldfile newfile (creates another link to the inode)

28
Permissions
Every file has a set of permissions associated with it
three types of permissions: read ( r), write (w), and execute (x)
three sets of permission: user, group, world.
In Unix system, users are identified by numbers:uid, gid
ls -l
-rwxr-xr-x 1 root 3743 Jan 4 1970 test

user group others #links owner size (time of last mod) (file name)

Problem: how do you write a program which allows different


users to access a set of files? E.g. the program passwd

29
Permissions (cont.)
Solution: use the set-uid bit
When a user execute a program with the set-uid bit set, the
user assume the identity of the owner of the program.
For example
ls -l /bin/passwd
-rwsr-xr-x 1 root 8454 Jan 4 1994 /bin/passwd
set-uid
Set-uid bit may break the security wall. (users can run the
/bin/passwd and act like root)
Only special programs can be set-uid program, particularly if
the owner is root.

30
Chmod
Change the access permissions of a file
chmod <permissions> <filename>
permissions can be specified as 3 octal digits, <user,group,others>,
the three bits of an octal means r,w,x
Example: chmod 755 test
permissions can be specified as +x, or u+x, or g+r,
chmod +s test sets the set-uid bit for file test.
If a directory has x in its permision, the dir is searchable, ie.,
one can do ls on the directory.

31
Pathnames
Every file and directory in the file system can be identified by
a full path name (route from root to file)
/home/sue/email/f1
/
Relative path name
location relative to current directory home
. Current directory
.. Parent directory sue
fred
if cwd is /home/sue:
ls email
ls ./email docs email
cd ..
ls ../fred f1 f2

32
Mounting File Systems

/dev /etc /var /bin /tmp /usr /mnt /home

a file system

A file system must be mounted before it can be used


Root file system is mounted at boot time.
A file system can be mounted to a dir. of another mounted fs.
Mounting is done in memory
33
Whats Next?
Shell scripts!

But first, some details

34
Some Details
cp [-ir] file1 file2
cp [-ir] file-list directory
i for interactive. prompt use whenever a file will be overwritten
r for recursive. copy a directory tree
ls [-alRF] file-list
a for listing all files including the dot files
l for long format
R for recrusive. list the all subdirectories.
F for listing directories with a trailing /
date [+format]
%date +%h %d, 19%y
Oct 1, 1996
35
Some Details (cont.)
wc file-list
display the number of lines, words and charaters
more file-list
Browse through text files on page at a time.
head [-n ] file-list
Display the first n lines of the files (default=10)
tail [+n|-n| -f| ]
Display the last few lines in the files (default = 10)
Example:
%tail +5 foo # display the last parf of foo starting from line 5
%tail -5 foo # display the last five lines of foo
%tail +30 foo | head -15 | more #display line 30-45 of foo
%tail -f foo # wait and display the new lines appended to foo

36
Some Details (cont.)
cut -c list file
cut -f list [-dChar] file
Cut out selected charaters or fields from each line of a file
Examples:
%cut -c 1-5,15-20 foo
# extract chars 1-5 and 5-20 from each line of foo.
%cut -f 1,3 -d moo # extract field 1 and 3 from each line of moo.
paste file1 file2
Concatenate corresponding lines of the given input files
Example (reverse two fields of the file abc)
%cut -f1 abc > abc1
%cut -f2 abc > abc2
%paste abc2 abc1 > xyz

37
Some Details (cont.)
grep, egrep, fgrep
grep [-nv...] pattern [file-list]
Search the input text files for lines matcing the pattern
%grep Unix doc.1 # Display the lines in doc.1 that contains Unix
%grep -n Unix doc.* # Display the lines with line numbers
%grep -v Unix doc.1 # Display the lines which do not contain Unix
sort [-tC] [-o outfile] [field-list] [file-list]
sort the files
%sort +1 list1 # sort list 1 starting from field 2 to the end of the line
%sort +2-3 list2 # sort list2 on the third field
%sort -n -o list4 list3 sort list3 numerically and place the output in list4

38
diff
diff file1 file 2
Display different lines that are found when comparing two files
It prints a message that users ed-lide notation (a - append, c -
change, d -delete) to describe how a group of lines has changed.
It also describes what changes need to be made to the first file to
make it the same as the second file.
Example
file1 file2 file3
apples apples oranges
oranges oranges bananas
bananas kumquats kiwis
peaches
39
diff (cont.)
%diff file1 file2
3c3
<bananas
-----------------
>kumquats

%diff file1 file3


1d0
<apples
3a3,4
>kiwis
>peaches
40
comm file1 file2
Takes two sorted text fiels and print common lines and lines which
appear exclusively in one file on separate colmns.
column1: lines only in file1, column 2: lines only in file2; col 3: comm
Example
file1 file2 %comm file1 file2
apple apple apple
banana cantaloup banana
grape grade cantaloup
orange kiwi grape
lemon kiwi
%comm -12 file1 file2
apple
grape

41
tr [-csd] pattern1 pattern2
Translate input character to output character based on the input and
output patterns
Example
%tr [A-Z] [a-z] <in >out
# xlate all letters to lower case.
%tr -s \012\011\040 \012\012\012 < in > out
# xlate blank, tab and newline chars to newline chars and squeeze (-
s) consecutive newline char into one
%tr -cs [a-z][A-Z] [\012*] < in > out
# change all non-aplphbetic (-c) chars to newline chars and
squeeze consecutive newlne char into one.
%tr -d \040 < in > out
# delete all blanks.
42
uniq [-cdu] file-list
Display a fiel, removing all successive repeated lines
Example:
file1: %uniq file1
apple apple
banana banana
banana apple
apple
banana
%sort fruit | uniq -c
apple 2
banana 3
%tr -cs [a-z][A-Z] [\012*] < fileA | sort | uniq
# show a list of distinct words in fileA.
43
find <dir-name> <exp>
Recursively search the directory tree rooted at <pathname> and find all
files whose names satisfy <exp>
There are many details in the expression.
Examples:
%find . -name \*.doc -print # list all files names ending with .doc
%find /etc/source -atime 2 -print
# print the names of the files under /etc/source whose lst access time
was 2 days ago.
%find . -name [a-z]* -exec rm {} \;
# remove the files under the curent directory whose names begin with a
lower case letter.
%find / \(-name a.out -o -name *.o \) -atime +7 -exec {} \;
# remove the object and binary executable files under the root direory
which have not be accessed more than 7 days.
44
cpio -i[cdv] -o[cBv]
System V file archive and backup progam
Example
%find proj -print | cpio -ocBv > /dev/rmt8
# cpio get file names from stdin. -o create archive which is
redirected to the tape device.
%find proj -print | cpio -ocBv > proj.cpi
# get file name from stdin and -o createsarchive which is
redirected to proj.cpio
%cpio -icdv *.c </dev/rmt8
# -i read from archive file from the tape device. -d creates
directories as needed.

45
tar [options] [file-list]
key := c (create) | t (table of content) |r (append the file) | u (update the
file)
options := v (verbose) | b (block) | f (file name follows) | m (use extraction
time as the mod file)
Create/extracting archive files for backup and transporting files
%tar cvf proj.tar proj # create archive file proj.tar from file or dir proj
%tar xvf proj.tar # extract files in proj.tar
% tar tf proj.tar # list of the filenames in proj.tar without extracting data.
%tar cf - proj | (cd /newproj/; tar xvpf -) # copy proj to the directory /newproj/.
p to keep all the protection mode.
cp -r copies a dir tree but all the time info is gone. Tar preserve the time
info.
%tar cbf 20 proj.tar /usr/local/proj # avoid using full path names. When you
extract the file, tar will insist to put fiels to /usr/local/proj.

46
uuencode & uudecode
Generate an ASCII encoded version of the give files
Example:
%uuencode file.bin newfile.bin > file.bin.uu
# encode file.bin and put the result in file.bin.uu
%uudecode file.bin.uu
# decode the file file.bin.uu and generate a new file newfile.bin
Sending a dir tree via email
%tar cvf proj.tar proj
%compress proj.tar # compress proj.tar to proj.tar.Z
%uuencode proj.tar.Z proj.tar.Z | mail qli
at the receiving end, extract the mail and save it in xx
%uudecode xx
%zcat proj.tar.Z | %tar xvf -
47
sed [-n] | [-e] sed_cmd file_lists
A stream editor. It copies the lines from the file_list or stdin to
stdout, editing the lines in the process.
Examples:
%sed -n /hello/p < input > output
# copy the lines contains hello. -n suppress stdout so only the
lines that matches are copied.
%sed 5,7d file1 # delete lines 5 to 7 from file1. File1 is unchanged.
%sed s/Unix/UNIX/ doc2
#replace the first occurrence of Unix in each line by UNIX.
%sed s/Unix/UNIX/g doc2
# replace all Unix by UNIX

48
awk [-f progfile] [-Fc] [prog] [files]
Pattern matching and stream editor.
Example:
Program awkexample:
BEGIN { linetype=0} # initialization
NR == 1 { print $1 $NF} # if it is the first line, print the last field
/^$/ { print This is an empty line }
/^Unix/ { printf(Line starts with Unix\n %s\n, $0); linetype=1; next;}
/NonUnix$/ { printf(End with NonUnix\n); linetype=0; next;}
linetype == 1 { print $0}
END {printf(%d lines processed\n, NR);} # finishing it up

49
awk (cont.)
Test data file (awktest):
Line1-field1 this is the las-field
This line should not show

Unix is simple and difficult


Hello world is very simple
Next blank line should show

I line some other NonUnix


This line should not show

Bye

50
awk (cont.)
%awk -F -f awkexample awktest
Line1-field1 las-field
This is an empty line
Line starts with Unix Unix is simple and difficult
Hello world is very simple
Next blank line should show
This is an empty line

End with NonUnix


This is an empty line
11 lines processed
Command line program: most of the awk commands can be used in
he command line.
%awk {print $2 $1} # exchange field1 and field2
51
Other Commands
chown [-R] <new-owner> <file-list>
Only owner and root can chown. (Root only in some systems.Why?)
Example:
%chown -R john testdir
# john becomes the owner of all files under testdir.
kill [-signal] pid
Sending the specified singal to the process. The process can be
programmed to catch most of the signals.
Examples:
%kill -1 1 # poke the init process to reinitialize (reading /etc/ttytab)
%kill -9 1234 # kill process 1234
Signals can be spefified by name, e.g., HUP is 1, KILL is 9.
%kill -l # lists signal names

52
Other Commands (cont.)
du [-as.] [dir list] [file list]
Reports the allocated dispace for each file and/or directory specified
-a lists all files, -s lists the grand total of each dir given
Examples:
%du -s # print the total disk space used by the files in and under the
current dir.
%du -s * # print the disk space used by each file and dir in the current
dir.

53
make
A tool for maintaining programming projects
make [-isrntqpd] [-f file] [macro-definition] [targets]
It allows the users to specify dependencies among different source and
binary files in his/her applications.
-i ignore error code returned by a command
-s silent mode
-r suppress built-in rules
-n no execute mode
-t touch target file
-q question before change
-p printout macro definitions and target descriptions
-d debug mode
-f alernative make file name
54
make (cont.)
Makefile:
prog: x.o y.o z.o
cc -o prog x.o y.o z.o -lm
x.o: x.c def.h
cc -c x.c x.c
y.o:y.c def.h x.o
cc -c y.c def.h
z.o:z.c prog y.o
cc -c z.c y.c
z.o
z.c

make does depth-first search on the dependency graph


55
Makefile format
a makefile containing explicit dependency lines in the
following format:
target1 [target2 ]: [dependency ] [; commands] [#commnets]
[<tab> commands] [#comments]
each command line start with a tab character, and no other lines
should start with a tab.
commands are bourne shell commands
a set of built-in rules are used by make, e.g.:
.c.o: $(CC) $(CFLAGS) -c $<
# if .o depends on .c and f.c is newer, compile f.c
Each command is executed by a different subshell.

56
make Macro
Syntax: Name=String. E.g. LIB=/users3/foo/lib
Predefined Macros for C
CC=cc
AS=as
CFLAGS= -O -g
LOADLIBS=
Built-in Macro (evaluated each time make reads a dependency line)
$* - the basename (suffix removed) of the current target
test.o : test.h
cc -c $*.c # cc -c test.c
$@ - the full target name
$? - the list of dependencies that are newer than the target
libops: ineract.o shed.o gen.o
ar r $@ $? # put any .o files newers than libops into libops
$$ - the $ sign.
57
make -- Suffix Rules
make uses some conventions to simplify the makefille. Example:
prog: x.o y.o z.o
cc -c .
# make finds files which can generate the .o files. Eg. x.c. If x.c is newer than
x.o, x.c is compiled.
Suffix rules are predefined, generalized descriptions:
.SUFFIXES: .o .c .s # define the suffix to be consdiered significant
.c.o:
$(CC) $(FLAGS) -c $<
.s.o:
$(AS) $(ASFLAGS) -o $@ $<
$< evaluates to whatever the dependecies triggered the rule. $* is similar
to $< except that the suffix is removed. Both are used only in suffix rules

58
An Example of make
MYPROG=/usr/local/myprog
INCLUDE=$(MYPROG)/include
BIN=$(MYPROG)/bin
LIB=$(MYPROG)/lib
CFLAGS= -g -I$(INCLUDE)
.c.o:
$(CC) $(CFLAGS) -c $*.c
HEADERS=interface.h dbms.h
SOURCE =driver.c interface.c dbms.c
OBJECT=$(SOURCE:.c=.o)
app: ($OBJECT)
$(CC) -o app $(OBJECT) -l$(LIB)
(continued)
59
make-example (cont.)
print:
@echo print source files # @ suppress the comman line printing
@for file in $(SOURCE) \
do \
pr -n $$file; \
# $$ to make a $-sign for the shell command
done
clean:
@rm -f *.o
Usage:
make app
make clean
60
Processes
Process: instance of a program
has unique pid.
Process environment
Process id, parent-process-id, process-group-id
Opened files,
Working directory, env
File creation mask code
User ID, Group ID,
Resource limits,
Environment variables
Code

61
UNIX Process
New process created each time you execute a command.
Current process (parent) forks a new process (child)
Child created as a foreground (wrt parent) process:
parent forks new child
parent deactivated, waits for child to die
parent reactived upon death of child
Child created as a background process:
parent forks new child
parent immediately resumes activity

62
Processes
use & to execute a task in background
Example: $ sort infile > outfile &
ps - list processes.
jobs - list background processes.
ctrl-C (cancel foreground job) ctrl-Z (suspend foreground job)
bg - move (suspended) job into background.
fg pid - move background job to foreground.
kill pid - kill the process
-1 (kill process, and children)
-9 (kill process, may leave children alive)

63
Shell Process
Upon login:
shell process created
Any command you type at prompt:
new child of your shell process
What is your current shell?
%echo $SHELL
How to switch to another shell?
%bash just type shell name
How to switch login shell?
%chsh user newshell (but wont work here)

64
Shell Metacharacters
> prog > file direct stdout to file
>> prog>>file append stdout to file
< prog<file take stdin from file
| p1|p2 connect stdout of p1 to stdin to p2
* match string of 0 or more characters
? match any single character
[ccc] match any single character from ccc
ranges like 0-9 or a-e are legal
; command terminator
& background process
65
Shell Metacharacters
`` run commands the output of produces
() run commands in in a sub-shell
$1, $2 arguments to shell file
$var value of shell variable var
\ \c take c literally (dont evaluate)
take literally
take literally, after $, ``, \ interpreted
# comment
var=val assign variable var
p1 && p2 run p1, if successful, run p2
p1 || p2 run p1, if unsuccessful, run p2
66
Whats going on?
What processes, programs, pipes, and files are used?

%cat doc1 > out1; wc -l <out1

%grep root /etc/passwd >OUT2 ; cat <IN >OUT &

%cat doc1 | wc -l

67
Whats going on?
% date ; who | wc
Wed Sep 24 16:00:00 PDT 1997
4 24 182

| higher precedence than ;

% (date ; who ) |wc


5 30 211

68
Whats Next?
Shell scripts!

69
Guidelines for writing Unix
Commands/Tools/Scripts
Standard command format
Recognize meta-characters (handle multiple files)
Standard I/O (stdin,stdout, stderr. If file arg is absent use std)
Keep messages and prompts to a minimum.
Provide verbose options
Input/output data should be text whenever possible.
Use dot files and environment variables for frequently used
info.
Use standard library and tools to save coding effort.

70
Shell Script
Bourne Shell/Korn Shell
Topics:
pass arguments
global and local variables
macro
functions
Invoking a shell script
$shell_script_file or $sh -options shell_script_file
the script file must have execute-permission.

71
Environment Variables
.profile

PATH=.:$HOME/bin:/bin:/usr/bin:/usr/local
TERM=vt100
export TERM PATH

- HOME -- home directory


PATH -- where to look for commands
TERM -- terminal type for programs such as the editor
CDPATH -- shortcut directories for cd command
PS1 PS2 -- prompt
72
A shell script example
The script file loginfo (must be executable):
echo The current date and time: \c
date
echo The number of users: \c
who | wc -l # count number of lines
echo personal info: \c);
who am i
exit 0
$chmod +x loginfo
$loginfo
The current date and time: Thursday October 10
The number of users: 7
Personal info: lseiter ttya .
73
Another Example
$cat shellvariables
#! /bin/sh
# show predefined shell variables
echo The number of arguments is $#
date &
echo The process id of date command is $!
wait
echo The process id of this shell is $$
grep root /etc/passwd
echo The return code from grep is $?
echo The current set of options is $-
$shellvariables one 2 xyz
$sh -a shellvariables one 2 xyz
74
Positional parameters
set to shell script arguments. e.g.
$my_script a b xy 1 2 3
positional parameters have the values
$0 -- my_script
$1 -- a
$2 -- b
$3 -- xy
$4 -- 1 2 3
shift command:
$shift [n]
# parambers $n+1 $n+2 become $1 $2 .
# default value of n is 1
75
Using Shell Variables
Symbolic Variables
parameter = value
Examples:
I=5; Y=$I
# There should be no space around the = sign
# More than one command can be in a line separated by ;
x = hello there\n; y=Hello
echo $x
dir1=/usr/bob/doc
ls -a $dir1
echo x = $x

76
Quoting
string take string literally
$echo * $HOME
* $HOME
string take string literally, except $,`,\,,
\c take c literally

77
Quoting & Compound Command
The Quotes from Hell
This is a string\n
The amount is $100.0
quotes all char except \ $ `
quotes all char except
command substitution (grave accent):
$echo Users currently on the system:\n `who`
$echo The banner command,\n `banner the banner`
Compound commands:
a pipeline, a list, a group (), a command that begins with a certain
reserved words: for, if, case, time,
I/O redirection applies to the complete command except a pipeline, a list
and the time command.
78
test Command or [ ]
if test $# -eq 0 if [ $# -eq 0 ]
then then
echo no positional param! echo no positional param!
fi fi

options:
-r | -w | -x | -f | -d file
# the file is readable, writeable, executable, a file, or a directory.
n1 -eq | -ne | -gt | -ge | -lt | -le n2
# n1 = | <> | > | >= | < | <= n2

79
Test Command
Options
File testing Numerical comparison String Comparison
-r file n1 -eq n2 str1 = str2
-w file n1 -ne n2 str1 != str2
-x file n1 -gt n2 str
-f file n1 -ge n2
-d file n1 -lt n2
n1 -le n

Logical Connectives
!
-a
-o

80
Parameter substitution
Parameter substitution
${param}
${param:-word}
value of param if defined, otherwise word,
param remains undefined
${param:=word}
value of param if defined, otherwise word,
if param undefined, param defined to word
${param:?word}
if defined, param, otherwise print word and exit shell
${param:+word}
word if param defined, otherwise nothing

81
Commands and Functions
Reserved word commands
[[ test_expression]]
if, case, for, select, while, until
command grouping:
(command_list)
{command_list}
Function definition
function id
{
compound_list
}

82
CX command
Suppose you want a command cx that will take a filename and
set its execute permission. For example,
$cx foo

Need to be able to get the filename from the command line


Shell variables: $1 (first argument), $2 (second), $* (all args)

chmod +x $1

83
Flow Control
General Format:
if command_list1 check exit code of the command
then 0 -> normal termination -> True
command_list2 non-0 -> abnormal termination -> False
elif command_list3
then if test -d /usr; then echo its a dir; fi
command_list4
. $if test -d /dir
else >then
command_listn > echo its a dir
fi >fi
>$
84
Looping
What does this command do?
$wc -l *

6 file1
10 file2
3 file3
19 total

What if you would like things formatted nicely?


There are 6 lines in file1
There are 10 lines in file2
There are 3 lines in file3
85
Loops
General Format:
while command(s) check return code of command,
do if 0, execute body and loop again
body
done

while test ! -s file1


do
echo file1 does not exist or is still empty
sleep 500
done

86
Loops
General Format:
until command
do
body
done

until who | grep $1


do
echo $1 has not logged in yet
sleep 500
done

87
Shell scripts
for var in listofwords
do
commands
done

for f in $*
do
x=`wc -l $f`
echo There are `echo $x |cut -f1 -d ` lines in $f
done

88
Examples
if test ! -f $1
then
echo First arg is not a file
if

if [$NAME = John Doe -a $BAL -gt 5000]


then
echo ok
else
echo not ok
fi

89
Examples
if test -f $1
then
echo First arg is a file
elif test -d $1
then
echo First arg is a directory
fi

90

You might also like