Main index

Introducing UNIX and Linux


Getting started

Overview
Using UNIX
      Usernames
      Logging in
Logging out
Commands
      Typing in commands
      Commands and options
Communication with other users
      Email
      Other communication facilities
Files
      The editor Vi
            Vi commands (command mode)
            Vi commands (colon mode)
      Other editors
Input and output
      Scripts
      Here-documents
      Pipes
      Making copies of input and output
      Pagers
Emergencies
Getting help
Summary
Exercises

Input and output

We now leave discussion of editors and return to consideration of the shell.

When you type in text at the terminal, the input is buffered; the characters you type in are not immediately transmitted to the shell. They are initially stored in a temporary area of the computer's memory called a buffer; the contents of the buffer are usually transmitted to the shell at the end of each line, when you press Return. A consequence of buffering is that if you make an error when typing in data on your terminal, you can correct it. Systems vary in the amount of 'line-editing' they allow, but you can expect at least the following:

DEL deletes the most recent character typed in
ctrl-U deletes the whole of the line currently being typed

Once you have typed in a command, and the command begins to be executed, you may be requested to type in data as input to the command, which will, in response, send messages as output. Output consists of the stream of characters usually sent to the screen. The commands we have looked at already - ls, date and who - give output but require no input. Some commands - such as vi - will 'interact' with you, and need you to type in data while they are running. For simple commands that require input, the input is formed by the characters you type in at the keyboard. More complex commands (vi is in this category) may also have other input and output, but this description of how a command communicates with the system will serve us for the present.

Each command has associated with it three input and output streams. They are called standard input, standard output and standard error (often abbreviated to stdin, stdout and stderr respectively). Normally, standard input will be taken from the keyboard, and standard output and standard error will both be sent to the terminal screen. A command may, in addition, have other input and/or output streams. Each input/output stream is also given a number: 0 for standard input, 1 for standard output and 2 for standard error:

Stdin, stdout and stderr

Commands that require input will usually take it from standard input, and the normal output of a command will go to standard output. Standard error is used for other messages, typically warning messages if the command could not be executed as intended (for instance, if you try to print a file that does not exist). Thus, the output from a command and its diagnostics can be separated, as we shall discuss later. The messages sent to standard error are not always error messages, and will include general information of use to you that is not part of the usual output of a command - it's called 'standard error' simply because the majority of messages sent to it tend to be error messages.

To terminate input to a command, type ctrl-D on a line by itself. When you log in, each command you type represents a new line of input to your login shell. A shell is simply a program that takes commands as its input. Terminating input to your login shell by typing ctrl-D causes the shell program to finish; that's all that logging out really is.

A useful command is cat (catenate), which takes names of zero or more files as arguments and copies them, in order, to standard output. We can use cat to display files we have created. If cat has no arguments, standard input is copied directly to standard output. For instance, to display the file myfile which was created earlier using Vi:

cat myfile
hello
there

With no arguments, cat will take its input from the standard input (and echoes each line as soon as it has been typed in since input is buffered):

cat
abc       -- standard input
abc      -- standard output
def       -- standard input
def      -- standard output
ctrl-D

It is possible, indeed common, to redirect input and/or output. Instead of input coming from the keyboard, it can be the contents of a file or the output of another command. Similarly output from a command can be sent to a file or used as input to another command.

The symbol < indicates that standard input should come from a file, and the following will produce the same output as cat myfile:

cat <myfile
hello
there

So, having created file myfile, you can display its contents on the screen in two ways. In the first case, cat is given one argument, the filename myfile, the contents of which are copied to standard output; in the second, cat is given zero arguments and thus the standard input, which has been redirected from myfile, is sent to standard output.

Redirection from file

The standard output can be directed to a file. The output from date, for example, can be sent to file xyz:

date >xyz

Redirection from file

Now, type cat xyz (This is probably the simplest way to create a file without needing an editor) to examine the contents of file xyz. The symbol > indicates that the standard output from date is to be sent to a file whose name immediately follows. In the case of cat, we can do the same, but remember that cat also requires input from the standard input stream. The following replaces the contents of xyz:

cat >xyz
have a nice
  
-- standard input
day           -- standard input
ctrl-D

You can copy several files to standard output:

cat myfile xyz
hello
there
have a nice
day

Redirection from stdin to file

A command can redirect both its input and its output; the following will create a copy of file myfile called def. In this example cat takes zero arguments.

cat <myfile >def

Redirection from file to file


The effect of this is the same as if you had not redirected standard input and had given cat a single argument myfile:

cat myfile >def

Beware that you cannot take input from and send output to the same file - it won't work. The file you try to take the input from will be destroyed in preparation for receiving the output before anything is read from it.

A command that requires a filename as an argument can use the symbol - (hyphen) to denote standard input. The following dialogue illustrates this:

cat myfile -
hello      -- file myfile
there      -- file myfile
Mike        -- standard input
Mike       -- standard output
ctrl-D

Thus we can refer to the standard input in situations where simple redirection using <</code> would be inappropriate. The hyphen can be used wherever the name of a file is used, so you can refer to standard input as a file (rather than using the notation for redirection).

Worked example

Create a file called theusers containing a list of those users currently logged in.
Solution: The command who will send a list of users to standard output, so we need only redirect the standard output of who to the file:

who >theusers

If you now type cat theusers a list of users who were logged on will be displayed on the screen.

Having created files, you will from time to time wish to remove some of them. The command rm (remove) deletes a file. Take care, as it is very easy to delete a file accidentally - if you use rm with option -i (inquire) it will ask you to confirm that you do in fact wish to delete the file:

rm -i theusers
rm: remove 'theusers'? y

We can separate standard output and standard error. To illustrate this we set up cat so that it produces good output and an error message by asking it to copy two files, one of which (say myfile) exists, and the other (qwerty, for instance) does not. We send the standard output to file output and the standard error to file error:

cat myfile qwerty
hello
there
cat: qwerty: No such file or directory
cat myfile qwerty 2>error 1>output
$ cat output

hello
there
cat error
cat: qwerty: No such file or directory

Note that this notation will not work for the C shell and its derivatives.

By prepending the symbol > with the number of the output stream, that stream is redirected to the named file. When > is used alone, it is equivalent to 1>, so that the diagnostic messages are still sent to your terminal unless you explicitly request otherwise.

Although in normal use UNIX commands have only one input stream, it is possible to write programs that have more than one such stream. In this case the same syntax applies as for output, namely that 0< denotes taking input stream number 0 from a file, and 0< is synonymous with <.

If a command redirects output, from whatever stream, to a file using >, then if that file does not exist it will be created. If the file does exist, then it will be overwritten and its previous contents will be lost. If you wish to append data to the end of a file, then replace > by >>. Appending data to a file means adding it on at the end without affecting the data already stored in it. Consider the following dialogue:

date >outfile
date >>outfile
cat outfile
Tue Dec 4 20:10:39 GMT 2001
Tue Dec 4 20:10:47 GMT 2001

The first time date is called, the standard output is sent to file outfile; the second time, the output has been added to the end of the same file.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck