On Sun, 2025-01-12 at 23:11 +0000, Harald van Dijk wrote:
> Likewise, when any FD > 2 needs to be passed to an external utility,
> one
> could always just duplicate such FDs via redirection specifically for
> the utility.
>
> $ mksh -c "exec 8</dev/null; ls /proc/self/fd/8"
> ls: cannot access '/proc/self/fd/8': No such file or directory
>
> $ mksh -c "exec 8</dev/null; ls /proc/self/fd/8 8<&8"
> /proc/self/fd/8
>
> $ ksh -c "exec 8</dev/null; ls /proc/self/fd/8"
> ls: cannot access '/proc/self/fd/8': No such file or directory
>
> $ ksh -c "exec 8</dev/null; ls /proc/self/fd/8 8<&8"
> /proc/self/fd/8
That seem like a pretty promising idea.
But does it strictly follow from POSIX, that it works portably in each
(conforming) shell implementation - or is it merely the way all shells
implement it right now?
In particular:
- The exec 8</dev/null sets the redirections in the current shell
execution environment.
The 8<&8 sets the the redirections for the utility (e.g. ls).
The 1st 8 in 8<&8 is the one the utility will "see" as FD 8.
But is it guaranteed that the word part of the redirection (i.e.
the 2nd 8, is the one from the shell execution environment?
Couldn't a shell implementation first open it's (1st) FD8, and
then use the very same to duplicate (and not the one from the shell
execution environment), and then perhaps error out?
- Next, is it in such a schema, i.e.:
command exec <arbitrary redirections> || exit 125
utility <duplications of each used FD number>
(where the redirections on the utility are only of the style n<&n
with n being the same number)
guaranteed, that these duplications cannot fail (and thereby again,
conceal the true exit status of the utility?
E.g. in case of resource outages or whatever?
Thanks,
Chris.