Article View: comp.sys.ibm.pc
Article #8489Re: Redirection and piping
From: Jeff Pipkins
Date: Tue, 12 Dec 1989 16:19
Date: Tue, 12 Dec 1989 16:19
67 lines
3417 bytes
3417 bytes
In article <89345.110313MHS108@PSUVM.BITNET> MHS108@PSUVM.BITNET (Mark Solsman) writes: > Can any one out there explain how to use the redirection and the pipeline >features with ms-dos? [...] I have a hard disk utility that makes you enter >the drive number from the keyboard. I would like to redirect the drive numbers >from another file, so that I could run that unattended [...]. A program that reads its input from stdin and writes it output to stdout is known as a filter. A pipe is an operating system method of linking several of these filters together like a chain of elephants, so that the output of one becomes the input of another. Example: C:\> echo A: | diskutil The echo program is not, strictly speaking, a filter because it does not take its input from stdin. Instead, it takes its input from its command line and merely writes that to stdout. The pipe symbol '|' says that the output (stdout) of the echo program is to be used as the input (stdin) of the diskutil program. NOTE that if the diskutil program reads its input directly from the keyboard or with a BIOS call, then it will not read the input from stdin and you will have to type the drive letter anyway. That's why this doesn't work as you would expect: C:\> echo Y | format c: (kids, don't try this at home...) Redirection is the act of specifying a file or device for stdin or stdout. For example: C:\> more <read.me The more program is a filter. The <read.me specifies that the input for more is to come from the file read.me. This is faster than saying: C:\> type read.me | more But has a similar effect. The > symbol is used to redirect stdout. Note that stdin and stdout can be redirected to a device as well as a file: C:\> echo ^L >lpt2 Will send a form feed character to the printer on LPT2. So you might also try something like this: C:\> echo Y >yes.ans C:\> diskutil <yes.ans These techniques can be combined to save you from writing new programs. C:\> dir c:\ | find '<DIR>' | sort >dirlist.c This line will do a directory of drive C:, throw away all lines that don't have '<DIR>' on them, sort the lines (by name, ext) and write it to a file called dirlist.c. In other words, dirlist.c will contain a sorted list of the directories on drive C: You can view this list later by typing C:\> more <dirlist.c I hope the presence of pipes encourage you to write small filters that can perform just one function. A collection of these is quite powerful. Instead of having a program to convert MS-DOS and UNIX files back and forth, I use pipes of small filters such as dtab (expands tabs to spaces), lf2crlf (converts line feed characters to CR/LF sequence) and dz (removes control-Z characters from end of file). By doing this, these small filter programs are reusable in many other situations. Hope this helps. -Jeff pipkins@imagen.com
Technical Details
Message-ID:
<63@qmsseq.imagen.com>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!think!ames!sun-barr!newstop!sun!imagen!qmsseq!pipkinsReferences:
<89345.110313MHS108@PSUVM.BITNET>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!think!ames!sun-barr!newstop!sun!imagen!qmsseq!pipkins From: pipkins@qmsseq.imagen.com (Jeff Pipkins) Newsgroups: comp.sys.ibm.pc Subject: Re: Redirection and piping Message-ID: <63@qmsseq.imagen.com> Date: 12 Dec 89 16:19:43 GMT References: <89345.110313MHS108@PSUVM.BITNET> Reply-To: pipkins@qmsseq.UUCP (Jeff Pipkins) Organization: QMS Inc., Mobile, Alabama Lines: 67