When you first start using UNIX, you learn to redirect your command's output to a file using redirection.
You can use the following to store the output of the
command into a file.
sort volleylist > volleylist.sort
This is very convenient, but sometimes you would like to see the output on the screen and copy the output to a file at the same time. This is the purpose of the
command. It sends your output to the screen and to a file at the same time. You can modify the previous
command to print its output to the screen and copy it to the volleylist.sort file at the same time in the following way:
sort volleylist | tee volleylist.sort
This technique of copying to a file and the screen at the same time is called piping to tee
When writing a shell script, it is common to display output on the screen and to store output in a file at the same time.
For example, if your script creates a report, your users will want to see at least the beginning of the report on the screen to verify that they have the correct information. They will also need to have the report saved in a file so that they can view, modify, and/or print the report once the script is finished. By piping the output to
, you can perform both tasks simultaneously.
You must use a unique file name when creating an output file using
or redirection. If your file name coincides with a name already in use, your script will accidentally overwrite the contents of a pre-existing file.
Shell programmers commonly use the current process id as part of the file name to create unique file names.
Here is a review of processes.
The shell automatically creates a variable named "
" to store the process id number of the current shell. You can use
to create a unique file name. In the FlipBook below is a script called pidfile.
Each time this script is run, the value of
changes. This reflects the fact that UNIX issues a new process number each time a script is run.