Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.

Similar presentations


Presentation on theme: "Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II."— Presentation transcript:

1 Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II

2 Unix Environment

3 Shell functions

4 Shell commands and utilities Shell commands are commands built into the shell (part of the kernel) and are an integral part of the shell. eval, login, exec, tee, and exit are shell commands. Utilities are external small programs (stored on disk) such as editors, compilers, sorting routines, etc. chsh, kill, ps, echo, nohup, and sleep are examples of utilities.

5 The three major shells Bourne: $ is usually the prompt. Korn: a superset of Bourne; $ is usually the prompt. C: % is usually the prompt. echo $SHELL displays the full pathname of the current shell, where echo is the utility and SHELL is a shell variable.

6 Metacharacters: characters that have special meaning to the shell SymbolMeaning >Output redirection; writes standard output to a file. >>Output redirection; appends standard output to a file. <Input redirection; reads standard input from a file. *File substitution wildcard; matches zero or more characters. ?File substitution wildcard; matches a single character. […]File substitution wildcard; matches any characters in brackets. `command`Command substitution; replaced by the output from command. |Pipe symbol; sends the output of one process to the input of another.

7 Metacharacters continued symbolMeaning ;Used to sequence commands. \Prevents special interpretation of the next character. ||Conditional execution; executes a command if the previous one fails. &&Conditional execution; executes a command if the previous one succeeds. (…)Group commands. &Run a command in the background. #All characters that follow up to a new line are ignored by the shell and programs, e.g., comments. $Expands the value of a variable. << tokInput redirection; reads standard input from script up to tok.

8 Metacharacter usage examples $ command > filename # output of command stores in filename $ command >> filename # output of command appended in filename $ command < filename# input from filename ---------------------------------------------------------------------------------------------------------------- $ ls *.cpp# list all filenames with.cpp extension $ ls ?.cpp# list all one character filenames with.cpp extension $ ls [ac]*# list all filenames that begins with a or c $ ls [A-Za-z]# list all filenames that begins with a letter $ command1 | command 2 # output of command1 becomes the input of command2 $ echo the data today is `date`# date enclosed in grave accents ` will be executed $ echo there are `who | wc -l` users on the system $ date; pwd; ls# executes three commands in sequence $ g++ myprog.cpp && a.out# a.out is executed if myprog compiles fine $ g++ myprog.cpp || echo compilation fails# prints msg if compilation fails $ (date; ls; pwd) > filename# group then redirect

9 The output redirection with the > and >> metacharacters The shell redirection facility allows –store the output of a process to a file. –use the contents of a file as input to a process. --------------------------------------------------------------------------------------------------- $ command > filename $ cat > example.txt first line second line ^D# press Ctrl+D to terminate keyboard input $ cat example.txt# create example.txt file first line second line $ cat >> example.txt# append third line fourth line ^D# press Ctrl+D to terminate keyboard input $ cat example.txt first line second line third line fourth line $

10 The input redirection with the < and << metacharacters $ a.out < input.dat# input data taken from input.dat file $ mail ewh < letter.txt# send mail to ewh (user name on same network) # content of mail taken from letter.txt file $ command << word# it copies its standard input (keyboard) up to but # not including the line starting with word into a # buffer and then executing command using the # contents of the buffer as its standard input.

11 Filename substitution with wildcards: *, ?, and […] $ ls –FR# recursively list my current directory a.cb.ccc.cdir1/dir2/ dir1: d.ce.e dir2: f.dg.c $ ls *.c a.cb.ccc.c $ ls ?.c a.cb.c $ ls [ac]* a.ccc.c $ ls [A-Za-z]* a.cb.ccc.c $ ls dir*/*.c# list all files with *.c in directories beginning with dir* dir1/d.cdir2/g.c

12 Pipes $ command1 | command2 causes the standard output of command1 to flow through to the standard input of command2. any number of commands may be chained together with pipes. Examples $ ls a.cb.ccc.cdir1dir2 $ ls | wc –w# wc performs word count 5

13 Tees The tee utility copies its standard input to the specified files and to its standard output. the –a option causes the input to be appended to the files rather than overwriting them. Example: $ who | tee who.capture | sort ables …# rest of the line represented by … glass … posey... $ cat who.capture glass … posey … ables …

14 Command substitution with the grave accent ` A command surrounded by grave accents ` is executed, and its standard output is inserted in the command’s place in the entire command line. Examples: $ echo Today’s date is `date` Today’s date is Tue Nov 5 21:52:45 EST 2002 $ cat who.capture glass … # rest of the line represented by … posey … ables … $ echo There are `who | wc – l` users on the system now. There are 3 users on the system now.

15 Sequences and conditional sequences $ date; pwd; ls executes a sequence of three commands in a left-to-right order $ date date.text; ls; pwd > pwd.text $ g++ test.cpp && a.out a.out will be executed only if compiles successfully $ g++ test.cpp || echo compilation failed. “compilation failed.” is displayed if compilation is unsuccessful.

16 Grouping commands $ date; ls; pwd > out.txt $ cat out.txt# what is displayed? $ (date; ls; pwd) > out.txt $ cat out.txt# what is displayed now?

17 Background processing


Download ppt "Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II."

Similar presentations


Ads by Google