Ch 9 Pipes, Filters, and Redirection

Total Page:16

File Type:pdf, Size:1020Kb

Ch 9 Pipes, Filters, and Redirection

Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection Chapter 9 Pipes, Filters, and Redirection

LECTURE NOTES

CHAPTER OUTLINE TEACHING SUGGESTIONS

Chapter Overview  Slides 2-6  Will use redirection to redirect standard input and standard output.  Chapter Overview duplicated in PowerPoint  Learn how pipes can be used to connect slides. programs.  After completion of lecture, suggest to  How filters can be used to manipulate data students that they review Objectives and will be explained. Outcomes found on first page of chapter as  What shell extensions are and how they may a check to see if they have mastered be used will be discussed. concepts.  Learn how to combine commands using pipes, filters, and redirection.

REDIRECTION OF STANDARD I/O (INPUT/OUTPUT) SECTION 9.1 (pp. 440-441)  Redirection of Standard I/O (Input/Output)  Slides 7-13  What is keyed in is input.  What is written to screen is output.  Discussion Question (1) - Explain redirection.  Input/output called I/O.  OS recognizes three standards:  Discussion Question (2) - Explain the terms  Standard input: Keyboard. standard input, standard output, and standard  Standard output: Screen. error.  Standard error: place from which OS Discussion Question (3) - Does every operating writes error messages to the screen.  system command use standard input and  Not all commands deal with standard standard output? If not, why not? input and standard output.  See PowerPoint slide #10 – Results of  Discussion Question (4) - What is the Copy Command difference between > and >> when redirecting  I/O redirection allows standard I/O to be output? changed. Discussion Question (5) - Explain how the  Read from file, not keyboard.  symbol < is used.  Write to printer, not screen.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 1 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

 Three redirection operators.  The > redirects output of command to device or file.  The < asks for input from source other than the keyboard.  The >> redirects standard output and appends but does not overwrite a file. ACTIVITY—USING > TO REDIRECT STANDARD OUTPUT SECTION 9.2 (pp. 441-443)  Using > to Redirect Standard Output  Slide 14  DATA disk in Drive A, A: \> displayed.  Activity steps.  Redirection is either/or process - See on  Key in: screen, or prints to printer.  DIR C:\WUGXP\*.TXT  Redirection can be used with commands  DIR C:\WUGXP\*.TXT > that writes its results to standard output TXTFILES.TXT device (screen).  TYPE TXTFILES.TXT  Cannot use COPY DIR *.TXT - COPY  Activity completed. copies files, not commands.  Discussion Question (6) – Keying in COPY DIR filename will not give you a file containing the directory display. Why?

ACTIVITY—USING < TO REDIRECT STANDARD INPUT SECTION 9.3 (pp. 443-445)  Using < to Redirect Standard Input  Slide 15  DATA disk in Drive A, A: \> displayed.  Activity steps.  DEL *.* needs keyboard response.  Key in:  Useful to lock up the system - i.e., write a  MD TEST file with X , then write command as  COPY C:\WUGXP\*.NEW TEST DEL \TEST\*.* < X.FIL. System will lock  DEL TEST\*.* up.  N then DIR TEST  Be careful with redirection of input.  TYPE Y.FIL - Tell OS to take input from a file; any  DEL TEST\*.* < Y.FIL keyboard input will be ignored.  DIR TEST .  Activity completed.

ACTIVITY—USING >> TO ADD

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 2 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

REDIRECTED OUTPUT TO A FILE  Using >> to Add Redirected Output to a File SECTION 9.4 (pp. 445-447)  DATA disk in Drive A, A: \> displayed.  Slide 16  Activity steps.  Key in:  Can only use >>. There is no <<.  COPY C:\WUGXP\*.TXT  >> between files – adds contents of first file  TYPE JUPITER.TXT to end of second file.  TYPE MERCURY.TXT  TYPE MERCURY.TXT >>  To append to end of existing file, use JUPITER.TXT double redirection symbol (>>)  TYPE JUPITER.TXT  Activity completed. FILTERS  Filters  Manipulate information. SECTION 9.5 (p. 447)  Read information from keyboard.  Change input in specified way.  Slides 17-20  Write results to the screen. Compare filters to filters in water  Three OS filters - external commands.  purification system.  SORT - arranges lines in ascending or - Extract unwanted elements. descending order. - Send pure water on its way.  FIND - searches for particular group of characters, called a character string.  Discussion Question (7) - What are filters?  MORE - temporarily halts screen display after each screenful.  OS creates temporary files while it "filters" data.  Important that there be access to the disk and the filters.  If a disk is write-protected, the OS will not be able to execute filter commands.

THE SORT COMMAND  The SORT Command  Sorts/arranges lines of text. SECTION 9.6 (p. 447)  Sends output to screen unless redirected.  Default sorts in ascending order.  Slides 21-22  SORT syntax: See PowerPoint slide #22.  /R - reverses sort order- sorts Z to A,  Sorts A to Z or from lowest to highest 9 to 0. numbers (starting in first column).

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 3 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

 /+n - sorts file according to characters in column n. Discussion Question (8) – What do the three  M kilobytes – amount of main  memory to use for sort SORT parameters covered in this chapter —/n, /O, and /R—represent?  /T – (Temporary) – path of directory to hold sort’s working storage in case does not fit into main memory.  /O – Output – if not specified data written to standard output. Specifying output file faster than redirecting standard output to same file.  [drive1:] [path1] filename1 - specifies file (s) to be sorted.  [drive2:] [path2] filename2 - specifies a file where the sorted input is to be stored. ACTIVITY—USING SORT  Using SORT  DATA disk in Drive A, A: \> displayed.  means to press the key. SECTION 9.7 (pp. 448-450)  Activity steps.  Slides 23-27  Key in the following commands. Each command followed by same as pressing + Z.  SORT then MECURY then VENUS  Numbers can be character data – then EARTH then 3 then MARS - Phone numbers or zip codes.  JUPITER then < F6> then SORT  Numbers - only when mathematical then 333 then 3 then 22 then 124 operation is performed on them. then  Character data sorted from left to right.  Discuss results of this sort– sorting by units not numerically.  Numeric data sorted by units.  Key in the following commands.  ASCII sort sequence: Each command followed by - Punctuation marks (including spaces).  SORT then 333 - Numbers. 3 then 23 - Letters (lowercase then uppercase). then 124 then  Carolyn Smith before Robert Nesler because  Discuss result of this sort – using SORT command looks at entire line and spaces forces lines to be same Carolyn comes before Robert. length – placing number digits in  Left justify character data and right justify proper position. numeric data.  Activity completed.  Discussion Question (9) -Explain how the FILTERS AND REDIRECTION SORT command works. Describe any  Filters and Redirection limitations of the SORT command.  Standard output of filters is screen

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 4 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

display.  Can redirect both the output and the input SECTION 9.8 (p. 450) of the filter commands.  Slide 28  Filter commands not usually used with actual keyboard input.  Filter commands used with input redirected from a file, a device, or another command. ACTIVITY—USING THE SORT COMMAND WITH REDIRECTION  Using the SORT Command with Redirection SECTION 9.9 (pp. 450454)  DATA disk in Drive A, A: \> displayed.  Slides 29-30  Activity steps.  /R – sorts in reverse or descending order.  Key in:  WXP – SORT does not require < prior to  COPY C:\WUGXP\STATE.CAP file being sorted.  SORT < STATE.CAP  /+n – sorts by column number .  SORT STATE.CAP Column – on screen is place occupied by  SORT /R < STATE.CAP  SORT /+17 STATE.CAP one character.  SORT /+17 STATE.CAP > Column number really means character SORTED.CAP number.  TYPE SORTED.CAP +17 – seventeenth position in list.  SORT/ +17 STATE.CAP /O  /O – stores sorted data in a file (faster than BYCITY.CAP redirection).  TYPE SORTED.CAP  TYPE BYCITY.CAP  Till now, changed data not been saved to a  Activity completed. file. Now creating a new file.  Break down SORT < STATE.CAP - Can be written to screen (standard output) The FIND Filter - Therefore can be directed to file.  The FIND Filter.  Searches for specific character string by  Discussion Question (10) -Identify one place enclosing it in quotation marks. that standard output can be written.  Command is looking for exact match, therefore, is case sensitive unless use /I SECTION 9.10 (p. 454) parameter.  Slides 31-34

 Example - Use TYPE command, FIND command can tell if the word “indictment” is in a file.  Search option available in Start menu - can  FIND syntax: search files for text. FIND [/V] [/C] [/N] [/I] "string" [[drive:] Command line not as reliable in WXP as [path] filename [ …]] it was in W2K  /V - displays all lines not containing specified string  /C - displays only the count of lines

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 5 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

containing a string.  /N - displays line numbers with  Discussion Question (11) - What is the purpose displayed lines. of the FIND command?  /I - ignores the case of characters  Discussion Question (12) - What are four when searching for the string. parameters that are used with the FIND  /OFF [LINE] - Do not skip files command and what do they represent? with offline attribute set  "string" - specifies the text string to  Discussion Question (13) - Why must the find. character string be enclosed in quotation marks  [drive:] [path] filename - specifies a when using the FIND command? file or files to search.  If pathname is not specified, FIND searches the text types at the prompt or piped from another command.  FIND command at command line can help find a file based on content.

ACTIVITY—USING THE FIND FILTER  Using the FIND Filter  DATA disk in Drive A, A: \> displayed.  PERSONAL.FIL on DATA disk.  Must use double quotes.  Activity steps.  Key in: SECTION 9.11 (pp. 454-457)  FIND "Smith" PERSONAL.FIL  Slides 35-36  FIND /V "Smith" PERSONAL.FIL  FND /N "Smith" PERSONAL.FIL  FIND /C "Smith" PERSONAL.FIL  Character string – enclosed in quotes  FIND /I "Jones” PERSONAL.FIL  FIND – case sensitive.  FIND “Jones” PERSONAL.FIL /I\I  Parameters.  Activity completed. /I – ignores case. /V- search file for anything except what is in quotes. /N – finds specific line # of each occurrence. /C – numeric count of # of times specific character string in file.

PIPES  Pipes  Pipes - output from one program becomes input to next program.  Not limited to two programs.  Pipes used with filter commands.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 6 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

 Used to further refine data.  Symbol is broken bar | SECTION 9.12 (pp. 457-458)  Location of pipe not standard  Slides 37-41  Filter commands.  Commands read and write temporary  Term “pipe” refers to flow of info. from one files to the disk. command to the next.  Filters are external commands.  Pipes move information the way water pipes  OS must be able to access the move water. commands.  Filter commands.  All files (even temporary ones) must be - Used with any command that uses standard named. output or input.  Temporary files "hold" data until the - Transforms data to meet needs. next command can process it.  Temporary files are deleted when pipes are done filtering.  Discussion Question (14) - What are pipes?  Filter commands will not work if a disk is  Discussion Question (15) - Are there any write-protected. restrictions on the use of pipes? What are they? THE MORE FILTER  The MORE Filter  MORE displays one screenful of data at a time.  Useful when want to read long text file.  Pauses after screen is full. - Press any key - next screen is displayed. SECTION 9.13 (pp. 458-459)  No more data in file – returns to system  Slides 42-44 prompt.  MORE can be both redirected and used with a pipe.  Extended features P n Display next n lines S n Skip next n lines F Display next file Q Quit = Show line number ? Show help line Display next page  MORE syntax: See PowerPoint slide #43. Display next line  [drive:] [path] filename - specifies file (s) to display one screen at a time.  command-name - specifies a command whose output will be displayed.  /E – enable extended features.  /C – clear screen before displaying page.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 7 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

 /P – expand FormFeed character.  /S – squeeze multiple blank lines into a single line.  Discussion Question (16) - How is the MORE  Tn – expand tabs to n space (default 8). command used?  Switches can be present in MORE environment.  +n – Start displaying the first file at line n.  files – List of files to be displayed. Files in the list are separated by blanks.  If extended features are enabled – following commands accepted at –More —prompt  See PowerPoint slide #44.  Right column describes features

ACTIVITY—USING THE MORE FILTER  Using the MORE Filter  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in all bold commands.  DIR | MORE then Return to System Prompt  DIR | MORE then Return to System Prompt  DIR | MORE then Return to System SECTION 9.14 (pp. 459-466) Prompt  Slides 45-46  DIR | SORT / +39 | MORE then Return to System Prompt  By continuing to press will  MORE PERSONAL.FIL then Return to System Prompt return to system prompt.  TYPE PERSONAL.FIL | MORE then  With extended features, pressing Q will Return to System Prompt break command and return to system  MORE PERSONAL.FIL /C +20 prompt.  MORE SORTED.CAP BYCITY.CAP  Can connect several commands with pipes /C and filters.  Press  MORE can be used with any ASCII file.  /P parameter can only be used with DIR. Demonstrate by using TYPE  MORE SORTED.CAP BYCITY.CAP PERSONAL.FIL /P.  Press  /P not a valid TYPE parameter.  Close all open windows and  Records are lines of information in a data return to desktop environment file. (GUI).  MORE allows viewing file at a specified  Activity completed. line or record number. OTHER FEATURES OF MORE  Other Features of MORE

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 8 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

 If extended features are enabled – have more choices available to you with  More filter can be used with more than one MORE command. file.  See PowerPoint slide # 45.  /C parameter clears the screen before each  Command Prompt by default display. enables shell extensions  CMD /X - Enables shell SECTION 9.15 (pp. 466-467) extensions  CMD /Y -Disables shell  Slides 47-50 extensions.  Open Command Prompt window are  Open Command Prompt window – running running a shell. a shell.  Shell is command interpreter used to pass  Commands that use shell extensions: DEL, commands to operating system. COLOR, CD, MD, PROMPT, PUSHD, POPD, SET, SETLOCAL, ENDLOCAL, IF, FOR, CALL, SHIFT, GOTO, STARTS, ACTIVITY—USING THE EXTENDED ASSOC, and FTYPE FEATURES OF MORE  Command name with /? - full details on  Using the Extended Features of MORE what can be done with command.  DATA disk in Drive A, A: \> displayed.  Activity steps.  Click Start/Run.  Discussion Question (17) – What are two  Key in: CMD.EXE /X useful features that extended features  Click OK. provides for the MORE command?  Key in: A: then MORE PERSONAL.FIL.  Press twice then Q.  Key in: MORE PERSONAL.FIL SECTION 9.16 (pp. 467-471)  Key in: P then 5  Slides 51-52  Press then = sign then S  Key in: 3 then press  Run remembers last command keyed in.  Close all open windows.  /X – ensures that you are going to be able to  Activity completed. use extensions to commands.  With extensions enabled Pressing (referred to as in syntax) - displays next page Pressing (referred to as in syntax diagram) - displays next line in file  To exit MORE command key in Q (returned to system prompt). COMBINING COMMANDS WITH PIPES  Press P where MORE stopped – can request AND FILTERS how many lines you want to display.  Combining Commands with Pipes and Filters  = sign – displays which line number you are  Use pipes so standard output from one on. command is standard input to next  S – asked how many lines you want to skip command (filters). in your display.  Use pipes to connect two or more programs and create a flow of data.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 9 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1 Instructor: Prof. Michael P. Harris Chapter 9 ITSC 1405 – Intro to PC Operating Systems Pipes, Filters, and Redirection

 Redirecting output from a command is an SECTION 9.17 (p. 471) “instead of” process.  Slides 53-55  When combine use of pipes and > - redirection becomes end of pipeline.  When pipe symbol used – must be  Last step in process. command on both sides of symbol. ACTIVITY—COMBINING COMMANDS  Redirection used with “pipeline,” -  Combining Commands command does not have to be on either side  Open Command Prompt window. of > or >>  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in:  FIND "Teacher" PERSONAL.FIL | FIND "CA"  FIND "Teacher" PERSONAL.FIL | SECTION 9.18 (pp. 471-474) FIND "CA" > TEACHER.FIL  Slides 56-57  TYPE TEACHER.FIL  FIND "PrOfeSSor" PERSONAL.FIL Data in PERSONAL.FIL not changed – | FIND "AZ" | SORT   Press up arrow once searched data so display only those  Use left arrow key until cursor in under P lines/records that met your requirements. in PERSONAL.FIL  Because FIND command sends output to  Key in /| screen can redirect output.  Press key then  Can use same filter more than once in same  Key in: command line.  DIR | SORT /+39 | MORE  Can use filters in combination in same  Return to system prompt. command line.  Key in:  DIR | FIND “

“ SORT +39  Pipe.  Quotation marks around so - Command on either side of pipe. command line would not use < and > - Taking standard output of a command and as redirection. using it as standard input to next command.  Activity completed.  Redirection. - An “instead of” action. - Only get one output place. - Output goes to last place it is directed to go.  Primary use of pipes and filters is to manipulate the standard output and standard input of commands.  Rarely use pipes and filters to sort or find data in text or data files.

Discussion Question (18) – How can combining filters be useful?

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line Page 10 Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1

Recommended publications