Operating Systems Part II: Introduction to the Unix Operating System (Utilities and Shell Programming)
1
Unix Design Principles
2
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)
Unix Design Principles
3
Unix later used for networking, graphics, realtime 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
Programmer Interface (The users) shells and commands compilers and interpreters system libraries
system interface to the kernel swapping disk and tape drivers virtual memory CPU scheduling
kernel
demand paging file system page replacement signals
kernel interface to the hardware
hardware
4
The Unix Shell
Command interpreter – – –
5
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
The Unix Shell
6
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
The Unix Shell
7
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
Unix Commands
8
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 ‘-’)
Unix Commands
Example – –
9
Basic directory command: ls (lists all contents of a directory) Extended command: ls -l (lists contents plus other file information)
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
–
10
Files may be shared (concurrent access)
The Unix File System
Features of Unix File System (continued) –
–
11
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
The Unix File System
Most often used file system commands: – – – – – – –
12
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)
The Unix File System
Most often used file system commands: – – – – – – –
13
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)
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 PS1 Variable
that contains string for command line
prompt May be replaced
14
Shell Basics
Input/Output commands –
– –
15
read - get input from keyboard and assign it to a variable echo - send stream of characters to screen pr - send output to printer
Shell Basics
Command substitution –
The output is taken instead of the command itself Command is enclosed in `` (e.g. x=`date`)
–
Common uses:
–
16
Variable assignment Command line arguments
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 ?
!
17
Defines an inclusive ranges (e.g. [0-9]) Negates the defined class
Shell Basics
Quoting characters Developed 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 ”
18
Shell Basics
Exercise for quoting characters What’s the difference between... echo ’$abc’ echo ”$abc” echo \$abc
19
Shell Basics
Redirection Every 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
20
–
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
Shell Basics
Redirection (cont’d) – – –
21
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
Shell Basics
Redirection (cont’d) –
–
22
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
Shell Basics
Redirection (cont’d) Special redirection commands: 2>&1 - redirects standard errors on same stream as stdout 1>&2 - redirects outputs to stdout on same stream as stderr
23
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 tmpfile Can be written instead as date | wc
24
Shell Basics
Filters – –
Program that reads input, performs translation, produces output Commonly used filters:
25
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)
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
26
Shell Programming
Command line arguments –
Shellscript command parameters are represented by $number
–
Number indicates position of command argument Example:
–
myprog word1 bin
27
$1 = word1 $2 = bin $0 = myprog
Shell Programming
Command line arguments –
Other important shell variables related to command parameters $# the number of arguments $* all arguments
28
Shell Programming
Decision statements –
IF command if command then commands if condition is true
else commands if condition is false
fi
29
Shell Programming
Decision statements –
CASE command case word in pattern) commands;; pattern) commands;; ... esac
30
Shell Programming
Looping constructs –
FOR loop for var in list of words do loop body , $var set to successive elements of the list
done
31
Shell Programming
Looping constructs (cont’d) –
WHILE loop while command do loop body executed as long as command is true
done
32
Shell Programming
Looping constructs (cont’d) –
UNTIL loop until command do loop body executed as long as command is true
done – – –
33
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