Top Banner
1 Operating Systems Part II: Introduction to the Unix Operating System (Utilities and Shell Programming)
33

Operating system (remuel)

May 25, 2015

Download

Education

Remuel Malinao

introduction to operating system
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Operating system (remuel)

1

Operating Systems

Part II: Introduction to the Unix Operating System(Utilities and Shell Programming)

Page 2: Operating system (remuel)

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)

Page 3: Operating system (remuel)

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

Page 4: Operating system (remuel)

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

Page 5: Operating system (remuel)

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

Page 6: Operating system (remuel)

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

Page 7: Operating system (remuel)

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

Page 8: Operating system (remuel)

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 ‘-’)

Page 9: Operating system (remuel)

9

Unix Commands

Example– Basic directory command: ls (lists all contents of a

directory)– Extended command: ls -l (lists contents plus

other file information)

Page 10: Operating system (remuel)

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)

Page 11: Operating system (remuel)

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

Page 12: Operating system (remuel)

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)

Page 13: Operating system (remuel)

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)

Page 14: Operating system (remuel)

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

Page 15: Operating system (remuel)

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

Page 16: Operating system (remuel)

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

Page 17: Operating system (remuel)

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

Page 18: Operating system (remuel)

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 ”

Page 19: Operating system (remuel)

19

Shell Basics

Exercise for quoting characters

What’s the difference between...

echo ’$abc’echo ”$abc”echo \$abc

Page 20: Operating system (remuel)

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

Page 21: Operating system (remuel)

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

Page 22: Operating system (remuel)

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

Page 23: Operating system (remuel)

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

Page 24: Operating system (remuel)

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

Page 25: Operating system (remuel)

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)

Page 26: Operating system (remuel)

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

Page 27: Operating system (remuel)

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

Page 28: Operating system (remuel)

28

Shell Programming

Command line arguments– Other important shell variables related to command

parameters$# the number of arguments$* all arguments

Page 29: Operating system (remuel)

29

Shell Programming

Decision statements– IF command

if commandthen

commands if condition is true

elsecommands if condition is false

fi

Page 30: Operating system (remuel)

30

Shell Programming

Decision statements– CASE command

case word inpattern) commands;;pattern) commands;;...esac

Page 31: Operating system (remuel)

31

Shell Programming

Looping constructs– FOR loop

for var in list of wordsdo

loop body , $var set to successive elements of the list

done

Page 32: Operating system (remuel)

32

Shell Programming

Looping constructs (cont’d)– WHILE loop

while commanddo

loop body executed as long as command is true

done

Page 33: Operating system (remuel)

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