Question

I have a bash script that simply calls different calls and redirect stdout and stderr outputs to different files.

I've done this:

command 1> datafile 2>> errorfile

However, when the command is erroneous (e.g. wrong username and password combination given as arguments), the error message does not get redirected to the errorfile. I still see the error message on my screen when I run this script. For instance, the error message indicates that I provided a wrong combination of username and password.

What am I doing wrong? I thought I should see no output on screen because I'm redirecting both stdout and stderr to files.

Was it helpful?

Solution

Perhaps the program isn't writing to stderr, but directly opening /dev/tty to communicate with the user? This approach is fairly common when it comes to password interaction: software wants to make sure password prompts get "through" to the user despite any redirections.

If this is the case, you need pseudo-terminal trickery to arrange output to end up in a file.

If you don't have the source of the software, you can use strace/truss to find out what the program is really doing.

OTHER TIPS

Your redirection command is wrong. With bash, try this:

$ cat redir.c
#include <stdio.h>

int main(void) {
        fprintf(stdout, "Hello stdout\n");
        fprintf(stderr, "Hello stderr\n");
}
$ ./redir > txt 2>&1
$ cat txt
Hello stderr
Hello stdout
$ 

The '2>&1' is key to also capture stderr.

It looks like you have an extra '>' in your stderr redirection. Try:

command 1> datafile 2> errorfile

Edit: As was pointed out in the comments, 2>> redirects stderr, appending to the file, whereas 2> overwrites the file.

I'm keeping this answer in place for reference purposes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top