Bug #874

sh: fd redirections

Added by stephane.russell almost 7 years ago. Updated almost 3 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:-
Target version:-

Description

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

History

#1 Updated by dillon almost 7 years ago

: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
<>

#2 Updated by stephane.russell almost 7 years ago

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:

#3 Updated by pavalos almost 3 years ago

  • Description updated (diff)
  • Status changed from New to Closed
  • Assignee deleted (0)

This should work correctly now. Please reopen if you don't agree.

Also available in: Atom PDF