views:

271

answers:

3

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.

+4  A: 

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.

Martin v. Löwis
Can you please elaborate more?
Davide
What specifically do you want to know? The question was asked more than a year ago, so maybe you better ask a new, specific question.
Martin v. Löwis
Ok, please see if you have an answer: http://stackoverflow.com/questions/4056075/module-program-directly-writes-to-stderr-making-redirection-hard
Davide
A: 

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.

Dirk Eddelbuettel
No, that redirection is perfectly fine. Try it. It works.
bcat
Jefromi
@bcat: I stand corrected. I mis-read the question -- I usually want stdout and stderr in the same file, which is what I showed. Which is not what was asked. Oops.
Dirk Eddelbuettel
@Jefromi: if I do whatever you stated, datafile gets NOTHING. That's why I changed the command as above (my question).
bLee
@bLee: Martin v. Löwis' answer sounds most likely then. I was just suspicious of the 2>> shorthand (for no good reason, really).
Jefromi
A: 

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.

PTBNL
well, it was for "append". If I have >, then new errorfile will get created each time.
bLee
@bLee: Doh! You're right. That's what I get for answering on my way out of the office. Will edit.
PTBNL