DragonFly bugs List (threaded) for 2007-12
DragonFly BSD
DragonFly bugs List (threaded) for 2007-12
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: sh: fd redirections


From: Stephane Russell <stephane.russell@xxxxxxxxxxxxxxx>
Date: Sun, 16 Dec 2007 03:40:02 -0500

It seems "exec 3>&1" is supposed to open descriptor 3 when it's closed.
I made some tests on both sh and ksh on an AIX, on a Solaris and on
DFBSD/mksh. They all behave this way:

--
$ exec 3>&1
$ echo toto >&3
toto
$ exec 3>&-
$ echo toto >&3
mksh: >&3 : bad file descriptor
--

This is useful to implement this kind of code:

--
exec 3>&1 1>&2
--

This "swap" is used for Ghostscript. PostScript files are sending error
messages to stdout. To avoid messing up the printing, gs stdout is
redirected to stderr (in 1 goes out 2), and the gs print output is going
to descriptor 3 (in 3 goes out 1). On DFBSD/sh, it works, but the
behavior is different. Even this works:

--
srussell@alcyone: {119} sh
$ echo toto >&9
toto
--

This is strange. Maybe the BSD design have choose to map all unknown
descriptors to stdin by default. But it's not consistent with this behavior:

--
srussell@alcyone: {122} sh
$ echo toto > /dev/fd/9
cannot create /dev/fd/9: Bad file descriptor
--

So, it could be a bug.

SR


Matthew Dillon a écrit :
> :Hi,
> :
> :I was testing a few things, and I was wondering if that behavior is normal:
> :
> :$ exec 3>&1
> :$ echo toto >&3
> :toto
> :$ echo toto > /dev/fd/3
> :toto
> :$ exec 3>&-
> :$ echo toto >&3
> :toto
> :$ echo toto > /dev/fd/3
> :cannot create /dev/fd/3: Bad file descriptor
> :
> :Why is >&3 ignores the redirection, but not /dev/fd/3? It seems to me
> :like a bug.
> :
> :SR
> 
>     I think the last part is working as it should.  You close descriptor 3
>     with the 'exec 3>&-' line.   An echo that dup's to descriptor 3 ... that's
>     only a temporary dup for just that command, so descriptor 3 shouldn't
>     exist afterwords, and it doesn't.
> 
>     I'm not sure why descriptor 3 exists in the first part of your script,
>     your original exec dup's descriptor 3 to descriptor 1.  Descriptor 3
>     probably doesn't exist at that point so it probably wound up not changing
>     descriptor 1 at all, and maybe that confused /bin/sh when you did the >&3
>     later on and caused /bin/sh to fail to close the redirected descriptor.
> 
> 					-Matt
> 					Matthew Dillon 
> 					<dillon@backplane.com>




[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]