Saving Output


In addition to the file redirection operator >, the UNIX System provides several commands that you can use to record output. The command tee can be used to copy standard output to a file, while script can be used to keep a record of your session. You can also use mail from the command line to send output as e-mail.

tee

The tee command is named after a tee joint in plumbing. A tee joint splits an incoming stream of water into two separate streams. tee splits its (standard) input into two or more output streams; one is sent to standard output, the others are sent to the files you specify

The following command uses file to display information about files in the current directory By sending the output to tee, you can view it on your screen and at the same time save a copy in the file filetypes:

 $ file * | tee filetypes

In this example, if the file filetypes already exists, it will be overwritten. You can use tee a filetypes to append output to the file.

You can also use tee inside a pipeline to monitor part of a complex command. The following example prints the output of a grep command by sending it directly to lp. Passing the data through tee allows you to see the output on your screen as well:

 $ grep perl filetypes | tee /dev/tty lp

Note the use of /dev/tty in this example. Recall that tee sends one output stream to standard output, and the other to a specified file. In this case, you cannot use the standard output from tee to view the information, because standard output is used as the input to lp. In order to display the data on the screen, this command makes use of the fact that /dev/tty is the name of the logical file corresponding to your display Sending the data to the “file” /dev/tty displays it on your screen.

Finally, tee can be used in a shell script to create a log file. For example, if you have a script that can be run periodically to backup files, the last line in the script could be

 $ echo "'date' Backup completed." tee −a logfile

This will print a message containing the current date and time to standard output, and also append the message to logfile.

script

The script command copies everything displayed on your terminal screen to a file, including both your input and the system’s prompts and output. You can use it to keep a record of part or all of a session. It can be very handy when you want to document how to solve a complicated problem, or when you are learning to use a new program. To use it, you invoke script with the name of the file in which you want the transcript stored. For example,

 $ script mysession Script started, file is mysession

To terminate the script program and end recording, type CTRL-D:

 $ [CTRL-D] Script done, file is mysession

If you invoke script without a filename argument, it uses the default filename typescript.

An example of a file produced by script is shown here:

 $ script ksh-install Script started on Mon 27 Nov 2006 09:59:58 AM PST $ cd Desktop^M $ gunzip ksh.2006–02–14.linux.i386.gz^M $ mv ksh* ../bin^M $ cd ../bin^M $ ln −s ksh* ksh^M $ Script done on Mon 27 Nov 2006 10:01:06 AM PST

Note that script includes all of the characters you type, including CTRL-M (which represents RETURN), in its output file. The script command is not very useful for recording sessions with screen-oriented programs such as vi because the resulting files include screen control characters that make them difficult to use.

mail

The mail command, and the related commands mailx and Mail, were introduced in Chapter 2. Most users will quickly switch to a more full-featured mail program, but mail is still useful for certain tasks. In particular, it can be used in a pipeline to mail the output of a command, as in this example:

 $ find . -print mail root

This will send a list of files to the root user. The mail command can also be useful in shell scripts, as will be seen in the next chapter.

If the mail command is unable to send a message, it will save it in the file dead.letter.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net