Subject: Re: Why does redirecting stderr prevent file list display? References: <0wiI7.39202$S4.3462556@newsread1.prod.itd.earthlink.net>
<zbCI7.41419$S4.3703529@newsread1.prod.itd.earthlink.net>
<3bf319ff$0$79557$8eec23a@newsreader.tycho.net>
<3bf3a5e8.4899124@news.earthlink.net> From: spcecdt@deeptht.armory.com (John DuBois) Date: 15 Nov 2001 23:24:09 GMT In article <3bf3a5e8.4899124@news.earthlink.net>, Dave Dickerson <ddinaz@hotmail.com> wrote: >On 15 Nov 2001 01:27:27 GMT, spcecdt@deeptht.armory.com (John DuBois) wrote: > >>exec 3>&1; find . -print | tee /dev/fd/2 2>&3 | cpio -o -O /tmp/archive.cpio 2>/tmp/errors > >I understand (mostly) everthing here except the reason for >/dev/fd/2. You want a file list to appear on stdout (fd 1). The file list is available in the pipe between find and cpio, so you should be able to tee it off there. However, file descriptors 0 and 1 are "used up" at that point for the input & output pipes, and you don't want to use fd 2 (stderr) since you're specifically trying to avoid mixing the file list with error output. The solution is to create an extra fd that is a clone of fd 1; that's what the exec 3>&1 does - after the exec, fd 3 refers to the same thing fd 1 did. Now all you need to do to accomplish your goal is tee to fd 3. However, there are two obstacles to this. First, tee requires a filename. That's easily dealt with thanks to the 'dup' driver that has been included since 5.0.0 - /dev/fd/n refers to whatever fd n does. The second obstacle is that any fds opened via the 'exec' mechanism other than 0, 1, and 2 are closed when the shell execs a program like tee. Fortunately, we can get around this by having the shell itself dup fd 3 onto fd 2 (2>&3) before exec'ing, and then tee to /dev/fd/2 (note that we could just as well use any other fd not in use here, e.g. tee /dev/fd/4 4>&3, since fds opened in this way are not marked to be closed on exec the way fds > 2 opened via the 'exec' mechanism are).
John
--
John DuBois spcecdt@armory.com. KC6QKZ/AE http://www.armory.com./~spcecdt/
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar