Unix Programming - Help with open, dup and dup2

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Help with open, dup and dup2





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Help with open, dup and dup2
seifyr@gmail.com

2006-03-16, 7:51 am

What sequence of calls to open, dup and/or dup2 do you perform in your
program in order to achieve the following effects?

$prog1>file 2>&1

and

$prog2 2>&1 > file

I understand which each line does, but I dont understand how to use dup

and dup2 to acheive this. I don't necessarily need code just an
understanding of how do accomplish this.

Gordon Burditt

2006-03-16, 7:51 am

>What sequence of calls to open, dup and/or dup2 do you perform in your
>program in order to achieve the following effects?
>
>$prog1>file 2>&1


filefd = open("file", ...); /* >file */
dup2(filefd, 1);
close(filefd);

dup2(1, 2); /* 2>&1 */

>and
>
>$prog2 2>&1 > file


dup2(1, 2); /* 2>&1 */

filefd = open("file", ...); /* >file */
dup2(filefd, 1);
close(filefd);

>I understand which each line does, but I dont understand how to use dup
>
>and dup2 to acheive this. I don't necessarily need code just an
>understanding of how do accomplish this.


Gordon L. Burditt
seifyr@gmail.com

2006-03-16, 7:51 am

Thank you very much.

grid

2006-03-16, 5:53 pm

Hi,
I am a little confused with the second one.Lemme explain what I
understand.
Gordon Burditt wrote:
>
>
> filefd = open("file", ...); /* >file */
> dup2(filefd, 1);
> close(filefd);
>
> dup2(1, 2); /* 2>&1 */


Here, stdout is redirected to file,so now file is the stdout.Then stderr
is redirected to stdout,which is also now the file.

>
>
> dup2(1, 2); /* 2>&1 */
>
> filefd = open("file", ...); /* >file */
> dup2(filefd, 1);
> close(filefd);
>


Here,stderr is redirected to stdout.stdout is redirected to file and
hence stdout is file.

Unlike the first case where both the stderr & stdout output goto the
file,in this case stderr goes to stdout and stdout goes to file.
Is the & then really required with the 1(stdout).

Thanks,
~
Gordon Burditt

2006-03-16, 5:53 pm

>>>$prog2 2>&1 > file
>
>Here,stderr is redirected to stdout.stdout is redirected to file and
>hence stdout is file.
>
>Unlike the first case where both the stderr & stdout output goto the
>file,in this case stderr goes to stdout and stdout goes to file.
>Is the & then really required with the 1(stdout).


$prog2 2>1 >file

would redirect stdout to a file named 'file' and redirect
stderr to a file named '1'.

Gordon L. Burditt
Micah Cowan

2006-03-16, 5:53 pm

grid <prohit99@gmail.com> writes:

> Hi,
> I am a little confused with the second one.Lemme explain what I
> understand.
> Gordon Burditt wrote:
>
> Here, stdout is redirected to file,so now file is the stdout.Then
> stderr is redirected to stdout,which is also now the file.
>
>
> Here,stderr is redirected to stdout.stdout is redirected to file and
> hence stdout is file.
>
> Unlike the first case where both the stderr & stdout output goto the
> file,in this case stderr goes to stdout and stdout goes to file.
> Is the & then really required with the 1(stdout).


Your question about & and 1 has been answered elsethread... I'll just
clarify the preceding statement...

Unlike the first case where both stderr and stdout go to "file", the
second case stderr goes to the /original/ stdout, and stdout now goes
to "file".

If stdout was on a terminal, this command's stderr will still go to
that terminal, whereas the command's stdout goes to "file". If you
pipe the whole thing into something else, stderr will go into the pipe
instead of the terminal.

Is that clear enough?

-Micah
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com