1 / 39

Lecture 8: SHELL

Lecture 8: SHELL. SHELL.

Download Presentation

Lecture 8: SHELL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8:SHELL

  2. SHELL • The shell acts as an interface between the user and the kernel. When the user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt.

  3. Shell (cont.) • The user can customise his/her own shell and users can use different shells on the same machine. Staff and students in the university have the tcsh shell by default • By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again. • The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.

  4. Command line structure • The simplest command is a single word, usually naming a file for execution: $ who you tty2 Sep 28 07:51 jpl tty4 Sep 28 08:32 • A command usually ends with a newline, but a semicolon “;” is also a command terminator $ date ; who Wed Sep 28 09:07:23 EDT 1983 you tty2 Sep 28 07:51 jpl tty4 Sep 28 08:32

  5. Command line structure (cont.) $ date ; who Wed Sep 28 09:07:23 EDT 1983 you tty2 Sep 28 07:51 jpl tty4 Sep 28 08:32 $ date ; who | wc 3 16 89 $ (date ; who) | tee save | wc • 16 89 $ cat save Wed Sep 28 09:07:23 EDT 1983 you tty2 Sep 28 07:51 jpl tty4 Sep 28 08:32 $ wc < save 3 16 89

  6. Command line structure (cont.) • Another command terminator is the ampersand &. It is exactly like the semicolon or newline, except that it tells the shell not to wait for the command to complete. Typically is used to run a long-running command “in the background” while you continue to type interactive commands: $ long-running-command & 5273 $ • Given the ability to group commands, there are some more interesting uses of background processes. The command sleep waits the specified number of seconds before exiting $ sleep 5 $ $ (sleep 5; date) & date 5278 Wed Sep 28 09:07:23 EDT 1983 //Output from second date $ Wed Sep 28 09:07:23 EDT 1983 //Prompt appears, then date 5 sec later $ (sleep 300; echo Tea is ready) & Tea will be ready in 5 minutes 5291 A HANDY REMINDER MECHANISM!

  7. Command line structure (cont.) • The & terminator applies to commands, and since pipelines are commands you do not need parentheses to run pipelines in the background: $ pr file | lpr & • Arranges to print the file on the line printer without making you wait for the command to finish. Parenthesising the pipeline has the same effect, but requires more typing: $ (pr file | lpr) & • Most programs accept arguments on the command line, such as “file” (an argument to “pr”) in the above example. Arguments are words separared by blanks and tabs, that typically name files to be processed by the command, but they are strings that may be interpreted any way the program sees it. The various special characters interpreted by the shell, such as <, >, |, ; and & are not arguments to the programs the shell runs. They instead control how the shell runs them: $ echo Hello > junk Tells the shell to run echo with the single argument Hello and place the output in the file junk. The string > junk is not an argument to echo; it is interpreted by the shell and never seen by echo. In fact, it need not be the last string in the command: $ > junk echo Hello Is identical but less obvious!

  8. Try this at the lab: • What are the differences among the following three commands? $ cat file | pr $ pr <file $ pr file (Over the years the redirection operator < has lost some ground to pipes; people seem to find “cat file |” more natural than “<file”).

  9. Metacharacters • The shell recognises a number of other characters as special; the most commonly used is the asterisk “*” which tells the shell to search the directory for filenames in which any string of characters occurs in the position of the “*”: $ echo * //is a poor facsimile of ls. (ls looks at file names beginning with dot as well) $ ls .profile junk temp $ echo * junk temp • Characters like * that have special properties are known as METACHARACTERS $ echo “***” *** $ echo “Do not do that” Do not do that & echo ‘hello > world’ hello world

  10. Metacharacters (cont.) • Quotes do not have to surround the whole argument $ echo x’*’y x*y $ echo ‘*’A’?’ *A? $ ls x*y x*y not found Message from ls, the file does not exist $ > xyzzy Create xyzzy $ ls x*y xyzzy File matches xyzzy $ ls ‘x*y’ x*y not found $ echo abc\ > def\ > ghi abcdefghi

  11. Metacharacters

  12. Creating new commands • Given a sequence of commands that is to be repeated more than a few times, it would be convenient to make it into a “new” command with its own name, so you can use it like a regular command $ who | wc -1 • The first step is to create an ordianry file that contains “who | wc –l” $ echo ‘who | wc –l’ > nu $ cat nu who | wc –l $ sh nu //this child shell is called sub shell $ chmod +x nu $ nu • Users of nu from now on cannot tell, just by running it, that you implemented it in this easy way!

  13. Creating new commands (cont.) • As it stands, nu works only if it is in your current directory. To make nu part of your repertoire regardless of what directory you are in, move it to your private bin directory, and add /osr/you/bin to your search path: $ pwd /usr/you $ mkdir bin $ echo $PATH :/usr/you/bin:/bin:/usr/bin $ mv nu bin $ ls nu nu not found $ nu 4

  14. Creating new commands (cont.) There are other simple commands that you might create this way to tailor your environment to your own taste: • cs, which echoes the proper sequence of mysterious characters to clear the screen on your terminal (24 newlines is a fairly general implementation) • what, which runs who and ps-a to tell who is logged on and what they are doing • where, which prints the identifying name of the UNIX system you’re using – it’s handy if you use sevral regularly

  15. Command arguments and parameters • Suppose we want to make a program called cx to change the mode of a file to executable: $ cx nu • Is a shorthand for chmod +x nu. Let’s look at the whole sequence of operations: $ echo ‘chmode +x $1’ > cx $ sh cx cx $ echo echo Hi, there! > hello $ hello hello: cannot execute $ cx hello $ hello Hi there! $ mv cx/usr/you/bin $ rm hello

  16. Command arguments and parameters • With $* added to your repertoire, you can make some convenient shell files, such as lc or m: $ cd /usr/you/bin $ cat lc # lc: count number of lines in files wc –l $* $ cat m # m: a concise way to type mail mail $*

  17. Command arguments and parameters • Let’s make a directory assistance program, which we’ll call 411 in honor of the telephone directory assistance number where we live: $ echo ‘grep $* /usr/you/lib/phone-book’ >411 $ cx 411 $ 411 joke Dial-a-joke 212-976-3838 Dial-a-prayer 212-246-4200 Dial santa 212-976-3636 $ 411 ‘dow jones’ Grep: can’t open jones

  18. Program output as arguments • The output of any program can be placed in a command line by enclosing the invocation in back quotes ‘…’: $ echo At the tone the time will be ‘date’ At the tone the time will be Thu Sep 29 00:02:15 EDT 1983 $ mail ‘cat mailinglist’ < letter $ cat mailinglist echo don whr ejs mb $ cx mailinglist $ mailinglist don whr ejs mb

  19. Shell variables • The shell has variables, which are called parameters $ pwd /usr/you/bin $ dir = ‘pwd’ $ cd /usr/mary/birf $ ln $dir/cx $ … $ cd $dir $ pwd /usr/you/bin

  20. Shell variables (cont.) • The value of a variable is associated with the shell that creates it, and is not automatically passed to the shell’s children $ x = Hello $ sh $ echo $x $ ctl-d $ $ echo $x Hello

  21. Shell variables (cont.) • The ‘.’ mechanism should be used to change the value of a variable permanently, while in-line assignements should be used for temporary changes. Consider the searching /usr/games for commands, with the directory not in your path: $ ls /usr/games grep fort fortune $ fortune fortune: not found $ echo $PATH :/usr/you/bin:/bin:/usr/bin $ PATH=/usr/games fortune Ring the bell; close the book; quench the candle. $ echo $PATH :/usr/you/bin:/bin:/usr/bin $ cat /usr/you/bin/games PATH=$PATH:/usr/games $ .games $ fortune Premature optimisation is the root of all evol – Knutch $ echo $PATH :/usr/you/bin:/bin:/usr/bin:/usr/games

  22. More on I/O redirection • The standard error was invented so that error messages would always appear on the terminal: $ diff file1 file2 > diff.out diff: file2: No such file or directory • It is also possible to merge the two output streams: $ time wc ch3.1 > wc.out 2>&1 $ cat wc.out • 4288 22691 ch3.1 Real 1.0 User 0.4 Sys 0.3

  23. Shell I/O Redirections

  24. Looping in shell programs • Looping over a set of filenames is very common, and the shell’s for statement is the only shell control-flow statement that you might commonly type at the terminal. The syntax is: for var in list-of-words do commands done • For example $ ls ch2.* | 5 ch2.1 ch2.2 ch2.3 ch2.4 ch2.5 ch2.6 ch2.7 $ for I in ch2.* > do > echo $I; > diff –b old/$I $I > echo > done | pr –h “diff ‘pwd’/old ‘pwd’ “ | lpr & > 3712

  25. Looping in shell programs $ for i in $* > do > chmod +x $1 > done Is inferior to: chmod +x $*

  26. Lecture 9:Shell Programming • Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command. • This lecture explains the basics of shell programming, but it is not a manual for the shell. • That is in the manual page sh of the Unix Programmer’s Manual • A major theme in shell programming is making programs robust so they can handle improper input and give helpful information when things go wrong.

  27. Customising the cal command • One common use of a shell program is to enhance or to modify the user interface to a program. Consider the cal command: $ cal Usage: cal [month] year $ cal october 1983 Bad argument $ cal 10 1983 October 1983 …

  28. Case statement • The shell provides a case statement that is well suited for making decisions: case word in pattern) commands ;; pattern) commands ;; … esac • The case statement compares word to the patterns fromtop to bottom, and performs the commands associated with the first, and only the first, pattern that matches. The patterns are written using the shell’s pattern matching rules, slightly generalised from what is available for filename matching. Each action is terminated by the double semicolon ;;. (The ;; may be left off the last case but we often leave it in for easy editing)

  29. Case statement $ cat cal # cal: nicer interface to /usr/bin/cal case $# in 0) set ‘date’; m=$2; y=$6 ;; #no args: use today 1) m=$1; set ‘date’; y=$6 ;; #1 arg: use this year *) m=$1; y=$2 ;; #2 args: month and year esac case $m in jan*|Jan*) m=1 ;; feb*|Feb*) m=2 ;; mar*|Mar*) m=3 ;; apr*|Apr*) m=4 ;; may*|May*) m=5 ;; jun*|Jun*) m=6 ;; jul*|Jul*) m=7 ;; aug*|Aug*) m=8 ;; sep*|Sep*) m=9 ;; oct*|Oct*) m=10 ;; nov*|Nov*) m=11 ;; dec*|Dec*) m=12 ;; [1-9]|10|11|12) ;; #numeric year *) y=$m; m=“” ;; #plain year esac /usr/bin/cal $m $y #run the real one $

  30. Shell build in variables

  31. The first case statement has a couple of tricky lines.. $ date Sat Oct 1 06:05:18 EDT 1983 $ set ‘date’ $ echo $1 Sat $ echo $4 06:05:20 $

  32. Shell pattern Matching Rules

  33. Which command is which? $ cat which # which cmd: which cmd in PATH is executed version 1 case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in ‘echo $PATH | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’ do if test –f $i/$1 #use test –x if you can then echo $i/$1 exit 0 #found it fi done exit 1 #not found $

  34. Which command is which? $ cat which # which cmd: which cmd in PATH is executed version 1 opath=$PATH PATH=/bin:/usr/bin case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in ‘echo $PATH | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’ do if test –f $i/$1 #use test –x if you can then echo $i/$1 exit 0 #found it fi done exit 1 #not found $

  35. While and until loops for i in list of words do loop body, $1 set to successive elements of list done while command do loop body executed as long as command returns true done until command do loop body executed as long as command returns value done

  36. While and until loops while sleep 60 until who| grep mary do who| grep mary do sleep 60 done done $ cat watchfor # watchfor: watch for someone to log in PATH=/bin:/usr/bin case $# in 0) echo ‘Usage: watchfor person’ 1>&2; exit 1 esac until who|egrep “$1” do sleep 60 done $cx watchfor $watchfor you you tty0 Oct 1 08:01 It works $

  37. Evaluation of Shell Variables

  38. Traps: catching interrupts • The shell build in command trap sets up a sequence of commands to be executed when a signal occurs • Trap sequence-of-commands list of signal numbers • Kill –9 process-id

  39. THURSDAY 06.02.2003ARTS DAYLECTURE CANCELLED

More Related