More Command Line Options Pipes Redirection Standard files












- Slides: 12
More Command Line Options Pipes, Redirection, Standard files Creative Commons License – Curt Hill.
Introduction • These are two features pioneered by UNIX and then partially copied in DOS • A pipe is a connector of the output of one program to the input of another • Redirection is changing the where the input of a program comes from or the output goes to Creative Commons License – Curt Hill.
Redirection • The idea of file redirection is an instance of device independence • We want our files to be independent of the type of device • Redirection mainly applies to the standard input, output and error files • Not every command allows redirection, but most of the standard ones do Creative Commons License – Curt Hill.
Output Redirection • Output redirection use the > preceding the new file • Thus the command: copy *. dat *. txt >results. lst – will do the copy as always but the copy output will be in the results. lst file – Copy output is the names of the files copied and a count Creative Commons License – Curt Hill.
Other Redirections • Somewhat less common, because most DOS commands take no input – They know everything they need from the command line parameters • The input redirection character is < followed by the file • The error redirection is >> followed by the file Creative Commons License – Curt Hill.
Standard Files • DOS had several standard file specifications • These included lpt 1: lpt 2: nul: among others • These substituted for a normal file name specification • Thus: copy xyz. dat lpt 1: prints the file Creative Commons License – Curt Hill.
Standard Files • The files lpt 1: lpt 2: lpt 3: only work if this is an attached printer – Not a network printer • Thus most of the standard files are not used much any more • Only the nul: is of much value – The : may be left out • Silencing a copy: copy a*. dat *. txt >nul – does the copy but nothing sent to console Creative Commons License – Curt Hill.
Pipes • Once we can redirect standard input and output then pipes are easy • A pipe is just a temporary file that connects two programs • The character is | and should be put between two programs • The OS provides a temporary file – The first program places its standard output in the file – The second program reads its standard input from the file – The file is. Creative then. Commons deleted License – Curt Hill.
Example • Assume abc and def are both programs abc | def • The abc program sends its standard output to an OS supplied temporary file • The def program reads it from there • This leads us to the concept of a filter Creative Commons License – Curt Hill.
Filters • Much more common on UNIX than DOS • A filter takes some data from standard input • It transforms it in some way • It writes it to standard output Creative Commons License – Curt Hill.
Example • A common filter on UNIX was lc – It prefixed each line with the current line number • Suppose that program mon produces a lot of output then: mon | lc >mon. out – will prefix line numbers and save in a file Creative Commons License – Curt Hill.
Finally • Pipes and redirection were very important in the UNIX environment – Especially before the GUI era • They were not as important in the DOS world – It was not automatic to write a program to use them – They are still handy Creative Commons License – Curt Hill.