Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lectures_SSD2_Yermakova / Lectures_SSD2 Yermakova.doc
Скачиваний:
226
Добавлен:
25.02.2016
Размер:
3.16 Mб
Скачать

Redirection and Piping

The command-line processor provides some additional syntax that can be used to control where a program gets its input from (if not the keyboard), and where its output will go (if not to the display). This facility is called redirection. Most commands and programs are written to refer to the virtual devices known as Standard Input and Standard Output for I/O purposes. On a PC, the command processor coordinates with the operating system to redirect all data from the keyboard driver to the Standard Input virtual device and all data from the Standard Output virtual device to the display driver. Redirection allows the user to change this, by associating the virtual devices with some other location for data, such as a disk file. The left angle bracket ( < ) is used to redirect standard input, and the right angle bracket ( > ) is used to redirect standard output. For example, the command dir >files.txt causes the dir command to write its directory listing to the file FILES.TXT in the current directory.

Here are some more examples of the use of redirection:

  • List all files in the root folder to the printer instead of the display: dir c:\*.* >lpt: 

  • Create a new text file called "temp.txt" containing a list of files in the root folder. (Note: Nothing will appear on the display.): dir c:\*.* >c:\temp.txt 

  • Run "program.exe" and have it get data from the text file INPUT.DAT rather than the keyboard: c:\program.exe <c:\input.dat 

  • Run "program.exe", have it get data from the text file "input.dat" rather than the keyboard, and display the output on the printer: c:\program.exe <c:\input.dat >lpt:

Another bit of syntax allows you to append output to the end of a file—or else create the file if it does not already exist. This is done using the >> redirection notation. For example, to append a listing of the root directory to the end of the file TEMP.TXT, do the following: dir c:\*.* >>c:\temp.txt. If you do this twice you will have two copies of the directory listing in the file, because the >> notation appends to the file rather than overwriting it.

Piping is a function of the command processor that links two commands together via redirection: the output of the first command becomes the input to the second. In order to accomplish this, Standard Output of the first command is redirected to a temporary file created by the command processor. The second command on the command line is then invoked by the command processor, which redirects its Standard Input to the temporary file that was just created. After the second command completes, the temporary file is deleted by the command processor.

To pipe data from one command to another as described above, the user separates the two commands on the command line with a vertical bar character ( | ). (The vertical bar character is sometimes pronounced, "pipe.") We have already seen an example of piping with the more command:

Display all the options of the dir command: dir /? | more

Another command commonly used with piping is the findstr command, which finds lines in a file that match a certain string, or pattern.

To display current environment variables whose names contain the string "HOME", do this: set | findstr /i home

The /i switch tells findstr to treat uppercase and lowercase letters as the same.