Help with open, dup and dup2
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Help with open, dup and dup2




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 12:51 PM

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.






[ Post a follow-up to this message ]



    Re: Help with open, dup and dup2  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 12:51 PM

>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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 12:51 PM

Thank you very much. 






[ Post a follow-up to this message ]



    Re: Help with open, dup and dup2  
grid


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 10: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,
~





[ Post a follow-up to this message ]



    Re: Help with open, dup and dup2  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 10: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





[ Post a follow-up to this message ]



    Re: Help with open, dup and dup2  
Micah Cowan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-16-06 10: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:09 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register