In the UNIX system, some commands expect to receive their input only from the keyboard (standard input) and most commands display their output at the terminal (standard output). However, the UNIX system lets you redirect both input and output to other files and programs. With such redirection, you can tell the shell to
To redirect input and output, you use a set of operators: the less than sign (``<''), the greater than sign (``>''), two greater than signs (``>>''), and the pipe (``|'').
To redirect input, specify a filename after a less than sign (``<'') on a command line:
command < fileWhen is this mechanism useful? A typical example is when you want to send someone--via the mail command--a message or file you've already created. By default, the mail command expects input from standard input (that is, the keyboard). But suppose you have already entered the information to be sent (to a user with the login name jim) in a file called report. Rather than retype that information, you can simply redirect input to mail as follows:
mail jim < report
To redirect output (from standard output) to a file,
specify a filename after
the greater than sign (``>'') on a command line:
command > file
Before redirecting the output of a command to a particular file, make sure a file by that name does not already exist, unless you do not mind overwriting it. The shell does not allow you to have two files of the same name in one directory. Therefore if you redirect the output of a command to a file with the same name as an existing file, the shell will overwrite the contents of the existing file with the output of your command. Keep this in mind when redirecting output; the shell does not warn you when it is about to overwrite a file.
To make sure that no file exists with the name you plan to use, run the ls command, specifying your proposed filename as an argument. If a file with that name exists, ls will list it; if not, you will receive a message that the file was not found in the current directory. For example, checking for the existence of the files temp and junk would give you the following output:
$ ls temp temp $ ls junk junk: no such file or directory $This means you can name your new output file junk, but you cannot name it temp unless you no longer want the contents of the existing temp file.
To keep from destroying
an existing file, you can also use
the double greater than symbol (``>>''), as follows:
command >> file
This appends the output of a command to the end of the file file. If file does not exist, it is created when you use the ``>>'' symbol this way.
The following example shows how to append the output of the cat command, (described in ``Shell programming'') to an existing file. The cat command prints the contents of the files to the standard output. If it has no arguments, it prints its standard input to the standard output. First, the cat command is executed on both files without output redirection to show their respective contents. Then the contents of trial2 are added after the last line of trial1 by executing the cat command on trial2 and redirecting the output to trial1.
$ cat trial1 This is the first line of trial1. Hello. This is the last line of trial1. $ $ cat trial2 This is the beginning of trial2. Hello. This is the end of trial2. $ $ cat trial2 >> trial1 $ cat trial1 This is the first line of trial1. Hello. This is the last line of trial1. This is the beginning of trial2. Hello. This is the end of trial2. $
Redirecting output is useful when you do not want it to appear on your screen immediately or when you want to save it. Output redirection is also especially useful when you run commands that perform clerical chores on text files. Two such commands are spell and sort.
The spell program compares every word in a file against its internal vocabulary list and prints a list of all potential misspellings on the screen. If spell does not have a listing for a word (such as a person's name), it will report that as a misspelling, too.
Running spell on a lengthy text file can take a long time and may produce a list of misspellings that is too long to fit on your screen. spell prints all its output at once; if it does not fit on the screen, the command scrolls it continuously off the top until it has all been displayed. A long list of misspellings will roll off your screen quickly and may be difficult to read.
You can avoid this problem by redirecting the output of spell to a file. In the following example, spell searches a filenamed memo and places a list of misspelled words in a filenamed misspell:
$ spell memo > misspellSee the spell(1) manual page for all available options and an explanation of the capabilities of each.
The sort command arranges the lines of a specified file in alphabetical or numerical order Because users generally want to keep a file that has been alphabetized, output redirection greatly enhances the value of the sort command.
Be careful to choose a new name for the file that will receive the output of the sort command (the alphabetized list). When sort is executed, the shell first empties the file that will accept the redirected output. Then it performs the sort and places the output in the blank file. If you type
sort list > listthe shell will empty list and then sort nothing into list.
Running a command in background does not affect the output of the command; unless it is redirected, output is always printed on the terminal screen. If you are using your terminal to perform other tasks while a command runs in background, you will be interrupted when the command displays its output on your screen. However, if you redirect that output to a file, you can work undisturbed, except when an error occurs.
For example, in ``Special characters'', you learned how to execute the grep command in background with ``&''. Now suppose you want to find occurrences of the word ``test'' in a filenamed schedule. Run the grep command in background and redirect its output to a file called testfile:
$ grep test schedule > testfile &You can then use your terminal for other work and examine testfile when you have finished it.
The ``|'' character is called a pipe. Pipes are powerful tools that allow you to take the output of one command and use it as input for another command without creating temporary files. A multiple command line created in this way is called a pipeline.
The general format for a pipeline is:
command1 | command2 | command3 . . .The output of command1 is used as the input of command2. The output of command2 is then used as the input for command3.
To understand the efficiency and power of a pipeline, consider the contrast between two methods that achieve the same results.
banner happy birthday > message.tmp
mail david < message.tmp
banner happy birthday | mail david
The cut and date commands provide a good example of how pipelines can increase the versatility of individual commands. The cut command allows you to extract part of each line in a file. It looks for characters in a specified part of the line and prints them. To specify a position in a line, use the -c option and identify the part of the file you want by the numbers of the spaces it occupies on the line, counting from the left-hand margin.
For example, suppose you want to display only the dates from a file called birthdays. The file contains the following list:
Anne 12/26 Klaus 7/4 Mary 10/18 Peter 11/9 Nandy 4/23 Sam 8/12The birthdays appear between the ninth and thirteenth spaces on each line. To display them, type:
cut -c2-13 birthdaysThe output is shown below:
12/26 7/4 10/18 11/9 4/23 8/12The cut command is usually executed on a file; however, piping makes it possible to run this command on the output of other commands, too. This is useful if you want only part of the information generated by another command. For example, you may want to have the time printed. The date command prints the day of the week, date, and time, as follows:
$ date Tue Dec 24 13:12:32 EST 1991 $Notice that the time is given between spaces 12 and 19 of the line. You can display the time (without the date) by piping the output of date into cut, specifying spaces 12-19 with the -c option. Your command line and its output will look like this:
$ date | cut -c12-19 13:14:56 $See the date(1) manual page for all available options and an explanation of the capabilities of each.
The output of most commands may be captured and used as arguments on a command line. Do this by enclosing the command in grave accents (` . . . `) and placing it on the command line in the position where the output should be treated as arguments. This is known as command substitution.
For example, you can substitute the output of the date and cut pipeline command used previously for the argument in a banner printout by typing the following command line:
$ banner `date | cut -c12-19`Notice the results: the system prints a banner with the current time.
``Shell programming'' shows you how you can also use the output of a command line as the value of a variable.