180 likes | 287 Views
Advanced Operating Systems. Lecture 3: Shell. University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani. This Lecture: Shell. An interface to the Kernel Resources The UNIX Time Sharing System by D. M. Ritchie and K. Thompson.
E N D
Advanced Operating Systems Lecture 3: Shell University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Distributed Operating Systems
This Lecture: Shell • An interface to the Kernel • Resources • The UNIX Time Sharing System by D. M. Ritchie and K. Thompson. • An Introduction to the Unix Shell, By Steve Bourne's • Rc The Plan 9 Shell, ByTom Duff • Es: A shell with higher-order functions, by Paul Haahr & Byron Rakitzis Distributed Operating Systems
Outline • Introduction • Why shell? • Bourne Shell • RC Shell • ES shell • Cons and pros Distributed Operating Systems
Shell • shell is a command programming language that provides an interface to the system. • It includes control-flow primitives, parameter passing, variables and string substitution. Distributed Operating Systems
Why Shell? • More abstract - higher level • Concise syntax • Simpler • Easier for file manipulation, process manipulation Distributed Operating Systems
Shell vs. Scripting languages • scripting languages fix data structures, functionality. • lost default behaviors that are convenient - manipulation of files, programs, pipes, etc. Distributed Operating Systems
Shell vs. Scripting languages • Shell scripting skills have many applications, including: • Ability to automate tasks, such as • Backups • Administration tasks • Periodic operations on a database via cron • Any repetetive operations on files • Increase your general knowledge of UNIX • Use of environment • Use of UNIX utilities • Use of features such as pipes and I/O redirection Distributed Operating Systems
Bourne Shell • Simplest shell: just run cmd arg arg... - fork, exec in child, wait in parent. • More functionality: file redirection cmd >file - open file as fd 1 in child before exec • Still more functionality: pipes cmd | cmd - create a pipe, run first cmd with pipe on fd 1, run second cmd with other end of pipe on fd 0. • More Bourne arcana: $*, $@, environment variables, macro substitution, if, while, for, ||, &&, "foo $x", 'foo $x', `foo`. Distributed Operating Systems
RC Shell • Rc is a command interpreter for Plan 9 that provides similar facilities to UNIXs Bourne shell. • Principal difference between RC and Bourne (others?): Bourne shell offers string-valued variables. Rc provides variables whose values are lists of arguments that is, arrays of strings. • Examples • path=(. /bin) • echo $path(2) which gives /bin • echo $path(2 1 2) => /bin . /bin • The number of strings in a variable can be determined by the $# operator. Distributed Operating Systems
RC Shell • build an argument list from the output of a command. • cat `{ls -tr|sed 10q} will concatenate the ten oldest files in the current directory. • Command grouping • {sleep 3600;echo 'Time''s up!'}& • sleep 3600;echo 'Time''s up!'& • Conditional execution if • for(i in *.c) if(cpp $i >/tmp/$i) vc /tmp/$i • while(newer subr.v subr.c) sleep 5, This waits until subr.v is newer than subr.c, presumably because the C compiler finished with it. Distributed Operating Systems
RC Shell • Built-in commands • [-i] file Read input from file file. • builtin command , ex. fn cd {builtin cd $* && pwd } • Advanced I/O Redirection • vc junk.c >[2]junk.diag, saves the compilers diagnostics from standard error in junk.diag. • vc junk.c >[2=1] >junk.out. redirects file descriptor 2 to a copy of file descriptor 1 (presumably the terminal). • Here documents for(i) grep $i <<! ... tor 2T-402 2912 kevin 2C-514 2842 bill 2C-562 7214 ... ! • The most important principle in rcs design is that its not a macro processor. Distributed Operating Systems
Es- Shell • applied concepts from modern functional programming languages, such as Scheme and ML, to shells. • Everything is in a uniform representation, function call. • Ability to replace primitive functions such as pipe, to run remotely. • Syntax and most functionality are the same. Distributed Operating Systems
Es- Shell • Functions: commands taking arguments and applying command to each of them. fn apply cmd args { for (i = $args) $cmd $i } For example es> apply echo testing 1.. 2.. 3.. testing 1.. 2.. 3.. • Take program fragments (enclosed in braces) as arguments. apply @ i {cd $i; rm -f *} \ inlining in the code /tmp /usr/tmp @ parameters { commands }: waiting to happen es> @ i {cd $i; rm -f *} /tmp : runs the inlined function directly on the argument /tmp. Distributed Operating Systems
Es- Shell • Settor Variables: gets evaluated every time the variable changes value like watch function. • Return Values: can return any object: string, program fragment, list, etc. • Exception: throw raises an exception es> fn in dir cmd { if {~ $#dir 0} { throw error ’usage: in dir cmd’ } fork # run in a subshell cd $dir $cmd } es> in usage: in dir cmd Distributed Operating Systems
Es- Shell • Spoofing: Overriden the create or pipe functions by user. • Implemented on 8000 lines of C code. • Not interact well with UNIX. • Garbage Collection: True recursive structures, that is, objects which include pointers to themselves, either directly or indirectly. It needs a full garbage collection system. Distributed Operating Systems
Good things about Shell • batch programming • portability • interactive development environment • connecting programs together Distributed Operating Systems
Bad things about Shell • string and character manipulation (sometimes) • speed • large programs • data structures • some functionality is hidden (e.g., networks, but can use nc instead; guis harder). Distributed Operating Systems
Next Lecture • OS Kernel • References Distributed Operating Systems