CS 101 - Lab Week 14
Due in lab during week 15, 4/22/2002
Joining UNIX commands
One of the nice things about UNIX shell scripting is the ability to
take the outpur from one command and use it as input to another command.
There is also the ability to read information from a file instead of
typing it in interactively and writing information to a file instead of
displaying it on the terminal screen.
Using the output of one command as input to another
The UNIX pipe character "|" allows two commands to be joined
together so that the output from one command is used as input
to another. The pipe character is normally the upper backslash
key located right about the enter key. The first command is used
as normal; however, instead of its output being shown on the screen,
the output is "piped" into the second command. The second command
typically must be a command that requires a filename to be one
of the command line arguments. In this case the filename is omitted
and the result from the first command are used as the "file".
For example, let us wish to print out a file were each line is
preceeded by a line number. The "-n" option of the cat
command produces output with preceeding line numbers. So if the
result from the cat -n command is piped into the lpr
command (the UNIX command to print a file), we can print out a file with
each line being preceeded with the line number.
cat -n myFile.txt | lpr -PprinterName
Redirection of input and output
Saving the result of any UNIX command to a file can be done by the
use of redirection of output. The greater than character ">" is
used to do this. For example, to save to a file the contains of a file with a
line number preceeding each line we could type:
cat -n myFile.txt > myFileWithLineNumbers.txt
You can also append the output of a command onto the end of a file. The above
command will erase anything in the file "myFileWithLineNumbers.txt" before
writing the new information into the file. To append information use the
double greater than, ">>", as shown below. The sequence of commands will
put into the file outfile.txt the contents of the files file1.txt, file2.txt,
file3.txt and file4.txt. Before each file will be the name of the file and a blank
line will separate between two files.
echo Filename: file1.txt > outfile.txt
cat -n file1.txt >> outfile.txt
echo >> outfile.txt
echo Filename: file2.txt > outfile.txt
cat -n file2.txt >> outfile.txt
echo >> outfile.txt
echo Filename: file3.txt > outfile.txt
cat -n file3.txt >> outfile.txt
echo >> outfile.txt
echo Filename: file4.txt > outfile.txt
cat -n file4.txt >> outfile.txt
You can also redirect the input to a command. This is most often used when running
a program with the same data over and over again. This is very common when testing
and debugging a program. The less than character "<" is used for redirection of
input.
Lab Assignment
The lab assignment is a variation on the script listed for the redirection of
outputs. Your script should take any number of filenames as command line
arguments. These files should be combined into the file "outfile.txt" with
each file having the line number listed before each line. Before each file,
list the name of the file and put a blank line between each file. You should
take the above script and use a loop instead of repeating the same commands
over and over.