DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
About files and directories

Specifying command input and output

Almost all UNIX commands require an input and an output; that is, some information to read and process, and somewhere to store the results. If you do not tell a command where to find its input and output, it makes assumptions about where to read and write information. These assumptions are called the standard input and standard output. These are, respectively, your keyboard and your screen by default. Alternatively, if you specify the name of a file, most programs will obtain their standard input by reading the file.

In addition to standard input and standard output, most programs need a standard error, to which they report any failures or errors. By default, the standard error is directed to the same place as the standard output, your screen.

You can make commands redirect their standard input and output by using the symbols ``<'' and ``>'' on the command line, followed by the name of a file to read input from or write output to. For example sort < list1 > list.out makes sort treat list.1 as its input, and send its output to list.out.

If you send the output of a program to a file, and the file already exists, the existing file will be ``clobbered'' or overwritten. If you are using the Korn shell, you can prevent this from happening by using the noclobber variable. You can identify your login shell by entering the following command:

   $ grep ${LOGNAME} /etc/passwd
   martins:x:13990:1014:Martin Smith:/u/martins:/bin/ksh
The last data field, after the last colon, identifies your login shell, in this case, the Korn shell (/bin/ksh). If you are using the Korn shell, you can turn on the noclobber feature, by typing the following:
   $ set -o noclobber
C shell users should type set noclobber: this feature does not exist in the Bourne shell. To turn off the feature in the Korn shell, type set +o noclobber. C shell users should type unset noclobber. To protect yourself from accidentally clobbering files, Korn shell users should add the appropriate noclobber line to your .profile file: C shell users should add it to .cshrc.

To append the output of a command onto the end of a file, use the >> notation instead of >. For example, the following command line appends the output from sort to the end of the existing contents of file2 rather than overwriting them:

   $ sort file1 >> file2
The C shell will not let you append the standard output to a file if the file does not exist and noclobber is set. The Bourne or Korn shells simply create the file.

Typically, your shell will support a wider range of redirection operators than those discussed here. For details, refer to ksh(1), sh(1) or csh(1) as appropriate.

Forcing a program to read standard input and output

Many programs get their input from the standard input and write their output to the standard output: others read from or write to named files.

For example, cat can be used to create a file using information typed in at the keyboard, as follows:

   $ cat > file8
In this case, file8 is a file that does not already exist within the current directory. You can then proceed to type text into the file. You can press <Bksp> to correct any mistakes you make on the current line, although you cannot correct mistakes on previous lines. Press <Ctrl>D when you have finished, to signal ``end of file'' to cat.

Alternatively, you can use the echo command to place text in a file, as follows:

   $ echo "Hello there!" > testfile
This results in a file (testfile) containing the text ``Hello there!''.

Note that the quote marks are stripped out by the shell when you use echo to print to a file.

The following use of the cat command places the contents of the three named files into outfile:

   $ cat file1 file2 file3 >outfile
Suppose you want cat to read file1, then read something from the standard input (your terminal) instead of from file2, then read file3. There exist some special device files to make life easier: /dev/stdin, /dev/stdout, and /dev/stderr. These three special files correspond to the standard input, standard output, and standard error, respectively. The following command line uses /dev/stdin in precisely this way:
   $ cat file1 /dev/stdin file3 >outfile
In this case, file1 is copied to outfile; then cat reads the standard input (your terminal) until you press <Ctrl>D (to signal end of file); then finally appends file3 to outfile.
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 22 April 2004