Upload
remuel-malinao
View
608
Download
3
Tags:
Embed Size (px)
DESCRIPTION
introduction to operating system
Citation preview
1
Operating Systems
Part II: Introduction to the Unix Operating System(Utilities and Shell Programming)
2
Unix Design Principles
Time-sharing system Supports multiple processes Simplicity is key -> kernel provides small set of
functionality Source code provided with O/S Designed by programmers for programmers
(i.e. programmable shell, make, SCCS)
3
Unix Design Principles
Unix later used for networking, graphics, real-time operation (not part of the original programming objective)
New functionality required large amount of code (networking, GUI doubled the size of the system)
Continued strength -> even with new development, system still remained Unix
4
Programmer Interface
(The users)shells and commands
compilers and interpreterssystem libraries
hardware
system interface to the kernel
kernel interface to the hardware
swappingdisk and tape driversvirtual memoryCPU scheduling
demand pagingfile systempage replacementsignals
kernel
5
The Unix Shell
Command interpreter – Most common user interface– Normally executes user-written and system
programs– Called shell in Unix because it surrounds the kernel
In Unix, users can write their own shell
6
The Unix Shell
Most popular Unix shells:– Bourne shell (Steve Bourne, prompt is ‘$’)– C shell (Bill Joy, most popular on BSD systems,
prompt is ‘%’)– Korn shell (Dave Korn, became popular because it
combines features of C and Bourne shells, prompt is ‘$’)
Unix GUIs: X, OpenView, Motif
7
The Unix Shell
Users can create programs using shell scripts (equivalent to batch files in MS-DOS)
Most popular Unix shells are also programming languages complete with variables and control constructs (loops, conditional, etc.)
Shell programming can be used to create another shell
8
Unix Commands
Commands all have the same structure Case-sensitive! (cd <> CD, unlike in MS-DOS) The number of commands kept small but each
command is extended through options/switches (preceded by ‘-’)
9
Unix Commands
Example– Basic directory command: ls (lists all contents of a
directory)– Extended command: ls -l (lists contents plus
other file information)
10
The Unix File System
Features of the Unix File System– Hierarchical structure (similar to MS-DOS)– Files are expandable (may grow are required)– Security rights associated w/ each file/ directory
Incorporates three-tiered structure on file access 9 bits (User-Group-Others, rwxrwxrwx) Access types (r - Read, w - Write, x - Execute, ‘-’ means
no access)-rwx---r-x 1 aam faculty 37 May 06 7:35 test
– Files may be shared (concurrent access)
11
The Unix File System
Features of Unix File System (continued)– Various information kept for each file (i.e. name,
location, size, owner, security, modified by, last access by, date & time stamp, etc.)
– File links - mechanism that allows multiple file names to refer to the same data on the storage device
12
The Unix File System
Most often used file system commands:– pwd (present working directory)– cd (change directory)– mkdir (create directory)– rmdir (remove directory)– ls (list directory contents)– cat (concatenate files) / more (pauses after screen
is full)– cp (copy file)
13
The Unix File System
Most often used file system commands:– vi (edit a file)– mv (move a file)– rm (remove file)– ln (create link)– chmod (change file access mode)– chown (change file owner -> can be done only by
“root”/file owner– cmp / diff (compare 2 files)
14
Shell Basics
Shell variables– Similar to environment variables in MS-DOS– Syntax: varname=value– Preceded by $ when printing value (e.g. echo $x, where x is a variable)
– Special variable PS1Variable that contains string for command line
promptMay be replaced
15
Shell Basics
Input/Output commands– read - get input from keyboard and assign it to a
variable– echo - send stream of characters to screen– pr - send output to printer
16
Shell Basics
Command substitution– The output is taken instead of the command itself– Command is enclosed in `` (e.g. x=`date`)– Common uses:
Variable assignment Command line arguments
17
Shell Basics
Wildcards? Matches any single character except a leading ‘.’
(dot)* Matches zero or more characters except a
leading dot[] Defines a class of characters
- Defines an inclusive ranges (e.g. [0-9])! Negates the defined class
18
Shell Basics
Quoting charactersDeveloped because of certain characters have special
meaning (e.g. $, *, <, >, #, etc.)\ Removes meaning of the next character’ Removes meaning of all characters” Removes meaning of all characters except \, $,
and ”
19
Shell Basics
Exercise for quoting characters
What’s the difference between...
echo ’$abc’echo ”$abc”echo \$abc
20
Shell Basics
RedirectionEvery time a shell is started, three files (devices) are
automatically opened:– stdin (standard input) - device from w/c the program
reads the input (keyboard), file descriptor 0– stdout (standard output) - device to w/c the
program writes the output (monitor), file descriptor 1– stderr (standard error) - device to w/c the program
writes the errors (monitor), file descriptor 2
21
Shell Basics
Redirection (cont’d)– Redirection - output is written to or read from
another file instead of the standard files/devices– Syntax: command symbol filename– Input redirection (<) - any command that reads its
input from stdin can have it read from a file
22
Shell Basics
Redirection (cont’d)– Output redirection (> or >>) - any command that
writes its output to stdout can be redirected to create, overwrite, or append to another file
– Error redirection (2> or 2>>) - error messages are redirected to create, overwrite, or append to another file
23
Shell Basics
Redirection (cont’d)Special redirection commands:2>&1 - redirects standard errors on same stream as
stdout1>&2 - redirects outputs to stdout on same stream as
stderr
24
Shell Basics
Piping– Sends output of one command as input of another
command– Syntax: command1 | command2– Eliminates temporary files using redirection
commands, for example$ date > tmpfile$ wc tmpfile$ rm tmpfileCan be written instead as date | wc
25
Shell Basics
Filters– Program that reads input, performs translation,
produces output– Commonly used filters:
grep (looks for occurrences of words/phrases in files)sort (sorts input and writes to the output)sed (reads lines from input file, applies edit commands,
and outputs to stdout)
26
Shell Basics
Filters (cont’d)– Commonly used filters
awk– designed by Al Aho, Peter Weinberger, and Brian Kernighan– much more flexible and addresses limitations of sed– allows programming constructs to be used (looping,
variables, arithmetic, etc.)– processing language based on C
27
Shell Programming
Command line arguments– Shellscript command parameters are represented
by $number– Number indicates position of command argument– Example:
myprog word1 bin $1 = word1 $2 = bin $0 = myprog
28
Shell Programming
Command line arguments– Other important shell variables related to command
parameters$# the number of arguments$* all arguments
29
Shell Programming
Decision statements– IF command
if commandthen
commands if condition is true
elsecommands if condition is false
fi
30
Shell Programming
Decision statements– CASE command
case word inpattern) commands;;pattern) commands;;...esac
31
Shell Programming
Looping constructs– FOR loop
for var in list of wordsdo
loop body , $var set to successive elements of the list
done
32
Shell Programming
Looping constructs (cont’d)– WHILE loop
while commanddo
loop body executed as long as command is true
done
33
Shell Programming
Looping constructs (cont’d)– UNTIL loop
until commanddo
loop body executed as long as command is truedone
– The symbol ‘:’ is a shell built-in that does nothing but return true
– The command true can also be used– ‘:’ is more efficient since it is not a command