Working with files and directories

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.
Next topic: Running a sequence of commands
Previous topic: Specifying command input and output

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003