UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 13.15 What to Do with a Full Bit Bucket :-) Chapter 13
Redirecting Input and Output
Next: III. Working with the Filesystem
 

13.16 Store and Show Errors with logerrs

logerrs
This simple script by Maarten Litmaath runs a command, logs any error messages to a file and also sends them to standard error. Because the standard error usually goes to your screen, this lets you see the errors and log them in a file too.

The script's first argument is the log filename. Then type the command and any other arguments for it. Here's an example of running cat foo bar and logging errors to a file named errors. The foo file exists; the bar file doesn't:

$ cat foo
hello world
$ logerrs errors cat foo bar
hello world
bar: No such file or directory
$ cat errors
bar: No such file or directory

These two lines of the script do the work:

exec 3>&1
"$@" 2>&1 >&3 | tee -a "$ERRLOG" >&2

If the >& stuff is a mystery and you're interested in the Bourne shell's handling of file descriptors, see articles 45.20 and 45.21.

- MAL, JP


Previous: 13.15 What to Do with a Full Bit Bucket :-) UNIX Power ToolsNext: III. Working with the Filesystem
13.15 What to Do with a Full Bit Bucket :-) Book IndexIII. Working with the Filesystem

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System