Input to Unix commands is normally given from the keyboard. For example you can use the cat command interactively:
% cat Hello - you typeHello - responsethere - you typethere - response^D - you type%
Note that input from the keyboard is terminated with the end-of-file character, usually ^D. For another example consider the spell command, which is the unix spelling checker:
% spell - you typeInput to the spell ulitity - you typeis typed at the keyboard - you type D - you typeulitity - response
The spell command outputs words that are incorrectly spelled in the input.
Output from Unix commands is normally displayed on the screen. For example:
% spell Input to the spell ulitity is typed at the keyboard ^D ulitity - output
Try out the spell checker. See how it copes with British spellings (remember it's an American system), proper nouns, hyphens and recently coined vocabulary.
It is possible to redirect standard input so that the input is taken from a file. Imagine you wish to check for spelling errors in a report. A text can be put into the file report, which can be fed into the spell command:
% cat > report Input to the spell ulitity can come from a file ^D % spell < report ulitity
The < character is used to re-direct the input from the file report to the command spell. The general format for re-direction of user input is:
command < filename
Another common use of re-direction of standard input is to mail a file to another user. The command:
% mail lnp8zz < report
will mail the file report to local user lnp8zz.
You do not always want the output from a Unix command to be displayed on the screen. It has already been shown how it is possible to direct the output from the cat command to a file. Imagine you want a list of your files and directories kept in a file. You would use the command:
% ls > filelist
The > character is used to re-direct the output from the command to the file called filelist. The general format for re-direction of user output is:
% command > filename
Note that output directed to the file /dev/null is effectively discarded. This is the system 'wastebasket'.
Another example involves directing the output of echo to a file:
echo "Hello there" > greeting
This would normally overwrite any existing contents of the file greeting. Study the following sequence:
% echo "Hello there" > greeting % cat greeting Hello there % echo "This instead" > greeting % cat greeting This instead
It is possible to append output to a file, rather than overwriting it, by using the >> operator. For example:
% echo "Hello there" > greeting % cat greeting Hello there % echo "and goodbye" >> greeting % cat greeting Hello there and goodbye
Look carefully at the difference between these two examples.
It is possible to re-direct both standard input and output. If you have a report containing many spelling mistakes you may wish to keep a list of the mistakes in a file. You can do this using the following command:
% spell < report > errors
Output from one command can be sent ('piped') to the input of another command using the | character:
command1 | command2
A common use for pipes is to control the output of large files to the screen. It is possible to send output to the more command so that only one screenful at a time is output. If the command
% ls -l
is used to give a long listing of all files and directories there may be too many lines to see them all at once on the screen. (If you don't have many files, move to /etc where there should be plenty.) Output from ls -l can be piped to more as follows:
% ls -l /etc | more
You can then use the usual more commands to control the output.
In the output from ls -l, directories are identified by the d character at the start of each line. A list of just the directories can be obtained by piping the output of this command to the grep command, giving grep an option which will list only lines containing the d character at the start of the line. The command is:
% ls -l | grep "^d"
The commands sort and grep are often used when piping. For example:
% cat phonenos | sort | lpr
will send an alphabetically sorted list of the phone numbers contained in the file phonenos to the line printer. The command:
% cat phonenos | grep leeds | sort | lpr
will send a sorted list of phone numbers containing the string 'leeds' to the line printer.
1. Put a listing of the files in your directory into a file called filelist. (Then delete it!)
2. Create a text file containing a short story, then use the spell program to check the spelling of the words in the file.
3. Redirect the output of the spell program to a file called errors.
4. Type the command ls -l and examine the format of the output. Pipe the output of the command ls -l to the word count program wc to obtain a count of the number of files in your directory.