Kbase 15736: Redirecting stdout and errout : where are my messages ?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  10/05/1998 |
|
Redirecting stdout and errout : where are my messages ?
Many people redirect stdout and stderr in scripts and cron jobs.
But if they don't get it right, they will be asking "Where did my
messages go?"
Here is a simple program that will answer that question.
#include <stdio.h>
main()
{
fprintf(stdout,"Send this string to fd 1\n");
fprintf(stderr,"Send this string to fd 2\n");
}
# Compile this program and run it from a Bourne shell.
$ a.out
Send this string to fd 1
Send this string to fd 2
# Now try it by redirecting standard out.
$ a.out >out.redir
Send this string to fd 2
# Note that standard error still goes to the terminal,
# And standard output is in the specified file.
$ cat out.redir
Send this string to fd 1
# As shown below, the order you place the redirection is important.
# The shell evaluates redirection from left to right.
$ a.out >both.redir 2>&1
$ cat both.redir
Send this string to fd 2
Send this string to fd 1
$ a.out 2>&1 >err.redir
Send this string to fd 2
$ cat err.redir
Send this string to fd 1
# In the first case both standard and error output are redirected
# to the same file. What has happened in the second case is that FIRST
# file descriptor 2 (error output) is redirected to file descriptor 1,
# the terminal, and THEN file descriptor 1 is redirected to err.redir.
# The first case is the one with which most people will be happiest,
# unless they are deliberately trying to seperate standard output and
# error output, in which case they will want something like
$ a.out >out.redir 2>err.redir
Progress Software Technical Support Note # 15736