Presentation is loading. Please wait.

Presentation is loading. Please wait.

Univ. of TehranDistributed Operating Systems1 Advanced Operating Systems University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani.

Similar presentations


Presentation on theme: "Univ. of TehranDistributed Operating Systems1 Advanced Operating Systems University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani."— Presentation transcript:

1 Univ. of TehranDistributed Operating Systems1 Advanced Operating Systems University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Shell Lecture 3: Shell

2 Univ. of TehranDistributed Operating Systems2 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, By Tom Duff Es: A shell with higher-order functions, by Paul Haahr & Byron Rakitzis

3 Univ. of TehranDistributed Operating Systems3 Outline Introduction Why shell? Bourne Shell RC Shell ES shell Cons and pros

4 Univ. of TehranDistributed Operating Systems4 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.

5 Univ. of TehranDistributed Operating Systems5 Why Shell? More abstract - higher level Concise syntax Simpler Easier for file manipulation, process manipulation

6 Univ. of TehranDistributed Operating Systems6 Shell vs. Scripting languages scripting languages fix data structures, functionality. lost default behaviors that are convenient - manipulation of files, programs, pipes, etc.

7 Univ. of TehranDistributed Operating Systems7 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

8 Univ. of TehranDistributed Operating Systems8 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`.

9 Univ. of TehranDistributed Operating Systems9 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.

10 Univ. of TehranDistributed Operating Systems10 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.

11 Univ. of TehranDistributed Operating Systems11 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.

12 Univ. of TehranDistributed Operating Systems12 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. Es- Shell

13 Univ. of TehranDistributed Operating Systems13 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. Es- Shell

14 Univ. of TehranDistributed Operating Systems14 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 Es- Shell

15 Univ. of TehranDistributed Operating Systems15 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. Es- Shell

16 Univ. of TehranDistributed Operating Systems16 batch programming portability interactive development environment connecting programs together Good things about Shell

17 Univ. of TehranDistributed Operating Systems17 string and character manipulation (sometimes) speed large programs data structures some functionality is hidden (e.g., networks, but can use nc instead; guis harder). Bad things about Shell

18 Univ. of TehranDistributed Operating Systems18 OS Kernel References Next Lecture


Download ppt "Univ. of TehranDistributed Operating Systems1 Advanced Operating Systems University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani."

Similar presentations


Ads by Google